表的字段名和实体类的属性名不一致
表的字段名和实体类的属性名,set方法名(去掉set)都不一样的话,则不能封装数据
二者有一个能和表的字段一样,就可以封装数据
<!-- 可以通过设置别名 -->
<!--List<Emp> getAllEmp();-->
<select id="getAllEmp" resultType="Emp">
select eid, emp_name as "empName", age from emp
</select>
<!--核心配置文件中进行全局配置-->
<settings>
<!--将_自动映射为驼峰-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="cn.domain"/>
</typeAliases>
<!--id是标识,type是实体类-->
<resultMap id="empResultMap" type="Emp">
<!--property是实体类属性名,column是表字段名-->
<!--主键字段的映射关系-->
<id property="eid" column="eid"></id>
<!--普通字段的映射关系-->
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
</resultMap>
<select id="getAllEmp" resultMap="empResultMap">
select * from emp
</select>
解决多对一的问题
第一种方式
# 多个员工对应一个部门
# 在多的这一方设置一的主键(did)
create table if not exists t_emp(
eid int primary key auto_increment,
emp_name varchar(10),
age int,
did int
);
create table if not exists t_dept(
did int primary key auto_increment,
dept_name varchar(10)
);
// 多个员工对应一个部门
public class Emp {
private Integer eid;
private String empName;
private Integer age;
private Dept dept;
}
public class Dept {
private Integer did;
private String deptName;
}
Emp empAndDept = mapper.getEmpAndDept(2);
System.out.println(empAndDept);
// Emp(eid=2, empName=lisi, age=34, dept=Dept(did=2, deptName=研发部))
<resultMap id="empAndDept" type="Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="dept.did" column="did"></result>
<result property="dept.deptName" column="dept_name"></result>
</resultMap>
<!--Emp getEmpAndDept(Integer eid);-->
<select id="getEmpAndDept" resultMap="empAndDept">
select * from t_emp
left join t_dept on t_emp.did = t_dept.did
where t_emp.eid = #{eid}
</select>
第二种方式
<resultMap id="empAndDept" type="Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<!--Emp类中的dept属性-->
<!--association:处理多对一的映射关系-->
<!--property:需要处理的属性名-->
<!--javaType:该属性的类型-->
<association property="dept" javaType="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
</association>
</resultMap>
第三种方式(了解即可,有点复杂)
<!--分步查询-->
<resultMap id="empAndDept" type="Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<!--使用getDeptInfo查询部门信息,传给dept属性-->
<!--参数是第一步sql查询出来did字段的值-->
<association property="dept"
select="cn.mapper.UserMapper.getDeptInfo"
column="did"></association>
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDept">
select * from t_emp where eid = #{eid}
</select>
<select id="getDeptInfo" resultType="Dept">
select * from t_dept where did = #{did}
</select>
分步查询可以实现延迟加载,如果只访问员工信息,只会执行第一条sql
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
Emp empAndDept = mapper.getEmpAndDept(1);
System.out.println(empAndDept.getEmpName()); // 此时只会执行第一条sql
解决一对多的问题
public class Dept {
private Integer did;
private String deptName;
private List<Emp> emps; // 一个部门对应多个员工
}
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept deptAndEmp = mapper.getDeptAndEmp(1);
System.out.println(deptAndEmp);
// Dept(did=1, deptName=销售部, emps=[Emp(eid=1, empName=zhangsan, age=12, dept=null), Emp(eid=3, empName=wangwu, age=23, dept=null)])
<resultMap id="deptAndEmp" type="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<!--ofType是集合中的泛型-->
<collection property="emps" ofType="Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
</collection>
</resultMap>
<!--Dept getDeptAndEmp(Integer did);-->
<select id="getDeptAndEmp" resultMap="deptAndEmp">
select * from t_dept
left join t_emp on t_dept.did = t_emp.did
where t_dept.did = #{did}
</select>
第二种方式
<!--分步查询-->
<resultMap id="deptAndEmp" type="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<collection property="emps"
select="cn.mapper.DeptMapper.getEmp"
column="did"></collection>
</resultMap>
<!--Dept getDeptAndEmp(Integer did);-->
<select id="getDeptAndEmp" resultMap="deptAndEmp">
select * from t_dept where did = #{did}
</select>
<!--List<Emp> getEmp(Integer did);-->
<select id="getEmp" resultType="Emp">
select * from t_emp where did = #{did}
</select>