目录

  • MyBatis的动态SQL是最令人喜欢的功能
  • if 标签
  • include标签
  • choose标签 ,配合when ,otherwise 标签使用
  • where 标签
  • foreach 标签
  • bind 标签
  • set 标签
  • trim 标签
  • trim标签各参数的说明
  • 然而我在配置的时候却遇到了更坑的问题,迟迟得不到解决......欢迎有兴趣的朋友一起交流下解决最后这个问题。

MyBatis的动态SQL是最令人喜欢的功能

在了解 动态SQL之前,你首先得知道一个表达式 OGNL,这个是基础!

  • 面试常问问题 : Mybatis 中$与#的区别?
  • #是将传入的值当做字符串的形式,select id,name,age from test where id =#{id},
    当把id值传入到后台的时候,就相当于 select id,name,age from test where id =‘1’.

  • "$"是将传入的数据直接显示生成sql语句,select id,name,age from test where id = ${id},
    当把id值1,传入到后台的时候,就相当于 select id,name,age from test where id = 1.

  • 使用#可以很大程度上防止sql注入。(语句的拼接)

if 标签

  • mapper
<select id="selectByTestSelective" resultMap="BaseResultMap" parameterType="com.sch.app.mybatis.entity.Test">selectfrom testwhere 1=1<if test="username != null and username !=''">and username like concat('%', #{username}, '%')</if><if test="ip != null">and ip=#{ip}</if></select>
  • 在mapper 接口中映射这个方法
  List<Test> selectByTestSelective(Test example);

下面每个标签都会有对应的方法,但下文没有一一写出,现参考如下

List<Test> selectByExample(TestExample example);
List<Test> selectByTestSelective(Test example);
List<Test> selectByIdOrUserName(Test example);
List<Test> selectByTestSelectiveWhereTag(Test example);
List<Test> selectByTestIdList(List<Integer> ids);
int insertList(List<Test> students);
int updateTestSetTag(Test example);
int selectSelectiveTrim(Test example);
  • 测试
 @RequestMapping(value = "/dongtaiSql")@ResponseBodypublic void dongtaiSql() {Test example = new Test();example.setUsername("周");List<Test> selectByTestSelective = testMapper.selectByTestSelective(example);for (Test test : selectByTestSelective) {System.out.println(test.getUsername());}}
  • 打印结果
  • 也就是说,你传什么值 它会根据你传的值来拼接sql,不传值则不拼接,这种相对来说比较简单,易于理解。

【注意】 下文所有的请求都是通过postman发出的。

include标签

  • 一个非常好用的辅助性标签,用于放一些公共的返回结果集,方便其他的查询方法使用,比如在mapper中使用方式如下:
<sql id="Base_Column_List" >username, lastloginTime
</sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >select <include refid="Base_Column_List" />from testwhere id = #{id,jdbcType=BIGINT}</select>

choose标签 ,配合when ,otherwise 标签使用

choose when otherwise 标签可以帮我们实现 if else 的逻辑。一个 choose 标签至少有一个 when,
最多一个otherwise。

  • mapper
 <select id="selectByIdOrUserName" resultMap="BaseResultMap" parameterType="com.sch.app.mybatis.entity.Test">select<include refid="Base_Column_List" />from testwhere 1=1<choose><when test="id != null">and id=#{id}</when><when test="username != null and username != ''">and username=#{username}</when><otherwise>and 1=2</otherwise></choose></select>
  • 打印结果

找不到 周 ,因为我只有周杰伦或者周杰 。 这个choose和 if 的功能有点类似,但是和if 不同的是choose 有点你有什么我就根据你给的查,而if 则是你传了所有条件,我逐个判断你的条件然后给你查。if 更多适用于表单查询的时候用。而choose 更多的时候。。。其实这两个达到的目的是一样的,我更喜欢用choose.

where 标签

  • mapper
 <select id="selectByTestSelectiveWhereTag" resultMap="BaseResultMap" parameterType="com.sch.app.mybatis.entity.Test">select<include refid="Base_Column_List" />from test<where><if test="username != null and username !=''">and username like concat('%', #{username}, '%')</if><if test="ip != null">and ip=#{ip}</if></where></select>
  • 代码
 @RequestMapping(value = "/dongtaiSql2")@ResponseBodypublic void dongtaiSql2() {Test example = new Test();List<Test> selectByTestSelective = testMapper.selectByTestSelectiveWhereTag(example);for (Test test : selectByTestSelective) {System.out.println(test.getUsername());}}
  • 结果

  • 我什么条件也没传,他在where中找不到匹配的条件就查找了全部给了我,这种其实和上面choose 中的最后那个条件有异曲同工之处,上变改成1=1 一样的效果。

foreach 标签

  • mapper
 <select id="selectByTestIdList" resultMap="BaseResultMap">select<include refid="Base_Column_List" />from testwhere id in<foreach collection="list" item="id" open="(" close=")" separator="," index="i">#{id}</foreach></select>
  • 代码
 @RequestMapping(value = "/dongtaiSql3")@ResponseBodypublic void dongtaiSql3() {ArrayList<Integer> arrayList = new ArrayList<Integer>();arrayList.add(6);arrayList.add(5);List<Test> selectByTestSelective = testMapper.selectByTestIdList(arrayList);for (Test test : selectByTestSelective) {System.out.println(test.getUsername());}}
  • 结果

  • 这个标签太好用了,foreach 也可以用来批量插入数据,比如:

  • mapper

<insert id="insertList">insert into test(username, gender, ip)values<foreach collection="list" item="test" separator=",">(#{test.username}, #{test.gender},#{test.ip})</foreach></insert>
  • 代码
@RequestMapping(value = "/dongtaiSql4")@ResponseBodypublic void dongtaiSql4() {Test example = new Test();example.setUsername("刘德华");example.setGender(1);example.setIp("123232113111");Test example2 = new Test();example2.setUsername("郭富城");example2.setGender(1);example2.setIp("123232113122");Test example3 = new Test();example3.setUsername("邱淑贞");example3.setGender(0);example3.setIp("123232113333");ArrayList<Test> arrayList = new ArrayList<Test>();arrayList.add(example);arrayList.add(example2);arrayList.add(example3);int selectByTestSelective = testMapper.insertList(arrayList);if (selectByTestSelective == 1) {System.out.println("批量插入:"+arrayList.size()+"条数据");}}
  • 结果

  • 妈的 这里的mapper 每次修改都要重新启动,很是麻烦。注意这里 #{test.username}, #{test.gender},#{test.ip} 最后不要有逗号,否则会报一个sql语法错误,原因是多了,。还有就是这里如果传的值是list等非实体类的参数的时候,是不用声明parameterType 的。
  • foreach 的变量说明
collection: 必填, 集合/数组/Map的名称.
item: 变量名。即从迭代的对象中取出的每一个值
index: 索引的属性名。当迭代的对象为 Map 时, 该值为 Map 中的 Key.
open: 循环开头的字符串
close: 循环结束的字符串
separator: 每次循环的分隔符

bind 标签

  • 使用 bind 来让该 SQL 达到支持两个数据库的作用
  • mapper
 <select id="selectByTestSelective" resultMap="BaseResultMap" parameterType="com.sch.app.mybatis.entity.Test">select<include refid="Base_Column_List" />from testwhere 1=1<if test="username != null and username !=''"><bind name="nameLike" value="'%'+username+'%'"/> and username like #{nameLike}</if><if test="ip != null">and ip=#{ip}</if></select>
  • 代码
@RequestMapping(value = "/dongtaiSql")
@ResponseBody
public void dongtaiSql() {Test example = new Test();example.setUsername("周");List<Test> selectByTestSelective = testMapper.selectByTestSelective(example);for (Test test : selectByTestSelective) {System.out.println(test.getUsername());}
}
  • 结果

    发现依然可以。 说明这个bind 就是绑定一些变量的 ,nameLike 就代表了username 的模糊搜索,就是如果别的地方用得到它的模糊搜索,拿来用即可。用法是 like 后面直接加上 #{nameLike }。

set 标签

这个标签常用于做修改语句,比如

  • mapper
<update id="updateTestSetTag" parameterType="com.sch.app.mybatis.entity.Test">UPDATEProducts<set><if test="username != null and username != ''">username = #{username},</if><if test="ip != null and ip != ''">ip = #{ip},</if></set><where>id = #{id}</where>
</update>
  • 代码
 @RequestMapping(value = "/dongtaiSql5")@ResponseBodypublic void dongtaiSql5() {Test example = new Test();example.setUsername("周");example.setIp("cium");example.setId(27);int selectByTestSelective = testMapper.updateTestSetTag(example);System.out.println(selectByTestSelective);}
  • 结果
  • 这个set 说白了就是update语句的 set 时候的一个灵活操作。

trim 标签

  • mapper
 <insert id="selectSelectiveTrim" parameterType="com.sch.app.mybatis.entity.Test" >select * from test<trim prefix="WHERE" prefixOverrides="AND |OR"><if test="username != null and username.length()>0"> AND username=#{username}</if><if test="ip != null and ip.length()>0"> AND ip=#{ip}</if></trim></insert>
  • 【注意】
  • 这里有很多坑,首先mybatis-plus 中不是 prefixoverride 而是prefixOverrides
  • 然后"AND |OR" 必须有空格,原因如下图
  • 如果 ip 不是字符串就不能用length() 方法

trim标签各参数的说明

prefix:在trim标签内sql语句加上前缀。
suffix:在trim标签内sql语句加上后缀。
prefixOverrides:指定去除多余的前缀内容
suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。

然而我在配置的时候却遇到了更坑的问题,迟迟得不到解决…欢迎有兴趣的朋友一起交流下解决最后这个问题。

Springboot框架整合Mybatis-plus实战动态SQL以及常见的Mybatis面试题相关推荐

  1. Java - MyBatis中的动态SQL是什么意思?

    分享一个大牛的人工智能教程.零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 对于一些复杂的查询,我们可能会指定多个查询条件,但是 ...

  2. Mybatis入门之动态sql

    Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...

  3. Mybatis映射文件动态SQL语句-01

    因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...

  4. mybatis注解开发动态sql

    mybatis注解开发动态sql 本篇来讲一下如何使用mybatis注解模式中的动态sql 先来讲一下什么是动态sql 在我们实际开发的时候可能会出现很多方法需要一条很相似的sql语句来进行增删改查, ...

  5. Mybatis 注解开发 + 动态SQL

    Hello 大家好我是橙子同学,今天分享注解Mybatis注解开发+动态sql 目录 每文一铺垫(今天有小插曲哦) 注解开发 添加 @Insert 删除 @Delete 查询 @Select 修改 @ ...

  6. 大道PHP+LAMP+ZEND+开源框架整合开发与实战|pdf电子书(100m)

    内容简介 <大道PHP:LAMP+Zend+开源框架整合开发与实战>以PHP应用程序开发为主题,对实践中必不可少的各项重要技术进行了全面介绍与系统讲解,并对多个PHP开源框架的应用进行了深 ...

  7. springboot 和 mybatis整合:参数查询和动态sql

    springboot 和 mybatis整合: mapper定义的是数据库的操作方法: @Mapper public interface UserMapper {} 单参数的处理: @Select(& ...

  8. SpringBoot集成Groovy、Mybatis注解 实现动态SQL,帮你摆脱繁琐的XML配置

    SpringBoot的超简洁配置,为我们省去了宝贵的配置时间. Mybatis3在这方面也提供了很好的支持,通过注解让我们摆脱了繁琐的mapper xml,写DAO层的时候再也不用在java接口和xm ...

  9. MyBatisPlus自动生成代码springboot+mybatis+mysql 以及动态sql生成方法(测试可用版)

    用了一段时间的springboot,想着百度一下自动生成代码的方式,包括后面如何生成动态sql方法的方式. 摸索了几天,整理一下: ** 1 自动生成代码方式:com.baomidou.mybatis ...

最新文章

  1. 五大风口产业全景手绘图(新能源汽车、人工智能等)
  2. 实现多个标签页之间通信的几种方法
  3. 蓝桥杯 1454 蚂蚁感冒 (找规律)
  4. 怎样用Jquery实现拖拽层,并实现网站自定义化模块功能?
  5. VS中lib和dll
  6. Keil5消除未调用警告
  7. SAP License:资产年结
  8. 【图像融合】基于小波变换的图像融合
  9. 2019级C语言大作业 - 十步万度
  10. 发现孩子做作业用计算机,儿童不宜长期使用计算器做作业
  11. 设计模式原则之三:接口隔离原则
  12. java—将数据库读取的list转tree
  13. Android Studio的build.gradle里面的各种版本信息
  14. android 语音读短信,读短信来电报姓名2021下载-读短信来电报姓名app下载10.50 安卓版-西西软件下载...
  15. AI安全 - 华为白皮书《AI Security White Paper》
  16. 矢量绘图软件 android,sai绘图软件官方
  17. 分享三大外汇日内交易策略
  18. WinUsb_ReadPipe和WinUsb_WritePipe函数功能理解
  19. C++语言涉猎笔记(二)
  20. 任正非:华为从未想干翻苹果 称霸死无葬身之地

热门文章

  1. mysql所选路径已经存在_mysql安装常见问题解决办法
  2. /bin/bash^M: 坏的解释器: 没有那个文件或目录
  3. 【附源码】计算机毕业设计SSM社区生鲜配送系统
  4. [C#] 使用 NAudio 实现音频可视化
  5. 163邮箱导出eml格式文件
  6. electron调节windows系统音量解决方案
  7. 必备工具:使用Pentaho进行数据迁移
  8. 使用xpath爬取学院新闻
  9. 如果把14亿中国人都拉到一个微信群。。。
  10. 【项目】多部门数据对接