SpringBoot整合MybatisPlus实战动态SQL

[](

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

======================================================================================

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

SpringBoot整合MybatisPlus实战动态SQL

  • 面试常问问题 : 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 = " 是 将 传入 的 数 据 直 接 显 示 生 成 s q l 语 句 , s e l e c t i d , n a m e , a g e f r o m t e s t w h e re i d = {id},当把id值1,传入到后台的时候,就相当于 select id,name,age from test where id = 1.

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

[](

)if 标签

=====================================================================

  • mapperselect from test where 1=1 and username like concat(’%’, #{username}, ‘%’) and ip=#{ip}

  • 在mapper 接口中映射这个方法List selectByTestSelective(Test example);

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

List selectByExample(TestExample example);

List selectByTestSelective(Test example);

List selectByIdOrUserName(Test example);

List selectByTestSelectiveWhereTag(Test example);

List selectByTestIdList(List ids);

int insertList(List students);

int updateTestSetTag(Test example);

int selectSelectiveTrim(Test example);

  • 测试

@RequestMapping(value = “/dongtaiSql”)

@ResponseBody

【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

public void dongtaiSql() {

Test example = new Test();

example.setUsername(“周”);

List selectByTestSelective = testMapper.selectByTestSelective(example);

for (Test test : selectByTestSelective) {

System.out.println(test.getUsername());

}

}

  • 打印结果

image.png

  • 也就是说,你传什么值 它会根据你传的值来拼接sql,不传值则不拼接,这种相对来说比较简单,易于理解。

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

SpringBoot整合MybatisPlus实战动态SQL

[](

)include标签

=========================================================================

  • 一个非常好用的辅助性标签,用于放一些公共的返回结果集,方便其他的查询方法使用,比如在mapper中使用方式如下:username, lastloginTime select from test where id = #{id,jdbcType=BIGINT}

[](

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

================================================================================================

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

最多一个otherwise。

  • mapperselect from test where 1=1 and id=#{id} and username=#{username} and 1=2

  • 打印结果

SpringBoot整合MybatisPlus实战动态SQL

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

[](

)where 标签

========================================================================

  • mapperselect from test and username like concat(’%’, #{username}, ‘%’) and ip=#{ip}

  • 结果

SpringBoot整合MybatisPlus实战动态SQL

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

[](

)foreach 标签

==========================================================================

  • mapperselect from test where id in #{id}

代码

@RequestMapping(value = “/dongtaiSql3”)

@ResponseBody

public void dongtaiSql3() {

ArrayList arrayList = new ArrayList();

arrayList.add(6);

arrayList.add(5);

List selectByTestSelective = testMapper.selectByTestIdList(arrayList);

for (Test test : selectByTestSelective) {

System.out.println(test.getUsername());

}

}

  • 结果

SpringBoot整合MybatisPlus实战动态SQL

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

  • mapperinsert into test(username, gender, ip) values ( #{test.username}, #{test.gender},#{test.ip} )

  • 代码

Test example2 = new Test();

example2.setUsername(“郭富城”);

example2.setGender(1);

example2.setIp(“123232113122”);

Test example3 = new Test();


版权声明:本文为m0_63175850原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。