#的MyBatis中参数传递问题
##一个别普通类型参数
- 普通类型=字符串+基本数据类型及包装类-任意使用(参数名称随意使用)
二多个参数
- 普通参数 * mybatis临时会封装的地图,键是固定值:{0,1 | param1,param2}-命名参数(重要) * mybatis可以通过命名参数作为地图的键,同时{param1,param2 …}依然适用。 *使用@Param(“命名”)注解实现命名参数-POJO参数(的| MyBatis的使用连续地图结构传递参,使用地图的的|多个|(重要) * mybatsi连续使用POJO中的属性作为地图的关键字,使用POJO中的属性获取参数值地图。+系列->键:收集+套装->键:收集+列表->键:收集和清单+表格->键:堆库+基元分析_命名参数+ MepperMethod类中的81行代码->对象参数= method.convertArgsToSqlCommandParam( args); +最终在ParamNameResolver中封装并贴图结构,具体代码如下:+最终地图结构为:最终Map <String,Object> param = new ParamMap < Object >(); +具体的封装过程代码如下:
` java final Map <String,Object> param = new ParamMap (); int i = 0; 对于(Map.Entry <Integer,String> entry:names.entrySet()){ param.put(entry.getValue(),args [entry.getKey()]); //添加通用参数名称(param1,param2等)
final String genericParamName = GENERIC_NAME_PREFIX + String.valueOf(i + 1);
// ensure not to overwrite parameter named with @Param
if (!names.containsValue(genericParamName)) {
param.put(genericParamName, args[entry.getKey()]);
}
i++;
}
return param;
```
查询(select)的几种情况:
resultType设置问题(期望sql查询的结果集封装类型(全类名或别名))
一 查询单行数据返回单个对象(重要)
- 映射文件中,resultType设置为POJO。
二 查询多行数据返回对象的集合(重要)
- resultType:注意,如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身的类型
- resultType依然设置POJO。
三 查询单行数据返回Map集合
- resultType设置为Map类型的全类名(java.util.Map)
- 将结果集的字段名作为map的key。
- map.get(“last_name”).
四 查询多行数据返回Map集合
- resultType依然设置POJO(返回类型中包含POJO)
- 必须使用@MapKey()注解,指定POJO类中的属性作为Map的key。
自动映射(resultType)&自定义映射(resultMap)
一 自动映射(resultType)
-
概述:自动将表中的字段与POJO类中的属性关联映射。
-
条件
- 结果集中的列明与POJO中的属性名一致
- 如果不一致可以使用mapUnderscoreToCamelCase=true设置,完成a_cl与aCl的映射。
-
问题:自动映射不是万能的,以下两种情况自动映射解决不了。
- 结果集中的列名,与POJO属性不满足驼峰式自动映射条件时,无法使用自动映射。
eg: last_name无法lName自动映射 - 多表连接查询,如需要多个表中的数据时,无法完成自动映射。
eg: SELECT * FROM tbl_employee e,tbl_dept d WHERE e.dept_id
=d.id
- 结果集中的列名,与POJO属性不满足驼峰式自动映射条件时,无法使用自动映射。
二 自定义映射
- 回顾表连接查询的几种情况
-
等值连接
select a.id,a.name,b.id,b.name
from A a,B b
where a.b_id = b.id -
内连接
select a.id,a.name,b.id,b.name
from A a inner join B b
on a.b_id = b.id -
外连接
- 左外
select a.id,a.name,b.id,b.name
from A a left join B b
on a.b_id = b.id - 右外
select a.id,a.name,b.id,b.name
from A a right join B b
on a.b_id = b.id
- 左外
-
自定义映射(resultMap)使用
- resultType解决不了时,使用resultMap
- 自定义映射详解
- 级联映射
<resultMap id="empAndDeptBase" type="com.atguigu.mybatis.bean.Employee"> <!-- 2)用于完成主键值的映射--> <id column="eid" property="id"></id> <!-- 3)用于完成普通列的映射--> <result column="last_name" property="lastName"></result> <result column="email" property="email"></result> <result column="gender" property="gender"></result> <!-- 级联映射--> <result column="did" property="dept.id"></result> <result column="dept_name" property="dept.deptName"></result> </resultMap> <select id="getEmpsByDeptId" resultMap="empAndDeptBase"> SELECT e.`id` eid,e.`last_name`,e.`email`,e.`gender`, d.`id` did,d.`dept_name` FROM tbl_employee e,tbl_dept d WHERE e.`dept_id`=d.`id` AND d.id=#{dId} </select>
- association映射
<!-- association--> <resultMap id="empAndDeptBaseAssociation" type="com.atguigu.mybatis.bean.Employee"> <id column="eid" property="id"></id> <result column="last_name" property="lastName"></result> <result column="email" property="email"></result> <result column="gender" property="gender"></result> <!-- association:多对一的关联关系 collection:一对多关联关系 --> <association property="dept" javaType="com.atguigu.mybatis.bean.Dept"> <result column="did" property="id"/> <result column="dept_name" property="deptName"/> </association> </resultMap> <select id="getEmpsByDeptIdAssociation" resultMap="empAndDeptBaseAssociation"> SELECT e.`id` eid,e.`last_name`,e.`email`,e.`gender`, d.`id` did,d.`dept_name` FROM tbl_employee e,tbl_dept d WHERE e.`dept_id`=d.`id` AND d.id=#{dId} </select>
- 分步查询
<!-- association高级用法 分步查询step: 1. 通过部门Id,查询员工信息 2. 通过部门Id,查询部门信息 --> <resultMap id="empAndDeptBaseAssociationStep" type="com.atguigu.mybatis.bean.Employee"> <id column="eid" property="id"></id> <result column="last_name" property="lastName"></result> <result column="email" property="email"></result> <result column="gender" property="gender"></result> <!-- 使用association分步查询 select属性:指定分步查询的方法(sql语句) column:指定方法参数(结果集列名) --> <association property="dept" select="com.atguigu.mybatis.mapper.DeptMapper.getDept" column="dept_id"> </association> </resultMap> <select id="getEmpsByDeptIdAssociationStep" resultMap="empAndDeptBaseAssociationStep"> SELECT e.`id` eid,e.`last_name`,e.`email`,e.`gender`,e.dept_id FROM tbl_employee e WHERE e.`dept_id`= #{did} </select> ------------------------------------------------------ <select id="getDept" resultType="com.atguigu.mybatis.bean.Dept"> select id,dept_name from tbl_dept where id = #{did} </select>
- 延迟加载(懒加载)
<!-- settings--> <settings> <!-- 开启延迟加载 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 设置加载的数据是按需还是全部 --> <setting name="aggressiveLazyLoading" value="false"/> </settings>
- 级联映射
-