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