resultMap自定义结果集
public Employee getEmpById(Integer id);
<!-- resultMap:自定义某个javaBean的封装规则
type:自定义规则的java类型
id:唯一id,方便引用
-->
<resultMap id="MyEmp" type="com.cn.bean.Employee">
<!--指定主键列的封装规则
column:指定那一列
property:对应的javaBean属性
-->
<id column="id" property="id"/>
<!--result定义普通列
不指定的列会自动封装,但一般只要写resultMap,所有映射规则都写上
-->
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<result column="last_name" property="lastName"/>
</resultMap>
<select id="getEmpById" resultMap="MyEmp">
delete from tb_employee where id = #{id}
</select>
association关联查询
查询Employee的同时查询员工对应的部门,一个员工有与之对应的部门信息。
Department 实体类:
public class Department {
private Integer id;
private String departmentName;
//
Employee 实体类:
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
private Department dept;
关联查询在resultMap中定义规则时可以使用两种方法,一种是级联查询,一种是使用association定义单个对象的封装规则。
public Employee getEmpAndDept(Integer id);
<resultMap id="MyDifEmp" type="com.cn.bean.Employee">
<id column="id" property="id"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<result column="