SpringBoot整合Mybatis-Plus 实战之动态SQL,Mybatis最拿得出手的功能之一

本文介绍了SpringBoot整合Mybatis-Plus时如何使用动态SQL,包括OGNL基础、动态SQL的各种标签如if、include、choose、where、foreach、bind、set和trim的使用,并通过实例展示了它们在实际操作中的应用。
摘要由CSDN通过智能技术生成

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语句,selectid,name,agefromtestwhereid="是将传入的数据直接显示生成sql语句,select id,name,age from test where id = "是将传入的数据直接显示生成sql语句,selectid,name,agefromtestwhereid={id},
    当把id值1,传入到后台的时候,就相当于 select id,name,age from test where id = 1.

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

if 标签

  • mapper

    select from test where 1=1 and username like concat('%', #{username}, '%') and ip=#{ip}
  • 在mapper 接口中映射这个方法

    List 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")
    @ResponseBody
    public void dongtaiSql() {
    	Test example = new Test();
    	example.setUs
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值