Mybatis学习1-5——Select元素-resultMap

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="
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值