1.1    什么是动态sql

mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。

1.2    需求

用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。

对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。

1.3    mapper.xml

<!-- 用户信息综合查询#{userCustom.sex}:取出pojo包装对象中性别值${userCustom.username}:取出pojo包装对象中用户名称-->
<select id="findUserList" parameterType="com.iot.mybatis.po.UserQueryVo"resultType="com.iot.mybatis.po.UserCustom">SELECT * FROM user<!--  where 可以自动去掉条件中的第一个and --><where><if test="userCustom!=null"><if test="userCustom.sex!=null and userCustom.sex != '' ">AND user.sex=#{userCustom.sex}</if><if test="userCustom.username!=null and userCustom.username != '' ">AND user.username LIKE '%${userCustom.username}%'</if></if></where></select><!-- 用户信息综合查询总数parameterType:指定输入类型和findUserList一样resultType:输出结果类型
-->
<select id="findUserCount" parameterType="com.iot.mybatis.po.UserQueryVo" resultType="int">SELECT count(*) FROM user<where><if test="userCustom!=null"><if test="userCustom.sex!=null and userCustom.sex != '' ">AND user.sex=#{userCustom.sex}</if><if test="userCustom.username!=null and userCustom.username != '' ">AND user.username LIKE '%${userCustom.username}%'</if></if></where>
</select>

1.4    测试代码

  • 测试结果

1.注释掉testFindUserList()方法中的userCustom.setUsername("张三");

//由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中
userCustom.setSex("1");
//userCustom.setUsername("张三");
userQueryVo.setUserCustom(userCustom);
  • 1
  • 2
  • 3
  • 4

输出

DEBUG [main] - Checking to see if class com.iot.mybatis.mapper.UserMapper matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class com.iot.mybatis.mapper.UserMapperTest matches criteria [is assignable to Object]
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 352359770.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1500955a]
DEBUG [main] - ==>  Preparing: SELECT * FROM user WHERE user.sex=?
DEBUG [main] - ==> Parameters: 1(String)
DEBUG [main] - <==      Total: 6
[User [id=10, username=张三, sex=1, birthday=Thu Jul 10 00:00:00 CST 2014, address=北京市], User [id=16, username=张小明, sex=1, birthday=null, address=河南郑州], User [id=22, username=陈小明, sex=1, birthday=null, address=河南郑州], User [id=24, username=张三丰, sex=1, birthday=null, address=河南郑州], User [id=25, username=陈小明, sex=1, birthday=null, address=河南郑州], User [id=28, username=王小军, sex=1, birthday=Tue Feb 23 00:00:00 CST 2016, address=河南郑州]]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

可以看到sql语句为reparing: SELECT * FROM user WHERE user.sex=?,没有username的部分

2.userQueryVo设为null,则userCustom为null

//List<UserCustom> list = userMapper.findUserList(userQueryVo);
List<UserCustom> list = userMapper.findUserList(null);
  • 1
  • 2

输出

DEBUG [main] - ==>  Preparing: SELECT * FROM user
DEBUG [main] - ==> Parameters:
DEBUG [main] - <==      Total: 9
[User [id=1, username=王五, sex=2, birthday=null, address=null], User [id=10, username=张三, sex=1, birthday=Thu Jul 10 00:00:00 CST 2014, address=北京市], User [id=16, username=张小明, sex=1, birthday=null, address=河南郑州], User [id=22, username=陈小明, sex=1, birthday=null, address=河南郑州], User [id=24, username=张三丰, sex=1, birthday=null, address=河南郑州], User [id=25, username=陈小明, sex=1, birthday=null, address=河南郑州], User [id=26, username=王五, sex=null, birthday=null, address=null], User [id=27, username=王大军, sex=2, birthday=Tue Feb 23 00:00:00 CST 2016, address=河南郑州], User [id=28, username=王小军, sex=1, birthday=Tue Feb 23 00:00:00 CST 2016, address=河南郑州]]
  • 1
  • 2
  • 3
  • 4
  • 5

可以看到sql语句变为了SELECT * FROM user

1.5    sql片段

1.5.1    需求

将上边实现的动态sql判断代码块抽取出来,组成一个sql片段。其它的statement中就可以引用sql片段。

方便程序员进行开发。

1.5.2    定义sql片段

<!-- 定义sql片段
id:sql片段的唯 一标识经验:是基于单表来定义sql片段,这样话这个sql片段可重用性才高
在sql片段中不要包括 where-->
<sql id="query_user_where"><if test="userCustom!=null"><if test="userCustom.sex!=null and userCustom.sex!=''">AND user.sex = #{userCustom.sex}</if><if test="userCustom.username!=null and userCustom.username!=''">AND user.username LIKE '%${userCustom.username}%'</if></if>
</sql>

1.5.3    引用sql片段

在mapper.xml中定义的statement中引用sql片段:

<!-- 用户信息综合查询#{userCustom.sex}:取出pojo包装对象中性别值${userCustom.username}:取出pojo包装对象中用户名称-->
<select id="findUserList" parameterType="com.iot.mybatis.po.UserQueryVo"resultType="com.iot.mybatis.po.UserCustom">SELECT * FROM user<!--  where 可以自动去掉条件中的第一个and --><where><!-- 引用sql片段 的id,如果refid指定的id不在本mapper文件中,需要前边加namespace --><include refid="query_user_where"></include><!-- 在这里还要引用其它的sql片段  --></where>
</select>

1.1    foreach

向sql传递数组或List,mybatis使用foreach解析

1.1.1    需求

在用户查询列表和查询总数的statement中增加多个id输入查询。两种方法,sql语句如下:

  • SELECT * FROM USER WHERE id=1 OR id=10 OR id=16
  • SELECT * FROM USER WHERE id IN(1,10,16) 

1.1.2    在输入参数类型中添加List<Integer> ids传入多个id

public class UserQueryVo {//传入多个idprivate List<Integer> ids;getter、setter方法。。。
}

1.1.3    修改mapper.xml

WHERE id=1 OR id=10 OR id=16

在查询条件中,查询条件定义成一个sql片段,需要修改sql片段。

<if test="ids!=null"><!-- 使用 foreach遍历传入idscollection:指定输入 对象中集合属性item:每个遍历生成对象中open:开始遍历时拼接的串close:结束遍历时拼接的串separator:遍历的两个对象中需要拼接的串--><!-- 使用实现下边的sql拼接:AND (id=1 OR id=10 OR id=16)--><foreach collection="ids" item="user_id" open="AND (" close=")" separator="or"><!-- 每个遍历需要拼接的串 -->id=#{user_id}</foreach>
</if>

1.1.4    测试代码

1.1.5    另外一个sql的实现:

    <!-- 实现  “ and id IN(1,10,16)”拼接 --><!-- <foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=",">每个遍历需要拼接的串#{user_id}</foreach> -->

8~mybatis的动态sql相关推荐

  1. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有:   if choose(when,otherwis ...

  2. Mybatis解析动态sql原理分析

    前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...

  3. MyBatis中动态sql实现时间范围比较的查询

    场景 前端传递两个时间参数,开始时间和结束时间,然后从数据库中筛选出某个时间属性在此范围的数据. Mybatis的动态sql的写法. 注: 博客: https://blog.csdn.net/bada ...

  4. MyBatis中动态sql的模糊搜索、foreach实现In集合的用法

    场景 在使用MyBatis的动态sql时,常见的是传递一个ID的数组,查询记录的 ID在这个数组中的记录和模糊搜索这两种场景. 注: 博客: https://blog.csdn.net/badao_l ...

  5. mybatis的动态sql的一些记录

    动态sql的作用:传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个空格,都会导致错误.Mybatis的动态SQL功能正是为了解决这种问题, 其通过 if ...

  6. MyBatis(三)——动态SQL

    文章目录 1. 简介 2. 搭建环境 2.1 在MySQL中创建blog表 2.2 编写实体类 2.3 编写实体类对应Mapper接口 2.4 编写Mapper接口对应的Mapper.xml文件 2. ...

  7. 9、mybatis中动态sql的使用

    对于初学者,如何进行mybatis的学习呢?我总结了几点,会慢慢的更新出来.首先大家需要了解mybatis是什么.用mybatis来做什么.为什么要用mybatis.有什么优缺点:当知道了为什么的时候 ...

  8. 利用MyBatis的动态SQL特性抽象统一SQL查询接口

    1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...

  9. java day55【 Mybatis 连接池与事务深入 、 Mybatis 的动态 SQL 语句、 Mybatis 多表查询之一对多 、 Mybatis 多表查询之多对多】...

    第1章 Mybatis 连接池与事务深入 1.1 Mybatis 的连接池技术 1.1.1 Mybatis 连接池的分类 1.1.2 Mybatis 中数据源的配置 1.1.3 Mybatis 中 D ...

  10. 2022/5/1 Mybatis框架动态SQL

    目录 1丶动态 SQL 2丶if标签 3丶choose.when.otherwise 4丶trim.where.set 5丶foreach 6丶script 7丶bind 8丶多数据库支持 9丶动态 ...

最新文章

  1. 11.使用ForwardAction实现页面屏蔽。
  2. 使用scp、ssh等不用输入密码
  3. [MySQL]--gt;查询5天之内过生日的同事中的闰年2月29日问题的解决过程
  4. linux是只读添加 来覆盖,Linux之指令 重定向 文件覆盖和文件追加
  5. 一文教你轻松搞定ANR异常捕获与分析方法
  6. java paint调用,求教 如何调用这个paint
  7. 二十五、MongoDB 索引 和 explain 的使用
  8. 2019 vs 安装odt_河南2019二级造价师考试教材出版信息,免费送考试大纲
  9. Spring 框架学习 有用
  10. 总结30个CSS3选择器(转载)
  11. 博客园博客转至个人网站博客声明
  12. PDF417数据容量
  13. 基于SSM实现的超市管理系统
  14. androidtabhost缓存_Android TabHost用法详解
  15. vue 实现简约留言板
  16. 自适应布局-使用css3函数clac()
  17. linux安装键盘鼠标失灵,在archlinux安装界面这卡住了,鼠标键盘失灵
  18. 计算机硬盘无法查找,电脑不认硬盘的原因,怎么解决电脑读不到硬盘?
  19. 论文笔记 PCL: Proposal Cluster Learning for Weakly Supervised Object Detection - TPAMI 2018
  20. C++ for循环嵌套 实现 打印10行10列星图

热门文章

  1. C. Mortal Kombat Tower(cf)dp
  2. 一文探究数据仓库体系(2.7万字建议收藏)
  3. CSS媒体查询(@media)全面解析
  4. Jmeter常用插件下载
  5. Nodejs获取微信签名并使用JSSDK
  6. 最好最实用的二次开发教程(dedeCMS,ecshop为例)
  7. 查看链接文件的最终目标的多种方法
  8. 笔记本开热点手机一直显示正在获取ip
  9. 马斯克狠狠地给马云上了一课 | 对话全文实录
  10. selenium模拟刷百度流量源码