MySql的動態語句foreach各種用法比較
1.dao層
//批量刪除
public void deleteRoleManagers(List<Integer> list);
//批量刪除2
public void deleteRoles(Map<String,Object> map);
2.xml檔案
<delete id="deleteRoleManagers" >
delete from user_role
where ID in
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<delete id="deleteRoles">
delete from user_role
where ID in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
3.測試程式碼
@Test
public void deleteRoleManagers(){
RoleManagerDao dao=ctx.getBean("roleManagerDao",RoleManagerDao.class);
//User user=new User(0,"chang",27,new Date(),"hu",new Date(),"jian");
List<Integer> list=new ArrayList<Integer>();
list.add(14);
list.add(15);
dao.deleteRoleManagers(list);
System.out.println(list);
}
@Test
public void deleteRoles(){
RoleManagerDao dao=ctx.getBean("roleManagerDao",RoleManagerDao.class);
Map<String,Object> map=new HashMap<String,Object>();
List<Integer> list=new ArrayList<Integer>();
list.add(12);
list.add(13);
map.put("ids", list);
dao.deleteRoles(map);
System.out.println(map);
}
4.各種用法
foreach的主要用在構建in條件中,它可以在SQL語句中進行迭代一個集合。foreach元素的屬性主要有item,index,collection,open,separator,close。item表示集合中每一個元素進行迭代時的別名,index指定一個名字,用於表示在迭代過程中,每次迭代到的位置,open表示該語句以什麼開始,separator表示在每次進行迭代之間以什麼符號作為分隔符,close表示以什麼結束,在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況下,該屬性的值是不一樣的,主要有一下3種情況:- 如果傳入的是單引數且引數型別是一個List的時候,collection屬性值為list
- 如果傳入的是單引數且引數型別是一個array陣列的時候,collection的屬性值為array
- 如果傳入的引數是多個的時候,我們就需要把它們封裝成一個Map了,當然單引數也可以封裝成map,實際上如果你在傳入引數的時候,在MyBatis裡面也是會把它封裝成一個Map的,map的key就是引數名,所以這個時候collection屬性值就是傳入的List或array物件在自己封裝的map裡面的key
- <selectid="dynamicForeachTest"resultType="Blog">
- select * from t_blog where id in
- <foreachcollection="list"index="index"item="item"open="("separator=","close=")">
- #{item}
- </foreach>
- </select>
- public List<Blog> dynamicForeachTest(List<Integer> ids);
- @Test
- publicvoid dynamicForeachTest() {
- SqlSession session = Util.getSqlSessionFactory().openSession();
- BlogMapper blogMapper = session.getMapper(BlogMapper.class);
- List<Integer> ids = new ArrayList<Integer>();
- ids.add(1);
- ids.add(3);
- ids.add(6);
- List<Blog> blogs = blogMapper.dynamicForeachTest(ids);
- for (Blog blog : blogs)
- System.out.println(blog);
- session.close();
- }
- <selectid="dynamicForeach2Test"resultType="Blog">
- select * from t_blog where id in
- <foreachcollection="array"index="index"item="item"open="("separator=","close=")">
- #{item}
- </foreach>
- </select>
- public List<Blog> dynamicForeach2Test(int[] ids);
- @Test
- publicvoid dynamicForeach2Test() {
- SqlSession session = Util.getSqlSessionFactory().openSession();
- BlogMapper blogMapper = session.getMapper(BlogMapper.class);
- int[] ids = newint[] {1,3,6,9};
- List<Blog> blogs = blogMapper.dynamicForeach2Test(ids);
- for (Blog blog : blogs)
- System.out.println(blog);
- session.close();
- }
- <selectid="dynamicForeach3Test"resultType="Blog">
- select * from t_blog where title like "%"#{title}"%" and id in
- <foreachcollection="ids"index="index"item="item"open="("separator=","close=")">
- #{item}
- </foreach>
- </select>
- public List<Blog> dynamicForeach3Test(Map<String, Object> params);
- @Test
- publicvoid dynamicForeach3Test() {
- SqlSession session = Util.getSqlSessionFactory().openSession();
- BlogMapper blogMapper = session.getMapper(BlogMapper.class);
- final List<Integer> ids = new ArrayList<Integer>();
- ids.add(1);
- ids.add(2);
- ids.add(3);
- ids.add(6);
- ids.add(7);
- ids.add(9);
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("ids", ids);
- params.put("title", "中國");
- List<Blog> blogs = blogMapper.dynamicForeach3Test(params);
- for (Blog blog : blogs)
- System.out.println(blog);
- session.close();
- }