MyBatis动态SQL foreach标签实现批量插入

需求:查出给定id的记录:

 

[html] view plain copy
 
  1. <select id="getEmpsByConditionForeach" resultType="com.test.beans.Employee">  
  2.         SELECT * FROM tb1_emplyee WHERE id IN  
  3.         <foreach collection="list" item="item_id" separator="," open="(" close=")">  
  4.             #{item_id}  
  5.         </foreach>  
  6. </select>  


关于foreach标签,有几个属性应该注意一下:

 

 

[java] view plain copy
 
  1. collection:指定要遍历的集合:  
  2. list类型的参数会特殊处理封装在map中,map的key就叫list  
  3. item:将当前遍历出的元素赋值给指定的变量  
  4. separator:每个元素之间的分隔符  
  5. open:遍历出所有结果拼接一个开始的字符  
  6. close:遍历出所有结果拼接一个结束的字符  
  7. index:索引。遍历list的时候是index就是索引,item就是当前值  
  8. 遍历map的时候index表示的就是map的key,item就是map的值  
  9. #{变量名}就能取出变量的值也就是当前遍历出的元素  


测试方法:

 

 

[java] view plain copy
 
  1. @Test  
  2.     public void testDynamicSqlTest() throws IOException{  
  3.         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();  
  4.         //1、获取到的SqlSession不会自动提交数据  
  5.         SqlSession openSession = sqlSessionFactory.openSession();  
  6.         try  
  7.         {  
  8.                 EmployeeMapperDymanicSQL mapper=openSession.getMapper(EmployeeMapperDymanicSQL.class);  
  9.                 /*Employee employee=new Employee(1,"lili",null,"1");*/  
  10.                 List<Employee> emps=mapper.getEmpsByConditionForeach(Arrays.asList(1,2,3,4));  
  11.                 for (Employee e:emps){  
  12.                     System.out.println(e);  
  13.                 }  
  14.   
  15.         }  
  16.         finally {  
  17.             openSession.close();  
  18.         }  
  19.     }  



foreach标签也可以实现实现批量插入(删除)数据:

这里以批量插入数据为例:

 

[html] view plain copy
 
  1. <insert id="addEmps">  
  2.         INSERT INTO tb1_emplyee(last_name,email,gender,d_id)  
  3.         VALUES   
  4.         <foreach collection="emps" item="emp" separator=",">  
  5.             (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})  
  6.         </foreach>  
  7. </insert>  

对应的接口:

 

[java] view plain copy
 
  1. public void addEmps(@Param("emps")List<Employee> emps);  


测试方法

[java] view plain copy
 
    1. @Test  
    2.     public void testBatchSave() throws IOException{  
    3.         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();  
    4.         //1、获取到的SqlSession不会自动提交数据  
    5.         SqlSession openSession = sqlSessionFactory.openSession();  
    6.         try  
    7.         {  
    8.             EmployeeMapperDymanicSQL mapper=openSession.getMapper(EmployeeMapperDymanicSQL.class);  
    9.             List<Employee> emps=new ArrayList<Employee>();  
    10.             emps.add(new Employee(null,"Eminem","Eminem@126.com","1",new Department(1)));  
    11.             emps.add(new Employee(null,"2Pac","2Pac@126.com","1",new Department(1)));  
    12.             mapper.addEmps(emps);  
    13.             openSession.commit();  
    14.         }  
    15.         finally {  
    16.             openSession.close();  
    17.         }  
    18.   
    19.     }  
posted @ 2018-04-19 09:56  庞国明  阅读(561)  评论(0编辑  收藏  举报