写点什么

SpringBoot 整合 MybatisPlus 实战动态 SQL,java 编程入门经典

作者:Java高工P7
  • 2021 年 11 月 10 日
  • 本文字数:2301 字

    阅读完需:约 8 分钟

  • 测试


@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());


}


}


  • 打印结果



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


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


![Spring


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


Boot 整合 MybatisPlus 实战动态 SQL](https://static001.geekbang.org/infoq/8a/8a1355d7ccaf43624d6352ee4a4412d0.jpeg)


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

  • 打印结果



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


where 标签


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


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

  • 结果



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


foreach 标签


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


  • mapperselect from test where id in #{id}


代码


@RequestMapping(value = "/dongtaiSql3")


@ResponseBody


public 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 也可以用来批量插入数据,比如:

  • 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();


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 达到支持两个数据库的作用

  • mapperselect from test where 1=1 and username like #{nameLike} and ip=#{ip}

  • 代码


@RequestMapping(value = "/dongtaiSql5") @ResponseBody public void dongtaiSql5() { Test example = new Test(); example.setUsername("周"); example.setIp("cium"); example.setId(27); int selectByTestSelective = testMapper.updateTestSetTag(example); System.out.println(selectByTestSelective);


}


  • 结果



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


set 标签


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


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


  • mapperUPDATE Products username = #{username}, ip = #{ip}, id = #{id}

  • 代码


@RequestMapping(value = "/dongtaiSql5") @ResponseBody public void dongtaiSql5() { Test example = new Test(); example.setUsername("周"); example.setIp("cium"); example.setId(27); int selectByTestSelective = testMapper.updateTestSetTag(example); System.out.println(selectByTestSelective);}

用户头像

Java高工P7

关注

还未添加个人签名 2021.11.08 加入

还未添加个人简介

评论

发布
暂无评论
SpringBoot整合MybatisPlus实战动态SQL,java编程入门经典