MyBatis SQL映射结果集封装

1.返回一个对象#

public interface EmployeeMapper {
    public Employee getEmpByMap(Map<String, Object> map);
}

对应的EmployeeMapper.xml语句

 <!-- public Employee getEmpByMap(Map<String, Object> map); -->
    <select id="getEmpByMap" resultType="com.atguigu.mybatis.bean.Employee">
        select * from ${tableName} where id=${id} and last_name=#{lastName}
    </select>

返回值为该对象的类型

2.返回一个集合#

public List<Employee> getEmpsByLastNameLike(String lastName);

对应的xml语句

   <!-- public Employee getEmpByMap(Map<String, Object> map); -->
    <select id="getEmpByMap" resultType="com.atguigu.mybatis.bean.Employee">
        select * from ${tableName} where id=${id} and last_name=#{lastName}
    </select>

返回值为集合中元素的类型

3.返回一个map集合#

//返回一条记录的map;key就是列名,值就是对应的值
public Map<String, Object> getEmpByIdReturnMap(Integer id);

对应的xml语句

select * from tbl_employee where id=#{id}

这里查找到tbl_employee中的一条记录,可以直接返回一个Employee对象,也可以直接返回一个map集合

此时map中的键就是列名,如id、last_name,值则就是该行记录中对应的值。

4.返回一个定制的map#

  //多条记录封装一个map:Map<Integer,Employee>:键是这条记录的主键,值是记录封装后的javaBean
    //@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key
    @MapKey("lastName")
    public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);

对应的xml语句

 <!--public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName);  -->
    <select id="getEmpByLastNameLikeReturnMap" resultType="com.atguigu.mybatis.bean.Employee">
        select * from tbl_employee where last_name like #{lastName}
    </select>

该sql查找到多条记录,其实也就对应着多个Employee,那如何封装成一个map集合,使其值为Employee,键为我们定制的值呢?

可以在查询语句中加上@MapKey(“lastName”),当然你也可以替换成主键id

以上这些其实都是resultType的用法

思考:表中的 一条记录通过restultType=“Employee”将结果与javaBean自动对应了起来,达到了自动映射的效果

当自动映射查询结果时,MyBatis 会获取结果中返回的列名并在 Java 类中查找相同名字的属性(忽略大小写)。 这意味着如果发现了 ID 列和 id 属性,MyBatis 会将列 ID 的值赋给 id 属性。

通常数据库列使用大写字母组成的单词命名,单词间用下划线分隔;而 Java 属性一般遵循驼峰命名法约定。为了在这两种命名方式之间启用自动映射,需要将 mapUnderscoreToCamelCase 设置为 true。

那如果返回的数据与javaBean不一致呢,比如我们关联查询了多张表,返回了各表中的部分字段,难道我们要往javaBean添加一些属性么?

那这里或许resultMap就能派上用场了,这是一种手动映射。

现在我们来看resultMap自定义结果集映射规则

// mapper接口与mapper.xml进行绑定
public interface EmployeeMapperPlus {
    
    public Employee getEmpById(Integer id);
    
    public Employee getEmpAndDept(Integer id);
    
    public Employee getEmpByIdStep(Integer id);
    
    public List<Employee> getEmpsByDeptId(Integer deptId);

}

以下为其基本用法:

  <!--自定义某个javaBean的封装规则
    type:自定义规则的Java类型
    id:唯一id方便引用
      -->
    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MySimpleEmp">
        <!--指定主键列的封装规则
        id定义主键会底层有优化;
        column:指定哪一列
        property:指定对应的javaBean属性
          -->
        <id column="id" property="id"/>
        <!-- 定义普通列封装规则 -->
        <result column="last_name" property="lastName"/>
        <!-- 其他不指定的列会自动封装:推荐我们只要写resultMap就把全部的映射规则都写上。 -->
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
    </resultMap>
    
    <!-- resultMap:自定义结果集映射规则;  -->
    <!-- public Employee getEmpById(Integer id); -->
    <select id="getEmpById"  resultMap="MySimpleEmp">
        select * from tbl_employee where id=#{id}
    </select>

场景一:#

查询Employee的同时查询员工对应的部门

 Employee===Department
  一个员工有与之对应的部门信息;
属性:   id  last_name  gender    d_id         did  dept_name (private Department dept;)
public class Employee {
    
    private Integer id;
    private String lastName;
    private String email;
    private String gender;
    private Department dept; //一个部门属性
}
public class Department {
    
    private Integer id;
    private String departmentName;
    private List<Employee> emps;
}

以下是sql

  <!--  public Employee getEmpAndDept(Integer id);-->
    <select id="getEmpAndDept" resultMap="MyDifEmp">
        SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
        d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
        WHERE e.d_id=d.id AND e.id=#{id}
    </select>

那如何封装resultMap?

可见resultMap支持:级联封装

还可以这么封装

 <!-- 
        使用association定义关联的单个对象的封装规则;
     -->
    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp2">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        
        <!--  association可以指定联合的javaBean对象
        property="dept":指定哪个属性是联合的对象
        javaType:指定这个属性对象的类型[不能省略]
        -->
        <association property="dept" javaType="com.atguigu.mybatis.bean.Department">
            <id column="did" property="id"/>
            <result column="dept_name" property="departmentName"/>
        </association>
    </resultMap>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值