Python 官方文档:入门教程 => 点击学习
目录一、模糊查询的几种实现方式1.concat函数和#{}拼接的方式2.%和${}拼接的方式3.concat函数和${}拼接的方式4.||和#{}拼接的方式5.建议使用的方式二、用m
student_name like concat('%',#{studentName},'%')
student_name like '%${studentName}%'
student_name like concat('%','${studentName}','%')
student_name like '%'||#{studentName}||'%'
分析: (1)${}:表示拼接sql串,将接收到参数的内容不加任何修饰拼接在sql中,可能引发sql注入。 (2)#{ }是预编译处理,MyBatis在处理#{ }时,它会将sql中的#{ }替换为?,然后调用PreparedStatement的set方法来赋值,传入字符串后,会在值两边加上单引号,使用占位符的方式提高效率,可以防止sql注入。因此最好使用#{ }方式。 (3)concat函数最好不要使用,最好使用||,因为在oracle中,concat()只能对两个字符串进行拼接(字符串多的话只能嵌套使用),而||可以对字符串无限拼接。
建议使用第四种哦,示例:
<if test="studentName != null and studentName !=''">
and student_name like '%'||#{studentName}||'%'
</if>
在mapper文件里面关于查询if的条件基本都要写出两种空值,否则就会在查询时把其他不需要的数据也查出来。
错误示范:
<if test="studentName != null">
and student_name = #{studentName}
</if>
正确示范:
<if test="studentName != null and studentName !=''">
and student_name = #{studentName}
</if>
这个是在写日期查询条件时,出现了日期只能修改,但不能为空的问题:
错误示范:
<if test="effDate != null and effDate !=''">
and eff_date = #{effDate}
</if>
<if test="expDate != null and expDate !=''">
and exp_date = #{expDate}
</if>
正确示范:去掉外面的if判空条件
and eff_date = #{effDate}
and exp_date = #{expDate}
查询时最好按id倒序,在修改之后不影响原有顺序
比如:order by student_id desc
到此这篇关于关于MyBatis模糊查询的几种实现方式的文章就介绍到这了,更多相关MyBatis模糊查询内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 关于MyBatis模糊查询的几种实现方式
本文链接: https://www.lsjlt.com/news/212010.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0