🧭MyBatis学习
🎉 内容回顾
Java MyBatis的介绍及其执行原理
Java MyBatis配置详解
Java Mybatis中使用Junit进行测试_程序员必备
Java MyBatis的使用
📢今天我们进行 Java MyBatis动态SQL 的学习,感谢你的阅读,内容若有不当之处,希望大家多多指正,一起进步!!!
♨️如果觉得博主文章还不错,可以👍三连支持⭐一下哦😀
🍀Java MyBatis动态SQL
☘️一、动态SQL
mybatis中特征之一支持动态SQL,以标签的形式动态编写SQL,根据业务逻辑来动态的进行SQL拼接功能。mybatis提供的9种动态SQL标签
trim
、if
、where
、set
、foreach
、choose
、when
、otherwise
、bind
执行原理:使用OGNL从SQL参数对象中计算表达式的值,根据表达式的值动态拼接SQL,以此完成动态SQL的功能。
动态SQL通常放在where子句的一部分中,常用于查询、 插入、 更新等操作。
☘️二、标签使用
🌵if标签
场景: 根据姓名、性别、姓名和性别的查询相应的数据
StudentMapper
List<Student> selectStudentBySnameOrSsex(Student student);
StudentMapper.xml
因为考虑到每个if体中都含有and,当为真时会增加一个and,所以在SQL语句where后用 1 = 1 巧妙的避开了这种尴尬。
<select id="selectStudentBySnameOrSsex" parameterType="student" resultMap="studentMap">
select * from student where 1 = 1
<if test="name != null">
and Sname = #{name}
</if>
<if test="Ssex != null">
and Ssex = #{Ssex}
</if>
</select>
if表达式中判断参数是否传递,test属性:该属性必填,为true或者false,test使用OGNL表达式处理,返回为true则进入if标签中的SQL,返回为false则不会进入标签中。
测试
🌵where标签
where标签:如果该标签包含的元素有返回值,就插入一个where,如果where后面的字符串是以and或者or开头的,就将他们剔除。
<select id="selectStudentBySnameOrSsex" parameterType="student" resultMap="studentMap">
select * from student
<where>
<if test="name != null">
and Sname = #{name}
</if>
<if test="Ssex != null">
and Ssex = #{Ssex}
</if>
</where>
</select>
执行结果:
select * from student WHERE Sname = ? and Ssex = ? //传递两个参数
select * from student WHERE Sname = ? //只传递name
select * from student WHERE Ssex = ? //只传递sex
select * from student //不传递参数
where表达式一般和if表达式一块使用,如果条件一个都不满足,则不拼接where条件,如果有一个或者多个表达式成立,where会直接拼接在SQL上,并且紧随where的表达式的and/or会忽略掉。
🌵trim便签
MyBatis提供trim标签一般适用于取出SQL语句中多余的and关键字,逗号,或者给SQL语句拼接”where“、”Set”以及values等,可用于选择性插入,更新,删除或者条件查询等操作,where和set标签都可以通过trim标签来实现
trim标签存在属性
属性 | 描述 |
---|---|
prefix | 给sql语句拼接的前缀 |
suffix | 给sql语句拼接的后缀 |
prefixOverrides | 去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为“AND”,当sql语句的开头为“AND”,trim标签会去除“AND” |
suffixOverrides | 去除sql语句后面的关键字或者字符, 该关键字或者字符由suffixOverrides属性指定 |
<select id="selectStudentBySnameOrSsex" parameterType="student" resultMap="studentMap">
select * from student
<trim prefix="where" prefixOverrides="and">
<if test="name != null">
and Sname = #{name}
</if>
<if test="Ssex != null">
and Ssex = #{Ssex}
</if>
</trim>
</select>
🌵foreach标签
适用于批量操作
foreach表达式
collection属性
: 必填,指定参数入参类型 列表(list) 、数组(array)、HashMap(map)
item属性
:起名字,给集合中单个元素起名称
open属性
:开始字符串
close属性
:结束字符串
separator属性
:分割符
index属性
:索引的属性名,在集合数组下值为当前的索引值
场景:通过一批id来查询用户 select * from student where SID in(1,2,3,4,5);
StudentMapper
List<Student> batchQureyStudent(List<Integer> sid);
StudentMapper.xml
<select id="batchQureyStudent" resultType="int" resultMap="studentMap">
select * from student where SID in
<foreach collection="list" item="sid" open="(" close=")" separator=",">
#{sid}
</foreach>
</select>
🌵bind标签
bind标签适合模糊查询,bind表达式中参数必须是具有getter方法,底层是通过OGNL表达式来进行解析,该表达式通过属性的getter进行获取值。
场景:查询student表中名字带有L的学生信息。select * from student where Sname like “%L%”;
StudentMapper
<select id="selectStudentByName" parameterType="string" resultMap="studentMap">
<bind name="n" value="'%'+name+'%'"/>
select * from student where Sname like #{n}
</select>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/95493.html