项目地址:https://github.com/wz2cool/mybatis-dynamic-query

文档地址:https://wz2cool.gitbooks.io/mybatis-dynamic-query-zh-cn/content/

博客文章:https://wz2cool.github.io/tags/Mybatis-Dynamic-Query/

之前写过一个整合文章可以查考:Mybatis Dynamic Query 框架整合

增加特性

1. jdbcType 支持在insert/update语句

    根据官方文档说明:就是当你要去插入修改可空列的时候,必须指定jdbcType,否则可能会报错。(ps:本人用MySql 不指定好像没什么问题,但是既然官方说了咱们也就照办吧。)

The JDBC Type from the list of supported types that follows this table. The JDBC type is only required for nullable columns upon insert, update or delete. This is a JDBC requirement, not an MyBatis one. So even if you were coding JDBC directly, you'd need to specify this type – but only for nullable values.

如何指定这个 jdbcType 其实也很简单,我们其实在 Column 这个注解上加上了这个属性。

@Table(name = "product")
public class Product {
@Column(name = "product_id", insertIgnore = true, jdbcType = JdbcType.INTEGER)
private Integer productID;
@Column(jdbcType = JdbcType.VARCHAR)
private String productName;
@Column(jdbcType = JdbcType.NUMERIC)
private BigDecimal price;
@Column(jdbcType = JdbcType.INTEGER)
private Integer categoryID;
// get /set ...
}

测试调用 insert

@Test
public void testInsert() throws Exception {
Product newProduct = new Product();
Random random = new Random(10000L);
Integer id = random.nextInt();
newProduct.setProductID(id);
newProduct.setCategoryID(1);
String productName = "Product" + id;
newProduct.setProductName(productName); // insert.
ParamExpression paramExpression = mybatisQueryProvider.getInsertExpression(newProduct);
Map<String, Object> insertParam = new HashMap<>();
insertParam.put("insertExpression", paramExpression.getExpression());
insertParam.putAll(paramExpression.getParamMap()); int result = northwindDao.insert(insertParam);
assertEquals(1, result);
}

我debug 打印一下我们生成生成的insert 语句, 看到jdbcType 被加上去了

INSERT INTO product (product_name, category_id) VALUES (#{productName,jdbcType=VARCHAR}, #{categoryID,jdbcType=INTEGER})

2.批量插入

批量插入其实是一个单插入差不多,但是我们需要注意,

  1. 如果主键是自增的,其实我们不应该在插入的时候插入,这也是为什么 Column 注解里面加了一个 insertIgnore 属性,这个和insertIfNull 不一样,这个是说无论有没有值在插入的时候直接忽略掉,特别适合在自增属性上设置。
  2. 批量插入 insertIfnull 属性将会失效,因为有些数据的列可能是null,有些数据列可能不是null这个不好区分。
@Test
public void testBulkInsert() throws Exception {
Product p1 = new Product();
p1.setProductName("product1");
p1.setPrice(BigDecimal.valueOf(500));
p1.setCategoryID(2); Product p2 = new Product();
p2.setProductName("product2");
p2.setPrice(BigDecimal.valueOf(1000));
p2.setCategoryID(2); Product[] products = new Product[]{p1, p2}; ParamExpression paramExpression = mybatisQueryProvider.getBulkInsertExpression(products);
Map<String, Object> insertParam = new HashMap<>();
insertParam.put("insertExpression", paramExpression.getExpression());
insertParam.putAll(paramExpression.getParamMap()); int result = northwindDao.insert(insertParam);
assertEquals(2, result);
}

输出我们就可以看到两条数据同时插入

==>  Preparing: INSERT INTO product (price, product_name, category_id) VALUES (?, ?, ?),(?, ?, ?)
==> Parameters: 500(BigDecimal), product1(String), 2(Integer), 1000(BigDecimal), product2(String), 2(Integer)
<== Updates: 2

3. 表达式获取属性名字

以前在做wpf的时候需要RaisePropertyChange 经常这么写的。 在填写FilterDescriptor 和 SortDescriptor 的时候我们需要指定这个是对哪个属性进行筛选或者排序,现在我们就用一个表达式去代替写死的String,这样写的好处在于以后改起来就非常容易了,不用去找String 了。有兴趣大家可以看一下实现 Mybatis Dynamic Query 属性表达式

@Test
public void simpleDemo() throws Exception {
FilterDescriptor idFilter =
new FilterDescriptor(FilterCondition.AND,
Product.class,
// 这里就是一个表达式,把原来写死的"price" 换掉。
product -> product.getPrice(),
FilterOperator.GREATER_THAN_OR_EQUAL, 2);
Map<String, Object> queryParams =
mybatisQueryProvider.getWhereQueryParamMap(
Product.class, "whereExpression", idFilter);
}

bug

  1. 主要解决了在sql server 中更新语句报错。

最后

1.0.2 搞完了, 本来想7号发的,没想到赶了赶就提前了哈, 感觉基本功能都有了,大家有什么建议可以提出来放到1.0.3 版本中去了。

关注我 ##

最后大家可以关注我和 Mybatis-Dynamic-query项目 _

Follow @wz2cool Star Fork

Mybatis Dynamic Query 1.0.2版本的更多相关文章

  1. Mybatis Dynamic Query 2.0.2

    项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...

  2. Mybatis Dynamic Query 2.0 入门

    简介 2.0 昨天打包好了,主要是整合了tk.mybatis.mapper 到项目中去,所以和1.x比起来主要多了一个通用mapper.因为作者主要是使用springboot 这里讲一下Springb ...

  3. Mybatis Dynamic Query 框架整合

    项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...

  4. SpringMVC+MyBatis+Druid使用MySQL8.0.11版本

    1.使用MySQL8.0.11版本,要使用5.1.45或其他高版本驱动jar包,我本地使用的是最新的8.0.11 2.更换了MySQL驱动后,报Cannot find class [com.aliba ...

  5. Mybatis Dynamic Query 简单筛选

    在框架中,筛选描述类有两种(FilterDescriptor, FilterGroupDescriptor),这里我们主要举例来说明FilterDescriptor用法. FilterDescript ...

  6. Mybatis Dynamic Query 更新

    文章目录 1. 简介 2. 准备工作 3. 开始更新 3.1. update 3.2. update Null 4. 结束 5. 关注@我 项目地址:https://github.com/wz2coo ...

  7. Sharding-JDBC(二)2.0.3版本实践

    目录 一.Sharding-JDBC依赖 二.分片策略 1. 标准分片策略 2. 复合分片策略 3. Inline表达式分片策略 4. 通过Hint而非SQL解析的方式分片的策略 5. 不分片的策略 ...

  8. eclipse- Web-app verson=2.5 调整将Dynamic Web Module3.0降为2.5

    如果提示cannot change version of project facet Dynamic Web Module to 2.5 1.把Dynamic Web Module复选框,勾选去掉,点 ...

  9. [Liferay6.2]Liferay Dynamic Query API示例

    介绍 Liferay提供了几种方法定义复杂的查询用来检索数据库中的数据. 通常情况下,在每个service Entity中,通过定义一些'finder'方法,可以便捷地满足基本的数据查询操作. 但是, ...

随机推荐

  1. Java 9 揭秘(5. 实现服务)

    Tips做一个终身学习的人. Implementing Services 在这章中,主要介绍如下内容: 什么服务,服务接口,服务提供者: 在 JDK 9之前和在JDK 9中如何实现服务 如何使用Jav ...

  2. Linux命令 查看及修改文件属性

    chmod [功能说明] 改变文件的访问权限  #Linux中访问权限分为:文件属主(文件的创建者)文件组属主(创建者所处的组)和其他(其他用户) [语法格式] Chmod[参数]mode[文件名或目 ...

  3. 解决ionic在Android和iOS的一些样式上的冲突

    //设置默认返回按钮的文字 $ionicConfigProvider.backButton.previousTitleText(false).text('返回'); // 设置全局 $http 超时 ...

  4. idea live template高级知识, 进阶(给方法,类,js方法添加注释)(二)

    上一篇文章(http://www.cnblogs.com/xzjxylophone/p/6994488.html) 是在 groovyScript中直接添加的代码,这个看起来是简单,粗暴,麻烦和不美观 ...

  5. 给js动态创建的对象绑定事件

    1.使用原生JS动态为动态创建的对象绑定事件 1-1.创建一个function,用来兼容IE8以下浏览器添加事件 function addEvent(el, type, fn) {  if(el.ad ...

  6. PHPCMS V9 导航栏当前栏目高亮

    实际上这个东西可有可无,很多站点看似导航栏当鼠标指向后都会变化等高亮处理,一般都比较醒目,但是实质点击过去后,都还是只是刚才的样式,因为这些站点的导航栏都没有对当前选中栏目做CSS的指定变化处理. 该 ...

  7. AJAX数据请求

    ajax数据请求需要四个步骤:(请求文本内容) 1.创建XMLHttpRequest对象: 2.打开与服务起的链接: 3.发送给服务器: 4.响应就绪. <!DOCTYPE html> & ...

  8. Example008关闭IE窗口时,不弹出询问对话框

    <!-- 实例008关闭IE主窗口时,不弹出询问对话框 --> <meta charset="UTF-8"> <a href="#" ...

  9. centos中NAT模式下静态IP连接外网

    使用linux虚拟机时,通常会用到yum命令来安装软件,然而这个命令需要连外网下载软件,用maven下载jar包也需要外网.虚拟机在内网可以互相ping通,然而ping不了外网,于是通过试验,终于找到 ...

  10. 分页插件Jpages的使用

    项目原因需要前端做分页表格,之前做了一个ul的分页效果,但是感觉自己写还是造轮子了,今天网上看到Jpqges插件就试了下,感觉平时使用挺方便的,写一下自己的使用过程. 先上套图,下载下来就2个js和1 ...