实体类参考
public class Person {
private Integer pId;
private String pName;//当和表里的列名不一致的时候,查询语句应该起别名
//p_Name(原表里的列名) pname别名
private String email;
private Department dept;
public class Department {
private Integer dId;
private String dName;
public Department() {
}
注意每个字段名的对应
第一种(常规)
接口
public Person getPersonAndEmpById(Integer id);
配置
<resultMap id="myEmp" type="com.yiki.Entity.Person">
<id column="pId" property="pId"/>
<result column="pname" property="pName"/>
<result column="email" property="email"/>
<!--private Department dept-->
<result column="did" property="dept.dId"/>
<result column="dName" property="dept.dName"/>
</resultMap>
<select id="getPersonAndEmpById" resultMap="myEmp">
SELECT p.pid pid,p.p_name pname,p.email email,p.d_id did,d.dName dName
FROM person p,department d
WHERE p.d_id=d.dId AND p.pid=#{pId}
</select>
测试
@Test//级联属性测试
public void test2() throws IOException {
start();
Person person = mapper.getPersonAndEmpById(1);
System.out.println(person);
sqlSession.close();
}
第二种<association>
该标签定义单个对象的封装规则
<resultMap id="myEmp2"
type="com.yiki.Entity.Person">
<id column="pId" property="pId"/>
<result column="pname" property="pName"/>
<result column="email" property="email"/>
<!--private Department dept-->
<!--【association】可以指定联合Javabean对象
【javaType】指定联合属性的类型
-->
<association property="dept" javaType="com.yiki.Entity.Department">
<id column="did" property="dId"/>
<result column="dName" property="dName"/>
</association>
</resultMap>
<select id="getPersonAndEmpById" resultMap="myEmp2">
SELECT p.pid pid,p.p_name pname,p.email email,p.d_id did,d.dName dName
FROM person p,department d
WHERE p.d_id=d.dId AND p.pid=#{pId}
</select>
参考表
association分步查询
首先新建DepartmentMapper.xml并注册
并且写接口
package com.yiki.Dao;
import com.yiki.Entity.Department;
import org.apache.ibatis.annotations.Param;
public interface DepartmentMapper {
public Department getDeptById(Integer id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yiki.Dao.DepartmentMapper">
<select id="getDeptById" resultType="com.yiki.Entity.Department">
select * from Department where did = #{id}
</select>
</mapper>
这里是PersonMapper
<!--分步查询-->
<resultMap id="step" type="com.yiki.Entity.Person">
<id column="pId" property="pId"/>
<result column="p_name" property="pName"/>
<result column="email" property="email"/>
<!--private Department dept
【select】见写法
【column】指定将哪一列的值传给这个方法
注意:!!!这里column里写的应该是person表里的d_id而不是bean里或者emp表里的did-->
<association property="dept"
select="com.yiki.Dao.DepartmentMapper.getDeptById"
column="d_id"/>
</resultMap>
<select id="getPersonAndEmpByIdStep" resultMap="step">
select * from Person where pid=#{id}
</select>
测试
@Test//级联属性测试分步查询
public void test3() throws IOException {
start();
Person person = mapper.getPersonAndEmpByIdStep(1);
System.out.println(person);
DepartmentMapper mapper2 = sqlSession.getMapper(DepartmentMapper.class);
System.out.println(mapper2.getDeptById(1));
sqlSession.close();
}
可以看到发出了两天sql语句