Mybatis有两种方式配置SQL
方式一:使用XML文件配置
方式二:在注解中配置SQL
通常,使用第一种方式,这里也只阐述第一种方式中SQL的用法
Mybatis动态的SQL常用的几个元素
元素 | 作用 | 备注 |
---|---|---|
if | 判断语句 | 单条件分之判断 |
choose(when、otherwise) | 相当于java中的case when语句 | 多条件分之判断 |
trim(where、set) | 辅助元素 | 用于处理一些SQL拼装问题 |
foreach | 循环语句 | 在in语句等列举条件常用 |
动态SQL用法
(1)if元素
if相当于java中的if判断语句,常常与test属性联合使用。实例如下:
查询要求:根据角色名称模糊查询角色,但是角色名称可填可不填。
<select id="findRoles" parameterType="string" resultMap="roleResultMap">
select role_no,role_name,note from t_role where 1=1
<if test="roleName != null and roleName != ''">
and role_name like concat('%',#{roleName},'%')
</if>
</select>
备注:roleName是传进的参数,参数名称必须要对应。
(2)choose、when、otherwise元素
这三个元素是多条件判断语句,相当于java中的switch…case…default语句。
查询要求:
- 当角色编号不为空,则根据角色编号查询
- 当角色编号为空,而角色名称不为空,则根据角色名称模糊查询
- 当角色编号和角色名称均为空,则要求角色备注不为空
<select id="findRoles" parameterType="role" resultMap="roleResultMap">
select role_no,role_name,note from t_role where 1=1
<choose>
<when test="roleNo != null and roleNo != ''">
and role_no = #{roleNo}
</when>
<when test="roleName != null and roleName != ''">
and role_name like concat('%',#{roleName},'%')
</when>
<otherwise>
and note is not null
</otherwise>
</choose>
</select>
(3)trim、where、set元素
细心的读者可以发现上面的查询加入了一个条件”1=1“。若没有这个条件,可能会出问题。
如果我们使用<where>
就可以轻松解决这个问题了,当where中元素条件成立时,会给SQL语句自动加上where关键字,否则不加入。
<select id="findRoles" parameterType="string" resultMap="roleResultMap">
select role_no,role_name,note from t_role
<where>
<if test="roleName != null and roleName != ''">
role_name like concat('%',#{roleName},'%')
</if>
</where>
</select>
当然,方式不止一种,除了使用where标签外,还可以使用trim进行处理。
<select id="findRoles" parameterType="string" resultMap="roleResultMap">
select role_no,role_name,note from t_role
<trim prefix="where" prefixOverrides="and">
<if test="roleName != null and roleName != ''">
and role_name like concat('%',#{roleName},'%')
</if>
</trim>
</select>
备注:prefix代表语句的前缀,当trim中元素条件成立时,会加上where关键zi
prefixOverrides代表是需要去掉的没用字样,比如这里的and。当当条件成立时,不去掉and,语句后半部分后变成where and and role_name like concat(‘%’,#{roleName},’%’),此时多个一个and。
set标签一般用户更新数据时使用。
<update id="updateRole" parameterType="role">
update t_role
<set>
<if test="roleName != null and roleName != ''">
role_name = #{roleName},
</if>
<if test="note != null and note != ''">
note = #{note},
</if>
</set>
where role_no = #{roleNo}
</update>
备注:set元素遇到逗号,会把对应的逗号去掉。
该语句也可以使用trim进行处理
<trim prefix="set" suffixOverrides=",">...</trim>
备注:suffixOverrides与prefixOverrides类似,前者是表示后缀,后者表示前缀。
1.where作用
a.当where标签内有条件成立时,添加where关键字,反之,不加where关键字
b.当where内条件成立时,添加的条件最前面有多余的and,or等关键字时,会自动去除2.set作用
a.添加set关键字
b.去除结尾多余的逗号
(4) foreach元素
该元素的作用是遍历集合。Mybatis可以很好的支持数组和List、Set接口的集合。
查询要求:根据性别查询,性别参数是个集合,是由男、女、未知三种组成。
<select id="findUserBySex" resultType="user">
select * from t_user where sex in
<foreach item="sex" index="index" collection="sexList" open="(" separator="," close=")">
#{sex}
</foreach>
</select>
备注:
- collection配置的sexList是传递进的参数名称,他可以是数组或者List,Set等集合
- item配置的是循环中当前的元素
- index配置的是当前元素在集合中的位置下标
- open和close配置的是以什么符号将这些集合元素包装起来。
- separator是各个元素之间的间隔符。
注意:sql查询中in一般会消耗大量的性能,同时一些数据库对于sql的长度也有限制。