🚀个人主页:Ali,S
📆 最近更新:2022年7月2日
⛽ Java框架学习系列:Mybatis框架
⛳ Java基础学习系列:面向对象飞机大战
🏆 通信仿真学习系列:【硬件】【通信】【MATLAB】
🍄 个人简介:通信工程本硕🌈、Java程序员🚴。目前只会CURD😂
💌 点赞 👍 收藏 💗留言 💬 都是我最大的动力💯
文章目录
前言
Mybatis的强大特性之一就是使用了动态SQL,使用JDBC或者其他框架时,需要根据不同的查询条件拼接SQL语句进行多字段的数据操作,操作十分繁琐,为解决这种复杂的SQL语句的编写,Mybatis提供了强大的动态SQL进行完善系列操作。
一、OGNL表达式
关系 | OGNL |
---|---|
与关系 |
m1 and m2 |
或关系 |
m1 or m2 |
相等关系 |
m1 == m2或者m1 eq m2 |
不等关系 |
m1 != m2或者m2 neq m2 |
大于关系 |
m1>m2或者m1 gt m2 |
小于关系 |
m1<m2或者m1 lt m2 |
大于等于关系 |
m1>=m2或者m1 gte m2 |
小于等于关系 |
m1<=m2或者m1 lte m2 |
取反 |
!m1或者not m1 |
二、动态SQL
1.动态标签if
if标签是最常用的标签之一,在查询、删除、更新操作时经常被被使用到,必须是和test
属性联合使用。
<select id="selectDynamic1" parameterType="com.entity.Student"
resultType="com.entity.Student">
select <include refid="BASE_COlUMN"/> from Student
<where>
<if test="name!=null and name!=''">
and name like concat('%',#{
name},'%')
</if>
<if test="address!=null and address!=''">
and address like concat('%',#{
address},'%')
</if>
</where>
</select>
在实体类映射文件中,写上述查询操作,使用动态SQL语句中的if标签,配合test属性进行联合操作,判断name、address
属性值非空并不为‘’,使用like
关键字进行模糊匹配。需要注意的是在select语句中的条件可以改为下面的代码,并且不使用where
关键字而是使用<where>
标签。
<select id="selectDynamic1" parameterType="com.entity.Student"
resultType="com.entity.Student">
select <include refid