---------------------------------- 自定义结果映射规则 ------------------------------
1.全局setting设置 ----autoMappingBehavior默认是PARTIAL,开启自动映射的功能。唯一的要求是列名和javaBean属性名一置。 -----如果autoMappingBehavior设置为null会取消自动映射 ------数据库字段命名规则,POJO属性符合驼峰命名法,如aColumn,我们则可以开启驼峰命名规范则映射功能,mapUndersourceToCamelCase=true。 2.自定义resultMap:实现高级结果映射。
---------- 映射文件 select—resultmap—关联查询—环境搭建 ----------------
----- 映射文件 select—resultmap—关联查询—分布查询&延迟加载 -------
<!-- 分布查询的好处:可以使用延迟加载 Employee===》Dept 我们每次查询Employee对象的时候,都将一起查询出来 部门信息在我们使用的时候再去查询。 分段查询的基础上加上两个配置 --> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/>— 映射文件 select—resultmap—collection定义关联集合封装规则 --------
<!--public Department getDeptByIdPlus(Integer id);--> <!--collection嵌套结果集的方式,定义关联的集合类型元素封装规则--> <resultMap id="myDept" type="cn.itcast.mybatis.bean.Department"> <id column="did" property="id"></id> <result column="dept_name" property="departmentName"/> <!--collection 定义关联集合类型的属性的封装规则 ofType:指定集合里面元素的类型--> <collection property="emps" ofType="cn.itcast.mybatis.bean.Employee"> <!--定义集合中元素的封装规则--> <id column="eid" property="id"/> <result column="lastname" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </collection> </resultMap> <select id="getDeptByIdPlus" resultMap="myDept"> SELECT d.id did, d.dept_name dept_name, e.id eid,e.lastname lastname ,e.email email ,e.gender gender FROM tbl_dept d LEFT JOIN tbl_employee e ON d.id=e.d_id WHERE d.id=#{id} </select>-------映射文件 select—resultmap—collection分布查询&延迟加载 ---------
<resultMap id="myDeptStep" type="cn.itcast.mybatis.bean.Department"> <id column="id" property="id"/> <id column="dept_name" property="departmentName"/> <collection property="emps" select="cn.itcast.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId" column="id"></collection> </resultMap> <!-- public Department getDeptByIdPlusStep(Integer id);--> <select id="getDeptByIdPlusStep" resultMap="myDeptStep"> select id, dept_name departmentName from tbl_dept where id=#{id} </select>------ 映射文件 select—resultmap—分布查询传递多列值&fetchType -----
<!--传递多列的值, 将多列的值封装map column="{key1=column1,key2=column2}" fetchType="lazy" 表示使用延迟加载 eager——立即-->------------- 映射文件 select—resultmap—discriminator鉴别器 --------------
<!--<discriminator javaType=""> <case value=""></case> </discriminator> 鉴别器:myBatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为 封装Employee 如果查出的是女生,就把部门信息查询出来。 如果是男生,把lastname这一列的值赋值给email; --> <resultMap id="myEmpDiscriminator" type="cn.itcast.mybatis.bean.Employee"> <id column="id" property="id"/> <result column="lastname" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!--column:指定判断那一列 javaType:列值对应的java类型 --> <discriminator javaType="string" column="gender"> <case value="0" resultType="cn.itcast.mybatis.bean.Employee"> <association property="dept" select="cn.itcast.mybatis.dao.DepartmentMapper.getDeptById" column="d_id" > </association> </case> <case value="1" resultType="cn.itcast.mybatis.bean.Employee"> <id column="id" property="id"/> <result column="lastname" property="lastName"/> <result column="lastname" property="email"/> <result column="gender" property="gender"/> </case> </discriminator> </resultMap>