<selectid="findActiveBlogWithTitleLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <iftest="title != null"> AND title like #{title} </if> <iftest="author != null and author.name != null"> AND author_name like #{author.name} </if> </select>
choose、when、otherwise
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<selectid="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <choose> <whentest="title != null"> AND title like #{title} </when> <whentest="author != null and author.name != null"> AND author_name like #{author.name} </when> <otherwise> AND featured = 1 </otherwise> </choose> </select>
where
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<selectid="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG <where> <iftest="state != null"> state = #{state} </if> <iftest="title != null"> AND title like #{title} </if> <iftest="author != null and author.name != null"> AND author_name like #{author.name} </if> </where> </select>
trim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<selectid="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG <trimprefix="WHERE"prefixOverrides="AND |OR "> <iftest="state != null"> state = #{state} </if> <iftest="title != null"> AND title like #{title} </if> <iftest="author != null and author.name != null"> AND author_name like #{author.name} </if> </trim> </select>
动态 SQL 的另一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候)。比如:
1 2 3 4 5 6 7 8 9
<selectid="selectPostIn"resultType="domain.blog.Post"> SELECT * FROM POST P WHERE ID in <foreachitem="item"index="index"collection="list" open="("separator=","close=")"> #{item} </foreach> </select>
<selectid="selectBlogsLike"resultType="Blog"> <bindname="pattern"value="'%' + _parameter.getTitle() + '%'" /> SELECT * FROM BLOG WHERE title LIKE #{pattern} </select>
<selectid="getAllUser"resultType="User"> select id,userName name from user </select>
解决方案二:使用resultMap
优势:开发效率高,降低代码耦合度
1 2 3 4 5 6 7
<resultMapid="uesrList"type="user"> <idproperty="id"column="id"></id> <resultcolumn="userName"property="name"></result> </resultMap> <selectid="getAllUser"resultMap="userList"> select * from user </select>
<resultMapid="rMap"type="Role"> <idproperty="id"column="id"/> <resultproperty="roleName"column="roleName"/> <resultcolumn="roleDesc"property="roleDesc"/> <collectionproperty="userList"ofType="User"> <idcolumn="uId"property="id"/> <resultcolumn="userName"property="userName"/> <resultcolumn="sex"property="sex"/> <resultcolumn="birthday"property="birthday"/> </collection> </resultMap> <selectid="getAllRoleAndUser"resultMap="rMap"> SELECT r.*, u.id uId, u.userName, u.sex, u.birthday FROM role r LEFT JOIN user_role ur ON r.id = ur.rid LEFT JOIN USER u ON u.id = ur.uId </select>
UserMapper.xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<resultMapid="uMap1"type="User"> <idproperty="id"column="id"/> <resultproperty="sex"column="sex"/> <resultcolumn="birthday"property="birthday"/> <resultcolumn="userName"property="userName"/> <collectionproperty="roleList"ofType="Role"> <idcolumn="rId"property="id"/> <resultcolumn="roleName"property="roleName"/> <resultcolumn="roleDesc"property="roleDesc"/> </collection> </resultMap> <selectid="getAllUser1"resultMap="uMap1"> SELECT u.*, r.roleName, r.id rId, r.roleDesc FROM USER u LEFT JOIN user_role ur ON ur.uId = u.id LEFT JOIN role r ON ur.rId = r.id </select>