Mybaits框架动态SQL的常用标签
MyBatis框架主要通过标签的配合使用实现SQL语句的动态拼接、前后缀格式化处理、复杂参数处理等功能。表格如下:
标签 | 说明 |
---|---|
if | 条件判断,与Java中的计语句类似 |
where | 为SQL语句动态添加where关键字 |
choose | 条件判断,这是一个组合标签,需要when、otherwise标签搭配使用。可实现与Java中的switch语句类似的功能 |
foreach | 以遍历方式处理集合类型参数 |
set | 为SQL语句动态添加set关键字,实现动态实现数据更新功能 |
trim | 对SQL语句进行格式化处理,添加或移除前后绥 |
1.1 if 标签
if 标签是MyBatis框架动态SQL技术中重要且常用的标签之一,它所实现的功能与Java中的if语句基本相同,用法也很相似。MyBatis 的 if 标签用于实现条件判断,根据提供的条件是否成立来决定是否包含某段 SQL 语句。
if标签的语法如下:
<if test="条件判断,返回true或false">
SQL语句...
</if>
//判断供货商id不能为空,就执行里面的SQL语句
<select id="storageRecordList" resultType="storageRecord" >
select * from t_storage_record
<if test="storageRecordId != null">
and storageRecordId = #{storageRecordId}
</if>
</select>
1.2 where 标签
where标签的主要作用是对SQL语句中的where关键字进行简化处理,并可以智能地处理其内部and、or等关键字,避免多余字符带来的语句错误。如果第一个条件就满足,它会自动忽略 and 或 or。
where标签语法如下:
<where>
<if test="条件判断,返回true或false">
SQL语句...
</if>
</where>
<select id="storageRecordList" resultType="storageRecord" >
select * from t_storage_record
<where>
<if test="storageRecordId != null">
and storageRecordId = #{storageRecordId}
</if>
<if test="goodsName != null and goodsName !=''">
and goodsName like concat('%',#{goodsName},'%')
</if>
</where>
</select>
1.3 choose 标签
MyBatis 的 choose 标签类似于 Java 的 switch 语句,它允许你在多个选项中选择一个执行。choose 标签下可以有一个或多个 when 和 otherwise 子标签。when 标签的 test 属性包含了条件表达式,当条件为 true 时,该标签内的 SQL 会被执行。otherwise 标签则在所有 when 条件都不为 true 时执行。
choose标签语法如下:
<choose>
<when test="条件判断,返回true或false">
SQL语句...
</when>
<when test="条件判断,返回true或false">
SQL语句...
</when>
<otherwise>
SQL语句...
</otherwise>
</choose>
<select id="getStorageRecordList" resultType="storageRecord">
select * from t_storage_record
<where>
<choose>
<when test="storageRecordId != null">
and storageRecordId = #{storageRecordId}
</when>
<when test="goodsName != null and goodsName !=''">
and goodsName like concat('%',#{goodsName},'%')
</when>
<otherwise>
and YEAR(createdTime) = YEAR(#{createdTime})
</otherwise>
</choose>
</where>
</select>
1.4 foreach 标签
foreach标签在MyBatis中用于动态地遍历集合来构建SQL语句。它通常用在构建 in 条件查询、批量操作等场景。假设我们要根据入库记录ID查询入库记录信息:
<select id="getStorageRecordList" resultType="com.bdqn.pojo.StorageRecord">
select * from t_storage_record where id in
<foreach collection="list" item="storageRecordId" open="(" separator=" ," close=" )">
#{storageRecordId}
</foreach>
</select>
在这个例子中,collection属性指定了要遍历的集合,item属性定义了集合中每个元素的别名,open、separator和close属性分别定义了IN子句的开始、分隔符和结束字符。
1.5 set 标签
MyBatis 的 set 标签主要用于动态更新语句,可以自动的处理 null 值和空集合。当你在更新操作时,如果某个字段没有被赋值,MyBatis 默认会将这个字段的值设置为 null。使用 set 标签可以避免这个问题,它会自动忽略那些 null 的字段。
set标签语法如下:
<set>
<if test="条件判断,返回true或false">
SQL语句...
</if>
</set>
<update id="updateSupplier">
update t_supplier
<set>
<if test=" supName != null "> supName=#{supName},</if>
<if test=" supDesc != null "> supDesc=#{supDesc},</if>
<if test=" supPhone != null "> supPhone=#{supPhone}</if>
</set>
where id=#{id}
</update>
1.6 trim 标签
trim 标签在 MyBatis 中用于动态地去除或添加某些字符串。它可以用在select, insert, update, delete等语句中。
trim标签有以下属性:
- prefix:前缀,可以用于添加前缀字符串
- prefixOverrides:去除首部的字符串
- suffix:后缀,可以用于添加后缀字符串
- suffixOverrides:去除尾部的字符串
一个简单的trim标签示例如下:
<select id="selectList" resultType="sysUser" >
select * from t_sys_user
<trim prefix="where" prefixOverrides="and|or">
<if test="roleId !=null">
and roleId =#{roleId}
</if>
<if test="realName !=null and realName!='' ">
and realName like concat('%',#{realName},'%')
</if>
</trim>
</select>