MyBatis-<resultMap>
resultType可以把查询结果封装到pojo类型中,但必须pojo类的属性名和查询到的数据库表的字段名一致。如果sql查询到的字段与pojo的属性名不一致,则需要使用resultMap将字段名和属性名对应起来,进行手动配置封装,将结果映射到pojo中
<resultMap>
的参数列表:
id:对resultMap的引用名
type:封装的对象类型
使用<resultMap>
实体类
@Data
public class Employee implements Serializable {
public static final long serialVersionUID = 2L;
private long id;
private String lastName;
private String gender;
private String email;
private Dept dept;
}
@Data
public class Dept implements Serializable {
public static final long serialVersionUID = 1L;
private Integer id;
private String deptName;
private List<Employee> employeeList;
}
具体使用
id:主键封装,mybatis对主键有优化
property:pojo的属性
column:列名称
result:对普通列的封装,可以只封装pojo属性名和列名不相同的列,但建议全部封装
<resultMap id="employeeMap" type="com.lhx.pojo.Employee" >
<id property="id" column="id"/>
<result property="email" column="email"/>
<result property="lastName" column="last_name"/>
<result property="gender" column="gender"/>
</resultMap>
联表查询时可以采用的级联映射
<resultMap id="selectWithDeptMap" type="com.lhx.pojo.Employee">
<id property="id" column="eid"/>
<result property="email" column="email"/>
<result property="lastName" column="last_name"/>
<result property="gender" column="gender"/>
<!-- 联表查询,封装时可以采用级联封装实现-->
<result property="dept.id" column="did"/>
<result property="dept.deptName" column="dname"/>
</resultMap>
<resultMap>
中的association标签
association(一对一):映射到JavaBean的某个“复杂类型”属性,比如JavaBean类,即JavaBean,即JavaBean内部嵌套一个复杂数据类型(JavaBean)属性,这种情况就属于复杂类型的关联。
使用association进行关联单个对象映射规则
association:
property:属性名
javaType:java类型,全类名或是别名,不可省略
property:要封装的属性select:调用的其他接口的方法,应写为全类名
column:分步查询时调用其他方法传入的参数
<resultMap id="ass" type="com.lhx.pojo.Employee">
<id property="id" column="eid"/>
<result property="email" column="email"/>
<result property="lastName" column="last_name"/>
<result property="gender" column="gender"/>
<association property="dept" javaType="com.lhx.pojo.Dept">
<id property="id" column="did"/>
<result property="deptName" column="dname"/>
</association>
</resultMap>
使用association进行分步查询示例
<resultMap id="step" type="com.lhx.pojo.Employee">
<id property="id" column="eid"/>
<result property="email" column="email"/>
<result property="lastName" column="last_name"/>
<result property="gender" column="gender"/>
<!--
property:要封装的属性
select:调用的其他接口的方法,应写为全类名
column:调用其他方法传入的参数
-->
<association property="dept"
select="com.lhx.dao.DeptMapper.getDeptById"
column="did">
</association>
</resultMap>
<select id="selectStep" resultMap="step">
SELECT
e.`id` eid,
e.`last_name` last_name,
e.`email` email,
e.`gender` gender,
e.`dept_id` did
FROM
employee e
WHERE e.`id` = #{id}
</select>
<resultMap>
中的Collection标签
一对多的映射,多用作封装集合类型对象
利用Collection标签封装Dept类中Employee列表
<select id="getDeptInEmpList" resultMap="lsit">
SELECT
e.`id` eid,
e.`last_name` last_name,
e.`email` email,
e.`gender` gender,
d.`id` did,
d.`dept_name` dname
FROM
dept d
LEFT JOIN employee e
ON d.`id` = e.`dept_id`
WHERE d.`id` = #{id}
</select>
<!-- 定义集合封装规则-->
<resultMap id="lsit" type="Dept">
<id property="id" column="did"/>
<result property="deptName" column="dname"/>
<collection property="employeeList" ofType="employee">
<id property="id" column="eid"/>
<result property="gender" column="gender"/>
<result property="email" column="email"/>
<result property="lastName" column="last_name"/>
</collection>
</resultMap>