笔记要点
出错分析与总结
include内部使用自定的属性,之能使用$ {}来取值 ,#{}不能用
工程组织
数据库组织
0.重新修改Bean类
1.定义接口
public interface EmployeeMapper_DynamicSQL { //批量插入,测试sql标签 public void addEmps2(@Param("emps")List<Employee> emps);
2.定义XML映射文件
<!--==================================================--> <!--sql标签: 抽取可重用的sql片段,方便后面的进行引用; 引用方法: <include refid="" ></include>
include内部使用自定的属性,之能使用$ {}来取值 ,#{}不能用 --> <sql id="insertColumn"> <if test="_databaseId=='mysql'"> last_name , email ,gender , d_id </if> </sql> <insert id="addEmps2"> INSERT INTO tbl_employee( <include refid="insertColumn"></include> ) VALUES <foreach collection="emps" item="emp" separator=","> (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id}) </foreach> </insert>
3.编写测试代码
@Test public void test12() throws Exception { SqlSession openSession = getSqlSessionFactory().openSession(); try { System.out.println("++++++++++---- tp48.测试动态sql_ sql标签:可重复引用数据内容"); EmployeeMapper_DynamicSQL mapper = openSession.getMapper(EmployeeMapper_DynamicSQL.class); List<Employee> emps=new ArrayList<>(); emps.add(new Employee(null,"smith2" ,"smith@qq.com" , "1", new Department(1))); emps.add(new Employee(null,"aliex2" ,"aliex@qq.com" , "0", new Department(1))); mapper.addEmps2(emps); openSession.commit(); } finally { openSession.close(); } }
测试结果
++++++++++---- tp48.测试动态sql_ sql标签:可重复引用数据内容 DEBUG 12-05 18:24:23,805 ==> Preparing: INSERT INTO tbl_employee( last_name , email ,gender , d_id ) VALUES (?,?,?,?) , (?,?,?,?) (BaseJdbcLogger.java:145) DEBUG 12-05 18:24:23,821 ==> Parameters: smith2(String), smith@qq.com(String), 1(String), 1(Integer), aliex2(String), aliex@qq.com(String), 0(String), 1(Integer) (BaseJdbcLogger.java:145) DEBUG 12-05 18:24:23,821 <== Updates: 2 (BaseJdbcLogger.java:145)