1:解决数据表字段名和实体类属性名不一致问题
方式1:设置数据表字段别名,别名为实体类属性名
emp_name -> empName
select id,emp_name empName,age,sex,email from t_emp
方式2:通过MyBatis全局配置开启驼峰转换
比如:emp_name->empName
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
方式3:使用resultMap,自定义结果集
- resultMap的id:resultMap唯一标识
- type:对应实体类
- id:设置主键映射关系
- result:设置普通字段映射关系
- property:实体类的属性名
- column:数据库的字段名
<resultMap id="empResultMap" type="Emp">
<id property="id" column="id"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
</resultMap>
<select id="selectAll" resultMap="empResultMap">
select * from t_emp
</select>
2:如何解决多对一关系
场景:比如许多员工属于同一个部门内,如何通过查询员工信息,并且得到部门信息。
员工类:
Dept是部门类
public class Emp {
private Integer id;
private String empName;
private Integer age;
private String sex;
private String email;
private Dept dept;//多对一,设置一的对象
}
方式1:级联属性赋值
a:员工接口的方法:
//查询员工和员工对应的部门
Emp getEmpAndDept(@Param("eid") Integer id);
b:员工接口的映射:连接员工和部门表,员工的部门id=部门id
<select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">
select * from t_emp left join t_dept
on t_emp.did=t_dept.did where t_emp.id=#{eid}
</select>
c:ResultMap
通过dept.属性 和 字段 进行匹配
<!--级联属性赋值-->
<resultMap id="empAndDeptResultMapOne" type="Emp">
<id property="id" column="id"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<result property="dept.did" column="did"/>
<result property="dept.deptName" column="dept_name"/>
</resultMap>
方式2:通过association实现
方法和映射同上,需要更改的是ResultMap:
- association:处理多对一的映射关系
- property:多对一关系的属性
- javaType:该属性的类型
<resultMap id="empAndDeptResultMapTwo" type="Emp">
<id property="id" column="id"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<association property="dept" javaType="Dept">
<id property="did" column="did"/>
<result property="deptName" column="dept_name"/>
</association>
</resultMap>
方式3:分布查询
我们可以通过员工id查询到员工的所有信息,这其中包含了部门的did,再通过部门的did查询到部门的信息。
1:分布查询第一步:
a:员工接口的方法:
//分布查询第一步,查询员工信息
Emp getEmpAndDeptByStepOne(@Param("eid") Integer id);
b:员工接口的映射:
<!--分布查询第一步,查询员工信息-->
<select id="getEmpAndDeptByStepOne" resultMap="getEmpAndDeptByStepResultMap">
select * from t_emp where id=#{eid}
</select>
2:分布查询第二步
a:部门接口的方法:
//分布查询第二步,以did作为条件查询部门信息
Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
b:部门接口的映射:
<!--分布查询第二步,以did作为条件查询部门信息-->
<resultMap id="deptResultMap" type="Dept">
<id property="did" column="did"/>
<result property="deptName" column="dept_name"/>
</resultMap>
<select id="getEmpAndDeptByStepTwo" resultMap="deptResultMap">
select * from t_dept where did=#{did}
</select>
3:分布查询第三步ResultMap
员工接口映射文件的ResultMap:
- association:引用第二个查询
- property:需要处理的分布查询的属性
- select:使用nameSpace+方法id唯一标识
- column:第一个查询的结果字段,作为第二个查询的条件
<resultMap id="getEmpAndDeptByStepResultMap" type="Emp">
<id property="id" column="id"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<association property="dept"
select="MapperInterface.DeptMapper.getEmpAndDeptByStepTwo"
column="did" fetchType="lazy">
</association>
</resultMap>
3:如何解决一对多关系
场景:查询部门信息,里面包含许多部门员工的信息
部门类:
Emp是员工类
public class Dept {
private Integer did;
private String deptName;
private List<Emp> emps;//一对多为集合
}
方式1:collection实现
a:部门接口的方法:
//获取部门和部门员工信息
Dept getDeptAndEmpByDid(@Param("did") Integer did);
b:部门接口的映射:通过部门did=员工did查询
<!--获取部门和部门员工信息-->
<select id="getDeptAndEmpByDid" resultMap="deptAndEmpResultMap" >
select * from t_dept left join t_emp
on t_dept.did=t_emp.did
where t_dept.did=#{did}
</select
c:ResultMap:
- collection:处理一对多的映射关系
- property:需要处理的属性
- ofType:该属性对应的集合的数据类型
<resultMap id="deptAndEmpResultMap" type="Dept">
<id property="did" column="did"/>
<result property="deptName" column="dept_name"/>
<collection property="emps" ofType="Emp">
<id property="id" column="id"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
</collection>
</resultMap>
方式2:分布查询
先查找部门信息,再根据部门的did查找相应的员工。
1:分布查询第一步:
a:部门接口的方法:
//分布查询第一步:查询部门信息
Dept getDeptAndEmpByStepOne(@Param("did") Integer did);
b:部门接口的映射
<!--分布查询第一步:查询部门信息-->
<select id="getDeptAndEmpByStepOne" resultMap="DeptAndEmpByStepResultMap">
select * from t_dept where did=#{did}
</select>
2:分布查询第二步:
a:员工接口的方法:
//分布查询第二步:以部门did作为条件查询员工信息
List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);
b:员工接口的映射:
<!--分布查询第二步:以部门did作为条件查询员工信息-->
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
select id,emp_name empName,age,sex,email from t_emp where did=#{did}
</select>
3:分布查询第三步ResultMap
<resultMap id="DeptAndEmpByStepResultMap" type="Dept">
<id property="did" column="did"/>
<result property="deptName" column="dept_name"/>
<collection property="emps"
select="MapperInterface.EmpMapper.getDeptAndEmpByStepTwo"
column="did"
>
</collection>
</resultMap>