需求:查出给定id的记录:
关于foreach标签,有几个属性应该注意一下:
测试方法:
@Test public void testDynamicSqlTest() throws IOException{ SqlSessionFactory sqlSessiOnFactory= getSqlSessionFactory(); //1、获取到的SqlSession不会自动提交数据 SqlSession openSession = sqlSessionFactoryopenSession(); try { EmployeeMapperDymanicSQL mapper=openSessiongetMapper(EmployeeMapperDymanicSQLclass); /*Employee employee=new Employee(1,"lili",null,"1");*/ Listemps=mappergetEmpsByConditionForeach(ArraysasList(1,2,3,4)); for (Employee e:emps){ Systemoutprintln(e); } } finally { openSessionclose(); } }
foreach标签也可以实现实现批量插入(删除)数据:
这里以批量插入数据为例:
INSERT INTO tb1_emplyee(last_name,email,gender,d_id) VALUES (#{emplastName},#{empemail},#{empgender},#{empdeptid})
对应的接口:
public void addEmps(@Param("emps")Listemps);
测试方法
@Test public void testBatchSave() throws IOException{ SqlSessionFactory sqlSessiOnFactory= getSqlSessionFactory(); //1、获取到的SqlSession不会自动提交数据 SqlSession openSession = sqlSessionFactoryopenSession(); try { EmployeeMapperDymanicSQL mapper=openSessiongetMapper(EmployeeMapperDymanicSQLclass); Listemps=new ArrayList (); empsadd(new Employee(null,"Eminem","Eminem@com","1",new Department(1))); empsadd(new Employee(null,"2Pac","2Pac@com","1",new Department(1))); mapperaddEmps(emps); openSessioncommit(); } finally { openSessionclose(); } }
到此这篇关于MyBatis动态SQL foreach标签实现批量插入的方法示例的文章就介绍到这了,更多相关MyBatis动态SQL foreach批量插入内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!