Magician 2.0 发布,彻底抛弃底层,全面迎接 Netty

栏目: 软件资讯 · 发布时间: 4年前

内容简介:经过了将近 4 个月,我终于又有了一点点闲置的时间,刚好趁这波机会 将项目进行了一次大升级,所以版本号也是飞跃式的上升,从1.1.17 直接升级到2.0。 本次升级主要是以下三个方面 Magician 的定位发生了改变,以...

经过了将近 4 个月,我终于又有了一点点闲置的时间,刚好趁这波机会 将项目进行了一次大升级,所以版本号也是飞跃式的上升,从1.1.17 直接升级到2.0。

本次升级主要是以下三个方面

  1. Magician 的定位发生了改变,以前是一个网络编程包,现在改成了小型http服务,因为底层被彻底抛弃了,全部换成了Netty,现在只是基于Netty开发的http服务,不在具备自定义协议的能力了。
  2. Magician-Web 为了配合Magician的升级,也进行了一次升级
  3. Magician-JDBC彻底进行了重构,抛弃了 SQL 构建器,改成了更为简单的方式,单表操作不需要写SQL,有条件构造器可以使用,复杂操作可以自己写sql,同时提供了事务管理器

具体的变化

Magician 除了底层大换血,全部改成了Netty,其他基本没变,只是少了一个UDP的支持,从使用者的角度来看 基本没啥变化

Magician-Web只是配合Magician进行的一次小小的调整,所以几乎没变化。

重点在Magician-JDBC

Magician-JDBC的变化

依然保留了多数据源的支持,其他变化如下

单表无SQL操作

插入数据

ParamPO paramPO = new ParamPO();
paramPO.setUserName("a");
paramPO.setUserEmail("test@qq.com");

int result = JDBCTemplate.get().insert("表名", paramPO);

修改数据

// 构建修改条件
List<Condition> conditionList = new ArrayList<>();
conditionList.add(Condition.get("id = ?", 4));

// 构建修改数据
ParamPO paramPO = new ParamPO();
paramPO.setUserName("a");
paramPO.setUserEmail("test@qq.com");

// 执行修改
int result = JDBCTemplate.get().update("表名", paramPO, conditionList);

删除数据

// 构建删除条件
List<Condition> conditionList = new ArrayList<>();
conditionList.add(Condition.get("id = ?", 42));

// 执行删除
int result = JDBCTemplate.get().delete("表名", conditionList);

查询数据

// 构建查询条件
List<Condition> conditionList = new ArrayList<>();
conditionList.add(Condition.get("id > ?", 10));
conditionList.add(Condition.get("and user_name != ?", "a"));
conditionList.add(Condition.get("order by create_time desc", Condition.NOT_WHERE));

// 执行查询
List<ParamPO> result = JDBCTemplate.get().select("xt_message_board", conditionList, ParamPO.class);

自定义sql

增删改

ParamPO paramPO = new ParamPO();
paramPO.setUserName("testTx222");
paramPO.setUserEmail("testTx222@qq.com");
paramPO.setId(4);

// 采用{}占位符的写法
int result = JDBCTemplate.get().exec("update xt_message_board set user_name = {user_name} , user_email = {user_email} where id = {id}", paramPO);

// 采用 ? 占位符的写法
int result = JDBCTemplate.get().exec("update xt_message_board set user_name = ? , user_email = ? where id = ?", new Object[]{"testTx222","testTx222@qq.com", 4});

查询数据

ParamPO paramPO = new ParamPO();
paramPO.setId(5);
paramPO.setUserName("a");

// 采用{}占位符的写法
List<ParamPO> result = JDBCTemplate.get("dataSource").selectList("select * from xt_message_board where id > {id} and user_name != {user_name}", paramPO, ParamPO.class);

// 采用 ? 占位符的写法
List<ParamPO> result = JDBCTemplate.get("dataSource").selectList("select * from xt_message_board where id > ? and user_name != ?", new Object[]{5, "a"}, ParamPO.class);

分页查询

// 查询条件
ParamPO paramPO = new ParamPO();
paramPO.setId(5);
paramPO.setUserName("a");

// 查询参数
PageParamModel pageParamModel = new PageParamModel();
pageParamModel.setCurrentPage(1);
pageParamModel.setPageSize(10);
pageParamModel.setParam(paramPO);

// 使用默认countSql查询
PageModel<ParamPO> pageModel =  JDBCTemplate.get().selectPage("select * from xt_message_board where id > {id} and user_name != {user_name}", pageParamModel, ParamPO.class);

// 使用自定义countSql查询
String countSql = "自己定义countSql";

PageModel<ParamPO> pageModel =  JDBCTemplate.get().selectPageCustomCountSql("select * from xt_message_board where id > {id} and user_name != {user_name}", countSql, pageParamModel, ParamPO.class);

事务管理

// 开启事务
TransactionManager.beginTraction();

try {
    ParamPO paramPO = new ParamPO();
    paramPO.setUserName("testTx222");
    paramPO.setUserEmail("testTx222@qq.com");
    paramPO.setId(4);
    int result = JDBCTemplate.get().exec("update xt_message_board set user_name = {user_name} , user_email = {user_email} where id = {id}", paramPO);

    // 提交
    TransactionManager.commit();
} catch(Execption e){
    // 回滚
    TransactionManager.rollback();
}

详情可以访问官方查看文档哦

https://magician-io.com


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Filter Bubble

The Filter Bubble

Eli Pariser / Penguin Press / 2011-5-12 / GBP 16.45

In December 2009, Google began customizing its search results for each user. Instead of giving you the most broadly popular result, Google now tries to predict what you are most likely to click on. Ac......一起来看看 《The Filter Bubble》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具