MyBatis中動態SQL
一、說在前面
mybatis 對sql語句進行靈活操作,通過表達式進行判斷,對sql進行靈活拼接、組裝。二、實例代碼片段
1、if
<!-- 傳遞pojo綜合查詢用戶信息 -->
<select id="findUserList" parameterType="user" resultType="user">
select * from user
where 1=1
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</select>
2、where
使用where來修改上面的代碼,<where />可以自動處理第一個and。<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</where>
</select>
3、foreach
向sql傳遞數組或List,mybatis使用foreach解析,代碼如下:(1)需求
在用戶查詢列表和查詢總數的statement中增加多個id輸入查詢。
sql語句如下(兩種方法):
SELECT * FROM user WHERE sex=1 and username LIKE "%小明%" and (id=1 OR id=16 OR id=22)
SELECT * FROM user WHERE sex=1 and username LIKE "%小明%" and id in (1,16,22)
(2)在pojo中定義list屬性ids存儲多個用戶id,並添加getter/setter方法
//傳入多個id
private List<Integer> ids;
public List<Integer> getIds() {
return ids;
}
public void setIds(List<Integer> ids) {
this.ids = ids;
}
(3)修改mapper.xml
<if test="ids!=null">
<!-- 使用foreach遍歷傳入的ids
collection:指定輸入對象集合屬性
item:每次遍歷生成的對象
open:開始遍歷時候的拼接串
close:結束遍歷時候拼接的串
separator:遍歷的兩個對象中需要拼接的串
-->
<!--
實現下面的sql拼接
and (id=1 OR id=16 OR id=22)
-->
<foreach collection="ids" item="user_id" open="and (" close=")" separator="or">
id=#{user_id}
</foreach>
<!--
另一種方式實現拼接,實現下面的sql拼接
and id in (1,16,22)
-->
<!-- <foreach collection="ids" item="user_id" open="and id in (" close=")" separator=",">
#{user_id}
</foreach> -->
</if>
(4)測試代碼
//Mapper代理方式 用戶綜合信息查詢
@Test
public void testFindUserList() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
//創建UserMapper對象,mybatis自動生成代理對象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//創建包裝對象,設置查詢條件
UserQueryVo userQueryVo = new UserQueryVo();
UserCustom userCustom = new UserCustom();
userCustom.setSex("1");
userCustom.setUsername("小明");
//傳入多個ids
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(16);
ids.add(22);
userQueryVo.setUserCustom(userCustom);
userQueryVo.setIds(ids);
//調用UserMapper的方法
List<UserCustom> list = userMapper.findUserList(userQueryVo);
System.out.println("Mapper代理方式 用戶綜合信息查詢====》"+list);
}
4、sql片段
Sql中可將重複的sql提取出來,使用時用include引用即可,最終達到sql重用的目的,如下(1)未提取重複之前代碼
<!-- 傳遞pojo綜合查詢用戶信息 -->
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</where>
</select>
(2)提取where條件
<sql id="query_user_where">
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</sql>
(3)使用include引用
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<include refid="query_user_where"/>
</where>
</select>
(4)總結如果引用其它mapper.xml的sql片段,則在引用時需要加上namespace,如下:
<include refid="namespace.sql片段”/>
By luoyepiaoxue2014
微博地址: http://weibo.com/luoyepiaoxue2014 點擊打開鏈接