MyBatis_tp50_动态sql_sql标签_抽取可重用的sql片段_使用include标签进行引用
2024-08-20 18:45:59
笔记要点
出错分析与总结
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)
最新文章
- ELK+redis搭建nginx日志分析平台
- 数据结构图文解析之:树的简介及二叉排序树C++模板实现.
- winform学习笔记-文档路径
- 为ListView组件加上快速滑块以及修改快速滑块图像
- go语言之并发
- __NSCFConstantString
- Android开发-API指南-<;meta-data>;
- IntelliJ IDEA的Maven项目在修改时报java.lang.OutOfMemoryError: PermGen space异常
- 暑假集训(1)第一弹 -----士兵队列训练问题(Hdu1276)
- bzoj1072
- CI笔记6 json 传值
- Windows Message Queue--hdu1509
- macbook连接linuxserver后不显示matlab桌面[问题]
- app控件唯一相对Xpath自动生成(增强版uiautomatorviewer)
- error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler op
- 使用混淆ProGuard压缩代码和资源/减少方法数量
- webdriver +浏览器驱动
- JAVA基本语法测试
- 使用windows性能计数器监控cpu使用率
- Matlab中 .&#39; 的作用。