mybatis系列之返回结果映射

xiaoxiao2021-02-28  29

一、映射器

1.1 select元素

select元素来定义查询操作。

参数描述Id唯一标识符。 – 用来引用这条语句,需要和接口的方法名一致parameterType参数类型。可以不传,MyBatis会根据TypeHandler自动推断resultType返回值类型。别名或者全类名,如果返回的是集合,定义集合中元 素的类型。不能和resultMap同时使用

1.2 返回值为map

一条记录封装成map:

接口中的方法

//返回一条记录的map;key就是列名,值就是对应的值 public Map<String, Object> getEmpByIdReturnMap(Integer id);

映射文件的sql片段;返回值类型直接使用map即可。

<select id="getEmpByIdReturnMap" resultType="map"> select * from tbl_employee where id=#{id} </select>

多条记录封装成map:

接口方法:

//多条记录封装一个map:Map<Integer,Employee>:键是这条记录的主键, //值是记录封装后的javaBean //@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key @MapKey("lastName") public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);

映射文件:

<!--public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName); --> <!--返回值,还是bean的类型--> <select id="getEmpByLastNameLikeReturnMap" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee where last_name like #{lastName} </select>

1.3 ResultMap

自定义返回结果; 和resultType不能同时使用

autoMappingBehavior默认是PARTIAL,开启自动映射 的功能。唯一的要求是列名和javaBean属性名一致 ; 如果autoMappingBehavior设置为null则会取消自动映射

数据库字段命名规范,POJO属性符合驼峰命名法,如 A_COLUMN – aColumn,我们可以开启自动驼峰命名规 则映射功能,mapUnderscoreToCamelCase=true

但是;针对更加复杂、多变的情况,我们则使用ResultMap


使用@Alias() 取别名。

@Alias("emp") public class Employee { …… }

映射文件编写:

<!--自定义某个javaBean的封装规则,使用标签为 resultMap type:自定义规则的Java类型 id: 这个resultMap的唯一标识 --> <resultMap type="com.atguigu.mybatis.bean.Employee" id="MySimpleEmp"> <!--指定主键列的封装规则 id: 使用该标签,定义主键会底层有优化;当然也可以使用result。推荐使用id column:指定数据库中哪一列 property:指定对应的javaBean属性 --> <id column="id" property="id"/> <!-- 定义普通列封装规则 --> <result column="last_name" property="lastName"/> <!-- 其他不指定的列会自动封装:但推荐写完整。 --> <result column="email" property="email"/> <result column="gender" property="gender"/> </resultMap> <!-- resultMap: 引用片段值 --> <!-- public Employee getEmpById(Integer id); --> <select id="getEmpById" resultMap="MySimpleEmp"> select * from tbl_employee where id=#{id} </select>

在实际应用中,resultMap这个标签使用最多。


1.3.1 使用ResultMap进行关联查询结果集映射(一对一的级联)

(一)级联属性方式:

一个员工对应一个部门

@Alias("emp") public class Employee { private Integer id; private String lastName; private String email; private String gender; private Department dept; …… }

映射文件

<!-- 联合查询:级联属性封装结果集 使用 dept.id 级联属性方式 --> <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="gender" property="gender"/> <result column="did" property="dept.id"/> <result column="dept_name" property="dept.departmentName"/> </resultMap> <!-- public Employee getEmpAndDept(Integer id);--> <select id="getEmpAndDept" resultMap="MyDifEmp"> SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id, d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d WHERE e.d_id=d.id AND e.id=#{id} </select>

通过级联属性方式

<result column="did" property="dept.id"/> <result column="dept_name" property="dept.departmentName"/>

(二)使用关键字 association嵌套定义关联的单个对象的封装规则

<!-- 使用association定义关联的单个对象的封装规则; --> <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp2"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="gender" property="gender"/> <!-- association可以指定联合的javaBean对象 property="dept":指定哪个属性是联合的对象 javaType:指定这个属性对象的类型[不能省略] --> <association property="dept" javaType="com.atguigu.mybatis.bean.Department"> <id column="did" property="id"/> <result column="dept_name" property="departmentName"/> </association> </resultMap>

(三)使用association进行分步查询:

<!-- 使用association进行分步查询: 1、先按照员工id查询员工信息 2、根据查询员工信息中的d_id值去部门表查出部门信息 3、部门设置到员工中; --> <!-- id last_name email gender d_id --> <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpByStep"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!-- association定义关联对象的封装规则 select:表明当前属性是调用select指定的方法查出的结果 column:指定将哪一列的值传给这个方法 流程:使用select指定的方法(传入column指定的这列参数的值)查出对象, 并封装给property指定的属性 --> <association property="dept" select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById" column="d_id"> </association> </resultMap>

(四)使用association进行分步查询延迟加载

可以使用延迟加载(懒加载)或者按需加载; 需要在全局中配置规则以下规则即可:

<settings> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> </settings>

1.3.2 collection定义关联集合封装规则

一个部门可以有多个员工

public class Department { private Integer id; private String departmentName; private List<Employee> emps; …… }

映射规则

<!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 --> <resultMap type="com.atguigu.mybatis.bean.Department" id="MyDept"> <id column="did" property="id"/> <result column="dept_name" property="departmentName"/> <!-- collection:定义关联集合类型的属性的封装规则 ofType: 指定集合里面元素的类型 --> <collection property="emps" ofType="com.atguigu.mybatis.bean.Employee"> <!-- 定义这个集合中元素的封装规则 --> <id column="eid" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </collection> </resultMap> <!-- 引用resultMap --> <!-- public Department getDeptByIdPlus(Integer id); --> <select id="getDeptByIdPlus" resultMap="MyDept"> SELECT d.id did,d.dept_name dept_name, e.id eid,e.last_name last_name,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>

分步查询

<!-- collection:分段查询 --> <resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep"> <id column="id" property="id"/> <id column="dept_name" property="departmentName"/> <collection property="emps" select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId" column="{deptId=id}" fetchType="lazy"></collection> </resultMap> <!-- public Department getDeptByIdStep(Integer id); --> <select id="getDeptByIdStep" resultMap="MyDeptStep"> select id,dept_name from tbl_dept where id=#{id} </select>

扩展:如果想将多列的值传递过去, 则将多列的值封装map传递;格式如下:

column="{key1=column1,key2=column2}"

fetchType="lazy":表示使用延迟加载; lazy:延迟; eager:立即


1.3.3 鉴别器

<!-- <discriminator javaType=""></discriminator> 鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为 封装Employee: 如果查出的是女生:就把部门信息查询出来,否则不查询; 如果是男生,把last_name这一列的值赋值给email; --> <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpDis"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!-- column:指定判定的列名 javaType:列值对应的java类型 ;必须要有 --> <discriminator javaType="string" column="gender"> <!--女生 resultType:指定封装的结果类型;不能缺少。/或者使用resultMap类型--> <case value="0" resultType="com.atguigu.mybatis.bean.Employee"> <association property="dept" select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById" column="d_id"> </association> </case> <!--男生 ;如果是男生,把last_name这一列的值赋值给email; --> <case value="1" resultType="com.atguigu.mybatis.bean.Employee"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="last_name" property="email"/> <result column="gender" property="gender"/> </case> </discriminator> </resultMap>
转载请注明原文地址: https://www.6miu.com/read-2625504.html

最新回复(0)