MyBatis dynamic SQL foreach tag realizes batch insertion

Requirement: Find out the record with a given 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>  


Regarding the foreach tag, there are several properties that should be noted:

 

 

[java]  view plain copy  
 
  1. collection: Specifies the collection to traverse:  
  2. The parameters of the list type will be specially processed and encapsulated in the map, and the key of the map is called list  
  3. item: assign the currently traversed element to the specified variable  
  4. separator: the separator between each element  
  5. open: traverse all the results and splicing a starting character  
  6. close: traverse all the results and splicing an ending character  
  7. index: index. When traversing the list, the index is the index, and the item is the current value  
  8. When traversing the map, the index represents the key of the map, and the item is the value of the map.  
  9. #{variable name} can take out the value of the variable, which is the element currently traversed  


testing method:

 

 

[java]  view plain copy  
 
  1. @Test  
  2.     public void testDynamicSqlTest() throws IOException{  
  3.         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();  
  4.         //1. The obtained SqlSession will not automatically submit data  
  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.     }  



The foreach tag can also implement batch insertion (deletion) of data:

Here is an example of batch inserting data:

 

[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>  

Corresponding interface:

 

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


testing method

[java]  view plain copy  
 
    1. @Test  
    2.     public void testBatchSave() throws IOException{  
    3.         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();  
    4.         //1. The obtained SqlSession will not automatically submit data  
    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","[email protected]","1",new Department(1)));  
    11.             emps.add(new Employee(null,"2Pac","[email protected]","1",new Department(1)));  
    12.             mapper.addEmps(emps);  
    13.             openSession.commit();  
    14.         }  
    15.         finally {  
    16.             openSession.close();  
    17.         }  
    18.   
    19.     }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772688&siteId=291194637