第四章 Mybatis映射文件详解
4.1 映射文件概述
- MyBatis 的真正强大在于它的语句映射,这是它的魔力所在。
- 如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% 的代码。
4.2 映射文件根标签
- mapper标签
- mapper中的namespace要求与接口的全类名一致
4.3 映射文件子标签
子标签共有9个,注意学习其中8大子标签
- insert标签:定义添加SQL
- delete标签:定义删除SQL
- update标签:定义修改SQL
- select标签:定义查询SQL
- sql标签:定义可重用的SQL语句块
- cache标签:设置当前命名空间的缓存配置
- cache-ref标签:设置其他命名空间的缓存配置
- resultMap标签:描述如何从数据库结果集中加载对象
- resultType解决不了的问题,交个resultMap。
4.4 映射文件中常用属性
- resultType:设置期望结果集返回类型【全类名或别名】
- 注意:如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身的类型。
- resultType 和 resultMap 之间只能同时使用一个。
day03
4.5 获取主键自增数据
- useGeneratedKeys:启用主键生成策略
- keyProperty:设置存储属性值
4.6 获取数据库受影响行数
- 直接将接口中方法的返回值设置为int或boolean即可
- int:代表受影响行数
- boolean
- true:表示对数据库有影响
- false:表示对数据库无影响
第五章 Mybatis中参数传递问题
5.1 单个普通参数
- 可以任意使用:参数数据类型、参数名称不用考虑
5.2 多个普通参数
- Mybatis底层封装Map结构,封装key为param1、param2....【支持:arg0、arg1、...】
5.3 命名参数
- 语法:
- @Param(value="参数名")
- @Param("参数名")
- 位置:参数前面
- 注意:
- 底层封装Map结构
- 命名参数,依然支持参数【param1,param2,...】
- 示例代码
1. /** 2. * 通过员工姓名及薪资查询员工信息【命名参数】 3. * @return 4. */ 5. public List<Employee> selectEmpByNamed(@Param("lName")String lastName, 6. @Param("salary") double salary);
1. <select id="selectEmpByNamed" resultType="employee"> 2. SELECT 3. id, 4. last_name, 5. email, 6. salary 7. FROM 8. tbl_employee 9. WHERE 10. last_name=#{param1} 11. AND 12. salary=#{param2} 13. </select>
- 源码分析
- MapperMethod对象:142行代码【命名参数底层代码入口】
- 命名参数底层封装map为ParamMap,ParamMap继承HashMap
- ParamNameResolver对象:130行代码,命名参数底层实现逻辑
1. //130行 2. final Map<String, Object> param = new ParamMap<>(); 3. int i = 0; 4. for (Map.Entry<Integer, String> entry : names.entrySet()) { 5. param.put(entry.getValue(), args[entry.getKey()]); 6. // add generic param names (param1, param2, ...) 7. final String genericParamName = GENERIC_NAME_PREFIX + (i + 1); 8. // ensure not to overwrite parameter named with @Param 9. if (!names.containsValue(genericParamName)) { 10. param.put(genericParamName, args[entry.getKey()]); 11. } 12. i++; 13. } 14. return param;
5.4 POJO参数
- Mybatis支持POJO【JavaBean】入参,参数key是POJO中属性
5.5 Map参数
- Mybatis支持直接Map入参,map的key=参数key
5.6 Collection|List|Array等参数
- 参数名:collection、list、array
第六章 Mybatis参数传递【#与$区别】
6.1 回顾JDBC
- DriverManager
- Connection
- Statement:执行SQL语句,入参使用SQL【String】拼接方式
- PreparedStatement执行SQL语句【预编译SQL】,入参使用占位符方式
- ResultSet
6.2 #与$区别
- 【#】底层执行SQL语句的对象,使用PreparedStatementd,预编译SQL,防止SQL注入安全隐患,相对比较安全。
- 【$】底层执行SQL语句的对象使用Statement对象,未解决SQL注入安全隐患,相对不安全。
6.3 #与$使用场景
查询SQL:select col,col2 from table1 where col=? and col2=? group by ?, order by ? limit ?,?
- #使用场景,sql占位符位置均可以使用#
- $使用场景,#解决不了的参数传递问题,均可以交给$处理【如:form 动态化表名】
1. /** 2. * 测试$使用场景 3. */ 4. public List<Employee> selectEmpByDynamitTable(@Param("tblName") String tblName);
1. <select id="selectEmpByDynamitTable" resultType="employee"> 2. SELECT 3. id, 4. last_name, 5. email, 6. salary 7. FROM 8. ${tblName} 9. </select>
第七章 Mybatis查询中返回值四种情况
7.1 查询单行数据返回单个对象
1. /** 2. * 通过id获取员工信息 3. */ 4. public Employee selectEmpById(int empId);
1. <select id="selectEmpById" resultType="employee"> 2. SELECT 3. id, 4. last_name, 5. email, 6. salary 7. FROM 8. tbl_employee 9. WHERE 10. id=#{empId} 11. </select>
7.2 查询多行数据返回对象的集合
1. /** 2. * 查询所有员工信息 3. */ 4. public List<Employee> selectAllEmps();
1. <select id="selectAllEmps" resultType="employee"> 2. SELECT 3. id, 4. last_name, 5. email, 6. salary 7. FROM 8. tbl_employee 9. </select>
- 注意:如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身的类型。
7.3 查询单行数据返回Map集合
- Map<String key,Object value>
- 字段作为Map的key,查询结果作为Map的Value
- 示例代码
1. /** 2. * 查询单行数据返回Map集合 3. * @return 4. */ 5. public Map<String,Object> selectEmpReturnMap(int empId);
1. <!-- 查询单行数据返回Map集合--> 2. <select id="selectEmpReturnMap" resultType="map"> 3. SELECT 4. id, 5. last_name, 6. email, 7. salary 8. FROM 9. tbl_employee 10. WHERE 11. id=#{empId} 12. </select>
7.4 查询多行数据返回Map集合
- Map<Integer key,Employee value>
- 对象的id作为key
- 对象作为value
- 示例代码
1. /** 2. * 查询多行数据返回Map 3. * Map<Integer,Object> 4. * Map<Integer,Employee> 5. * 对象Id作为:key 6. * 对象作为:value 7. * @return 8. */ 9. @MapKey("id") 10. public Map<Integer,Employee> selectEmpsReturnMap();
1. <select id="selectEmpsReturnMap" resultType="map"> 2. SELECT 3. id, 4. last_name, 5. email, 6. salary 7. FROM 8. tbl_employee 9. </select>