MyBatis 动态SQL
MyBatis通过标签的配合使用,可实现如下功能
语句的动态拼接、前后缀格式处理、复杂参数处理
动态SQL的常用标签
1、if
动态SQL技术中最常用的标签之一,类似于Java中的if语句
以下是语法:
<if test = "条件判断,返回true或false" >
SQL语句
</if>
以下是示例:
<!-- 根据Id或者用户名称查询用户信息 -->
<select id="getUserByIdAndName" resultType="com.cskt.mybatis2.pojo.SysUser">
select * from t_sysUser where
<if test="realName!= null and realName!=''">
and u.realName like concat('%',#{realName},'%')
</if>
<if test="roleId!=null and roleId!=0">
and u.roleId=#{roleId}
</if>
</select>
2、where
简化SQL语句中where子句处理,智能处理and、or等关键字
以下是语法:
<where>
<if test="条件判断">
SQL语句
</if>
…
</where>
以下是示例:
<!-- 根据供货商编码、供货商名称模糊查询供货商表 -->
<select id="getSupperByIdAndSupName" resultType="com.cskt.mybatis2.pojo.Supplier">
select id,supCode,supName,supContact,supPhone,supFax,createdTime from t_supplier
<where>
<if test="supCode!=null and supCode!=''">
supCode like concat('%',#{supCode},'%')
</if>
<if test="supName!=null and supName!=''">
and supName like concat('%',#{supName},'%')
</if>
</where>
</select>
3、choose
是一个组合标签,通常与when、otherwise标签配合使用,类似于Java中switch语句
以下是语法:
<choose>
<when test="条件判断,返回true或false">
</when>
<when test="条件判断,返回true或false">
</when>
...
<otherwise>
</otherwise>
</choose>
以下是示例:
<!-- 供货商表 -->
<select id="getSupplierList" resultType="com.cskt.mybatis2.pojo.Supplier">
select id,supCode,supName,supContact,supPhone,supFax,createdTime from t_supplier
<where>
<choose>
<when test="supName!=null and supName!=''">
and supName like concat('%',#{supName},'%')
</when>
<when test="supCode!=null and supCode!=''">
and supCode like concat('%',#{supCode},'%')
</when>
<when test="supContact!=null and supContact!=''">
and supContact like concat('%',#{supContact},'%')
</when>
<when test="supName==null and supCode==null and supContact==null">
and createdTime =#{createdTime}
</when>
</choose>
</where>
</select>
4、foreach
迭代一个集合,通常用于in条件
以下是语法:
<foreach collection = "参数名称"
item = "元素别名"
open = "("
separator = ","
close = ")"
index = "当前元素位置下标" >
#{元素别名}
</foreach>
以下是示例(入参:数组、List、Map):
<!-- 查询多个供货商关联的入库单列表数据(以数组为参数实现) -->
<select id="getStorageBySupplierIdArray" resultType="com.cskt.mybatis2.pojo.StorageRecord">
select * from t_storageRecord where supplierId in
<foreach collection="array" item="item" open="(" separator="," close= ")">
#{item}
</foreach>
</select>
5、set
简化SQL语句中set子句处理,智能忽略更新语句尾部多出来的逗号
以下是语法:
<set>
<if test="条件判断">
SQL语句
</if>
…
</set>
以下是示例:
<!-- 根据Id修改用户信息 -->
<update id="getUpdateUser" parameterType="com.cskt.mybatis2.pojo.SysUser">
update t_sysUSer
<set>
<if test="account!=null">account=#{account},</if>
<if test="realName!=null">realName=#{realName},</if>
<if test="sex!=null">sex=#{sex},</if>
<if test="phone!=null">phone=#{phone},</if>
<if test="age!=null">age=#{age},</if>
<if test="roleId!=null">roleId=#{roleId},</if>
</set>
where id=#{id}
</update>
6、trim
动态地为SQL语句添加前后缀,智能忽略标签前后多余的and、or或逗号等字符
以下是语法:
<trim prefix = "前缀"
suffix = "后缀"
prefixOverrides = "忽略前缀"
suffixOverrides = "忽略后缀" >
</trim>
以下是示例:
<!-- 根据id修改供货商表的供货商名称、描述、联系电话 -->
<update id="updateSupplier2" parameterType="com.cskt.mybatis2.pojo.Supplier">
update t_supplier
<trim prefix="set" suffix="where id=#{id}" suffixOverrides=",">
<if test="supName !=null">
supName=#{supName},
</if>
<if test="supDesc !=null">
supDesc=#{supDesc},
</if>
<if test="supPhone !=null">
supPhone=#{supPhone},
</if>
</trim>
</update>