MyBatis 动态SQL 逻辑
1.IF
单条件分支
<select id="findActiveBlogWithTitleLike" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</select>
- 如果没有传入title 则会选出所有处于'ACTIVE' 状态的数据,
- 若传入titile 则会进行模糊查找将结果返回也可以同时加入两个条件进行搜索
2.CHOOSE
choose类似于 switch 多条件分支
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title = #{title}
</when>
<when test="author != null and author.name != null">
AND author_name = #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
- 提供了 ' title' 就按照 title 查找
- 提供了 'author' 就按照 author 查找
3.WHERE
处理SQL 拼接问题
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</where>
</select>
where 元素只有在一个以上的 if 条件有值的时候才会去插入where字句,若最后的内容是 AND | OR 开头,where 元素也知道如何将他们去除
4. SET
用于动态更新语句
<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set> where id=#{id}
</update>
- SET 元素可以用于动态包含需要更新的列,而舍去其他的
5. For Each
对集合进行遍历
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT * FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
-
list, set, map 或者数组对象都可以作为集合参数传递给 foreach
-
声明可以在元素内使用的集合项(item) 和索引 (index)变量
当使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素
当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值
MyBatis ResultType
1. 基础类型
Java 的基础类型 int double string 和 java.lang.integer
-
mapper 接口
// 根据 id 获得数据库中的 username 字段的值 String getEmpNameById(Integer id); -
SQL 映射文件
<mapper namespace="com.xx.xx.dao.UserMapper"> <!-- 返回值为int,resultType为string ,java.lang.String也可以--> <select id="getEmpNameById" resultType="string"> select username from t_employee where id = #{id} </select> </mapper>
2. 实体类型
-
mapper接口
// 根据 id 查询信息,并把信息封装成 Employee 对象 Employee getEmpById(Integer id); -
SQL 映射文件
<mapper namespace="com.xx.xx.dao.UserMapper"> <!-- 返回值为实体类,resultType为employee--> <select id="getEmpById" resultType="employee"> select * from t_employee where id = #{id} </select> </mapper>
3. List 类型
-
mapper 接口
// 假如是全表查询数据,将查询的数据封装成 Employee 类型的集合 List<Employee> getALLEmps(); -
SQL 映射文件
<mapper namespace="com.xx.xx.dao.UserMapper"> 注意这里的 resultType 返回值类型是集合内存储数据的类型,不是 'list' <select id="getAllEmps" resultType="employee"> select * from t_employee </select> </mapper>
4. Map 类型 java.util.map
-
mapper 接口
// 根据 id 查询信息,并把结果信息封装成 Map Map<String, Object> getEmpAsMapById(Integer id); -
返回类型为 List<Map <String,Object> >
<mapper namespace="com.xx.xx.dao.UserMapper"> <!-- 返回值为Map<String,Object>,resultType为map--> <select id="getEmpAsMapById" resultType="map"> select * from t_employee where id = #{id} </select> </mapper>
5. List - LinkedHashMap 类型
-
mapper 接口
List<LinkedHashMap<String, Object>> listCycleTimesByAllLayer(Map<String, Object> map); -
返回类型为 LinkedHashMap
<select id="listCycleTimesByAllLayer" resultType="java.util.LinkedHashMap">\