对Mybatis Dynamic Query 没概念的同学可以传送到: http://www.cnblogs.com/wz2cool/p/7268428.html

简介

2.0 昨天打包好了,主要是整合了tk.mybatis.mapper 到项目中去,所以和1.x比起来主要多了一个通用mapper。

哈哈~ 简单的说,单表用通用mapper, 多表查询仍然用动态查询。

因为作者主要是使用springboot 这里讲一下Springboot 配法。有问题的话可以参照本章demo。

本章demo下载地址: https://github.com/wz2cool/mdq2.0test

配置步骤

  1. 添加依赖
<!-- 基本库 -->
<dependency>
<groupId>com.github.wz2cool</groupId>
<artifactId>mybatis-dynamic-query</artifactId>
<version>2.0.0</version>
</dependency>
<!-- 主要注册通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.3</version>
</dependency>
<!-- mybatis 最新版本 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- 如果有Spring boot web 自带jackson 这个可以不要,防止版本冲突 -->
<!-- <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>-->
<!-- spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
  1. 在applicaiton.properties 注册DynamicQueryMapper, (这里也可以注册自己写的mapper)。
mapper.mappers[0]=com.github.wz2cool.dynamic.mybatis.mapper.DynamicQueryMapper
  1. 在Application 类中设置扫描mapper包的路径
@SpringBootApplication
@MapperScan(basePackages = "com.github.wz2cool.mdqtest.mapper")
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

2.0 新特性

单表无XML

主要是tk.mybatis.mapper 已经实现了很多默认通用模板,所以我们无需再去写XML,DynamicQueryMapper 在tk.mybatis.mapper 基础上对筛选使用了我们自己的筛选器进行筛选。

实体类Product

@Table(name = "product")
public class Product {
@Id
@Column(name = "product_id")
private Integer productId;
@Column(name = "name")
private String productName;
private BigDecimal price;
private Integer categoryId;
// get/set...
}

对于mapper 我们只要继承一下DynamicQueryMapper 即可。

public interface ProductDao extends DynamicQueryMapper<Product> {
}

然后我们就可以看到通用方法已经可使用了

全程强类型编写

当我们使用最常用的筛选表述器FilterDescriptor/SortDescriptor 的时候,我们需要填写我们对哪个属性进行操作,以前是写一个String,现在我们可以使用表达式编写。

FilterDescriptor nameFilter = new FilterDescriptor(
User.class, User::getUsername,
FilterOperator.CONTAINS, "18");

添加自定义筛选

当FilterDescriptor 和 FilterGroupDescriptor 不能满足我们时候我们需要使用自定义筛选,比如H2数据库在做位运算的时候,需要调用Bitand 方法。

@Test
public void testBitand() throws Exception {
if ("h2".equalsIgnoreCase(active)) {
CustomFilterDescriptor bitFilter =
new CustomFilterDescriptor(FilterCondition.AND,
"Bitand(product_id, {0}) > {1}", 2, 0);
Map<String, Object> filterParams = MybatisQueryProvider.getWhereQueryParamMap(
Product.class, "whereExpression", bitFilter);
List<Product> products = northwindDao.getProductByDynamic(filterParams);
assertEquals(true, products.size() > 0);
} else {
CustomFilterDescriptor bitFilter =
new CustomFilterDescriptor(FilterCondition.AND,
"product_id & {0} > {1}", 2, 0);
Map<String, Object> filterParams = MybatisQueryProvider.getWhereQueryParamMap(
Product.class, "whereExpression", bitFilter);
List<Product> products = northwindDao.getProductByDynamic(filterParams);
assertEquals(true, products.size() > 0);
}
}

AS 枚举列

以前我们都是直接 SELECT * XXX 来读取数据,这样做有两个非常不好的地方

  1. 选择了所有的列,不是每个列都是我们需要的,增加传输时间。
  2. 当多表查询的时候,如果你两个表里面有列重名了,这样会有问题。

MybatisQueryProvider 帮助类中增加columsExpression占位。

Map<String, Object> params = MybatisQueryProvider.getQueryParamMap(dynamicQuery,
"whereExpression",
"sortExpression",
"columnsExpression");
return productViewDao.getProductViewByDynamicQuery(params);

在 xml 中的写法

<select id="getProductViewByDynamicQuery" resultType="com.github.wz2cool.mdqtest.model.entity.view.ProductView">
SELECT ${columnsExpression} FROM product LEFT JOIN category on product.category_id = category.category_id
<if test="whereExpression != null and whereExpression != ''">WHERE ${whereExpression}</if>
<if test="orderExpression != null and orderExpression != ''">ORDER BY ${orderExpression}</if>
</select>

输出的时候我们可以看到每个列都被AS 成为对应的属性列了

==>  Preparing: SELECT product.product_id AS product_id, product.price AS price, category.description AS description, category.name AS category_name, product.name AS product_name, category.category_id AS category_id FROM product LEFT JOIN category on product.category_id = category.category_id WHERE ((product.price > ? AND product.price < ?) AND category.name = ?)
==> Parameters: 10(Integer), 20(Integer), Condiments(String)
<== Columns: PRODUCT_ID, PRICE, DESCRIPTION, CATEGORY_NAME, PRODUCT_NAME, CATEGORY_ID
<== Row: 3, 16.5000, test, Condiments, Northwind Traders Cajun Seasoning, 2
<== Row: 8, 17.4375, test, Condiments, Northwind Traders Walnuts, 2
<== Total: 2

json 序列化支持

我们筛选器现在已经支持json 序列化,这就意味着,我们查询可以通过接口完全动态化。当然你也可以把json 放入数据库,当做一个配置来用。

可以运行一下我们的demo,打开swagger: http://localhost:8080/swagger-ui.html

  1. 先去获取一下我们示例的json (GET /serialize/getGroupPriceFilters)

  2. 我们把的出来的json 调用 (POST /data/getProductsByDynamicQuery)

文章整合

Mybatis Dynamic Query 简单筛选

Mybatis Dynamic Query 组筛选

Mybatis Dynamic Query 排序

Mybatis Dynamic Query 筛选+排序

Mybatis Dynamic Query 插入

Mybatis Dynamic Query 更新

Mybatis Dynamic Query 删除

Mybatis Dynamic Query 属性表达式

Mybatis Dynamic Query join视图

结束

2.0 更新概括一下:

  1. 整合tk.mybatis.mapper, 并自定义 DynamicQueryMapper 通用mapper。
  2. 添加自定义筛选 CustomFilterDescriptor。
  3. AS 枚举列。
  4. json 序列化支持。

关注我 ##

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

Follow @wz2cool Star Fork

Mybatis Dynamic Query 2.0 入门的更多相关文章

  1. Mybatis Dynamic Query 2.0.2

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

  2. Mybatis Dynamic Query 1.0.2版本

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

  3. Mybatis Dynamic Query 框架整合

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

  4. Mybatis Dynamic Query 简单筛选

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

  5. Mybatis Dynamic Query 更新

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

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

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

  7. Json.Net6.0入门学习试水篇

    原文:Json.Net6.0入门学习试水篇 前言 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.简单地说,JSON 可以将 JavaScript 对象中 ...

  8. Mybatis-Plus3.0入门手册

    Mybatis-Plus3.0入门手册   ref: https://blog.csdn.net/moshowgame/article/details/81008485 Mybatis-Plus简介 ...

  9. Mybatis系列(一)入门

    Mybatis系列(一)入门 mybatis简介 MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结 ...

随机推荐

  1. Example001使用window对象打开窗口

    <!--实例001使用window对象打开窗口--> <script> <!--使用window对象打开窗口的语法格式如下--> <!--window.ope ...

  2. php如何上传txt文件,并且读取txt文件

    1.创建目录如下

  3. create groups 和 create folder reference

      当将文件拖入工程中的时候会出现这个对话框,这个对话框中在Added folders中有两种选择:Create groups 和 Create folder references   这两种的区别是 ...

  4. openssl req(生成证书请求和自建CA)

    伪命令req大致有3个功能:生成证书请求文件.验证证书请求文件和创建根CA.由于openssl req命令选项较多,所以先各举几个例子,再集中给出openssl req的选项说明.若已熟悉openss ...

  5. centos+apache 2.x 开启gzip压缩

    最近做了一个网站(PHP+Apache+MySQL),挂在百度云平台上面,基本配置是2G内存+5Mb带宽,每次打开主页都需要2-3s左右的时间,对于一个垂直搜索引擎来说,用户体验肯定会很差. 于是开始 ...

  6. React 实践项目 (一)

    React在Github上已经有接近70000的 star 数了,是目前最热门的前端框架.而我学习React也有一段时间了,现在就开始用 React+Redux 进行实战! 项目代码地址:https: ...

  7. spring基础系列--JavaConfig配置

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/7171011.html 早以前,Spring推荐使用XML的方式来定义Bean及Bean之间 ...

  8. 支持苹果IPV6 ONLY 的socket 修改方法

    首先别错误理解以为app 必须支持 ipv6 的服务端, 只需要支持 ipv6的客户端需要访问 ipv4 的服务端. 目前需要经过  NAT64 转换就能达到目的,客户端只需要支持该转换就能实现, 在 ...

  9. Centos 7部署大众点评CAT(一)——单服务器部署

    前一篇拙作上传的时间已经过去2个月了,中间并不是闲着...主要是忙着学习各种组件的安装,写了几篇安装心得存在硬盘里. 最近尝试了点评开源的CAT监控平台的安装,并且希望能够引入到工作中.在部署实践的过 ...

  10. shell统计文本中单词的出现次数

    Ubuntu14.04 给定一个文本,统计其中单词出现的次数 方法1 # solution 1 grep与awk配合使用,写成一个sh脚本 fre.sh sh fre.sh wordfretest.t ...