前言

我们知道在项目开发中之前使用数据库查询,都是基于jdbc,进行连接查询,然后是高级一点jdbcTemplate进行查询,但是我们发现还是不是很方便,有大量重复sql语句,与代码偶合,效率低下,于是就衍生出来ORM框架,如Mybatis,Hibernate,还有SpringBoot的,Spring Data JPA

条件查询

我们知道在mybatis mapper文件中条件查询符,如>=,<,之类是不能直接写的会报错的需要转移一下 如下图表

详细内容参考

常见的条件查询操作有

我们通过mybatis 提供的特有标签进行条件判断,达到动态拼接sql语句

if标签 where标签 choose when otherwise标签 foreach标签

快速入门

if标签

语法:

<if test="xxx != null and xxx != ''">

test中写判断条件 参数直接paramN或者别名 多个条件使用and或者or连接

只要条件成立就拼接在Sql语句中,都成立就全部都拼接

注意where子句中加上1=1来规避and的风险

如下例子:

<select id="selg" resultType="log">
select * from log where 1=1
<if test="param1!=null and param1!=''">
and outno=#{param1}
</if>
<if test="param2!=null and param2!=''">
and inno=#{param2}
</if>
</select>

where标签

对上面if标签条件判断where连接做了处理会自动的给Sql语句添加where关键字,并将第一个and去除

上面sql可以改造成如下:

<select id="selg" resultType="log">
select * from log
<where>
<if test="param1!=null and param1!=''">
and outno=#{param1}
</if>
<if test="param2!=null and param2!=''">
and inno=#{param2}
</if>
</where> </select>

choose when otherwise标签

类似于Java语法中的,case,switch语句判断

条件只要有一个成立,其他的就不会再判断了。如果没有成立的条件则默认执行otherwise中的内容

上面sql可以改造成如下:

<select id="selg" resultType="log">
select * from log
<where>
<choose>
<when test="param1!=null and param1!=''">
and outno=#{param1}
</when>
<when test="param2!=null and param2!=''">
and inno=#{param2}
</when>
<otherwise>
and 1=1
</otherwise>
</choose>
</where> </select>

foreach标签

语法:

  <foreach collection="idList" item="id" open="(" separator="," close=")">
</foreach>
  1. collection:要遍历的集合对象
  2. item:记录每次遍历的结果
  3. open:在结果的左边添加内容
  4. separator:结果和结果之间的内容
  5. close:在最后添加的内容

常用于in查询,和批量插入操作 如下案例:

<select id="selF" parameterType="list" resultType="account">
select * from account where ano in
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>

<insert id="insertBatch">
INSERT INTO t_user
(id, name, password)
VALUES
<foreach collection ="userList" item="user" separator =",">
(#{user.id}, #{user.name}, #{user.password})
</foreach >
</insert>

其他标签使用参考点击进入·

场景案例

  1. 当我们需要对多张表的关联数据进行复杂动态条件查询的时候,就需要用到 if标签进行判断 如下

根据用户手机号姓名年龄性别,等进行动态条件检索,这个时候我们需要动态通过调节去拼接sql 当条件满足sql语句加上对应条件差许

<select id="findUsersByUser" resultType="cn.soboys.kmall.sys.entity.User">
select tu.USER_ID,tu.USERNAME,tu.SSEX,td.DEPT_NAME,tu.MOBILE,tu.EMAIL,tu.STATUS,tu.CREATE_TIME,
td.DEPT_ID
from t_user tu left join t_dept td on tu.DEPT_ID = td.DEPT_ID
where tu.ADMIN_TYPE_ID &gt;= 0 AND tu.ADMIN_TYPE_ID &lt;= #{userParams.adminType}
<if test="userParams.roleId != null and userParams.roleId != ''">
and (select group_concat(ur.ROLE_ID)
from t_user u
right join t_user_role ur on ur.USER_ID = u.USER_ID,
t_role r
where r.ROLE_ID = ur.ROLE_ID
and u.USER_ID = tu.USER_ID and r.ROLE_ID=#{userParams.roleId})
</if> <if test="userParams.mobile != null and userParams.mobile != ''">
AND tu.MOBILE =#{userParams.mobile}
</if>
<if test="userParams.username != null and userParams.username != ''">
AND tu.USERNAME like CONCAT('%',#{userParams.username},'%')
</if>
<if test="userParams.ssex != null and userParams.ssex != ''">
AND tu.SSEX =#{userParams.ssex}
</if>
<if test="userParams.status != null and userParams.status != ''">
AND tu.STATUS =#{userParams.status}
</if>
<if test="userParams.deptId != null and userParams.deptId != ''">
AND td.DEPT_ID =#{userParams.deptId}
</if>
<if test="userParams.createTime != null and userParams.createTime != ''">
AND DATE_FORMAT(tu.CREATE_TIME,'%Y%m%d') BETWEEN substring_index(#{userParams.createTime},'#',1) and substring_index(#{userParams.createTime},'#',-1)
</if>
</select>

对应mapper对应的方法

<T> IPage<User> findUsersByUser(Page<T> page, @Param("userParams") SearchUserParams userParams);

对应参数实体对象

@Data
public class SearchUserParams {
private String username;
private String mobile;
private String status;
private String ssex;
private Long deptId;
private String createTime;
private long adminType;
private String roleId;
}

通过if标签去判断条件是否满足,满足就拼接对应sql

注意在上面我们提到的条件拼接第一个是where连接,而不是and应规避and风险保证sql语法正确 如下

<select id="findSearchCouponsPage" parameterType="cn.soboys.kmall.bean.web.params.SearchCouponParams" resultType="coupon">
select *
from coupon c
left join user_coupon uc on c.coupon_id = uc.coupon_id
WHERE 1 = 1
<if test="couponParams.userId != null and couponParams.userId != ''">
and uc.user_id =#{couponParams.userId}
</if>
<if test="couponParams.status != null and couponParams.status != ''">
and c.status =#{couponParams.status}
</if>
<if test="couponParams.couponId != null and couponParams.couponId != ''">
and c.coupon_id =#{couponParams.couponId}
</if>
<if test="couponParams.couponType != null and couponParams.couponType != ''">
and c.type =#{couponParams.couponType}
</if>
</select>

我们可以通过假定给他一个默认条件 WHERE 1 = 1来解决,也可以通过嵌套where标签来解决

最新文章

  1. Spark笔记:复杂RDD的API的理解(下)
  2. PHP中的date()函数
  3. CentOS6.5上安装MySQL5.6
  4. http的500,502,504错误
  5. 【9-20】vimtutor学习笔记
  6. Webhooks PHP
  7. Effective Java 27 Favor generic methods
  8. winform学习之----图片控件应用(上一张,下一张)
  9. 深刻理解Python中的元类metaclass(转)
  10. 【转】蓝牙4.0——Android BLE开发官方文档翻译
  11. 使用SQL脚本删除冗余的视图和表
  12. linux编程掌握常用命令
  13. 前端之BOM
  14. sqlserver可将字符转成数字再进行sum,如果varchar类型中存放的都是数字
  15. 关于Python的一些看法
  16. scikit_learn,NLTK导入分类器相关流程命令
  17. C++代码审查---审查孙晓宁马踏棋盘谜题程序
  18. Community宣言
  19. phpwind v9存在命令执行漏洞(登陆后台)
  20. swap(十六)

热门文章

  1. Sqli-Labs less32-37
  2. Docker++:从 0 到 1 学习Docker(笔记)
  3. 高效编程:在IntelliJ IDEA中使用VIM
  4. 从net到java:MyBatis快速入门
  5. RabbitMQ从零到集群高可用(.NetCore5.0) - RabbitMQ简介和六种工作模式详解
  6. 关于c#:如何在不同的命名空间中处理相同的类名?
  7. WPF 附件路由事件
  8. springCloud实战
  9. java基础路线与详细知识点
  10. 输入URL后浏览器的过程