Mybatis 实用篇返回值类型

Posted binarylei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis 实用篇返回值类型相关的知识,希望对你有一定的参考价值。

Mybatis 实用篇(四)返回值类型

一、返回 List、Map

List<User> getUsers();
<select id="getUsers" resultType="User">
    select * from user;
</select>

Map<String, Object> getUsers();
<select id="getUsers" resultType="map">
    select * from user;
</select>

二、返回指定的 key

    @MapKey("id")
    Map<Integer, User> getUsers();
    <select id="getUsers" resultType="User">
        select * from user;
    </select>

三、resultMap

mapUnderscoreToCamelCase=true 时可以自动将下划线转为驼峰规则,如果还不能满足要求就需要自定义返回类型。如下:

List<User> getUsers();
<select id="getUsers" resultMap="myMap">
    select u.id, u.name, u.did, d.name dname from user u, dept d where u.did=d.id;
</select>

<resultMap id="myMap" type="User">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="dept.id" column="did"/>
    <result property="dept.name" column="dname"/>
</resultMap>

3.1 association

association 可以将一个 java bean 对象 dept 封装到起来,如:

public class User {

    private int id;
    private String name;
    private DePartment dept;
}
<resultMap id="myMap" type="User">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <association property="dept" javaType="DePartment">
        <result property="id" column="did"/>
        <result property="name" column="dname"/>
    </association>
</resultMap>
<!-- association 可以分步查找 -->
<resultMap id="myMap" type="User">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <!-- 指定 select 语句的 id 和要传入的参数 id -->
    <association property="dept" javaType="DePartment" select="getDept" column="id"/>
</resultMap>

<select id="getUsers" resultMap="myMap">
    select id, name from user;
</select>
<select id="getDept" resultType="DePartment">
    select id, name from dept where id=#{id};
</select>

3.2 collection

collection 可以将 java bean 对象的 list 集合 users 封装到起来,如:

public class DePartment {

    private int id;
    private String name;
    private List<User> users;
}
<resultMap id="myMap" type="DePartment">
    <id property="id" column="did"/>
    <result property="name" column="dname"/>
    <collection property="users" ofType="User">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
    </collection>
</resultMap>
<select id="getDept" resultMap="myMap">
    select u.id, u.name, u.did, d.name dname from user u, dept d where u.did=d.id;
</select>

也可以分步查找,注意 N + 1 问题

<resultMap id="myMap" type="DePartment">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <collection property="users" ofType="User" select="getUsers" column="id"/>
</resultMap>
<select id="getUsers" resultType="User">
    select id, name from user where did=#{did};
</select>
<select id="getDept" resultMap="myMap">
    select id, name from dept;
</select>

注意:

  1. 支持多列传值:column="{key1=colum1, key2=colum2}"
  2. 分步查找支持缓存,可以配置属性 fetchType="eager(立即执行)/lazy(延迟加载)",也可以全局配置
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>

使用时如果用到了就会去查找,否则不会查找:

List<DePartment> depts = userMapper.getDept();
System.out.println(depts.get(0).getName());
System.out.println(depts.get(0).getUsers());

mybatis 查找时的 sql 如下,可以看到用到 dept 的 users 属性时才进行查找:

2018-09-06 06:50:30 DEBUG getDept9:159 - ==>  Preparing: select id, name from dept; 
2018-09-06 06:50:30 DEBUG getDept9:159 - ==> Parameters: 
2018-09-06 06:50:31 DEBUG getDept9:159 - <==      Total: 2
dev
2018-09-06 06:50:31 DEBUG getUsers9:159 - ==>  Preparing: select id, name from user where did=?; 
2018-09-06 06:50:31 DEBUG getUsers9:159 - ==> Parameters: 1(Integer)
2018-09-06 06:50:31 DEBUG getUsers9:159 - <==      Total: 2
[User{id=1, name=‘binarylei‘, age=0, sex=‘null‘}, User{id=3, name=‘binarylei3‘, age=0, sex=‘null‘}]

每天用心记录一点点。内容也许不重要,但习惯很重要!

以上是关于Mybatis 实用篇返回值类型的主要内容,如果未能解决你的问题,请参考以下文章

TypeHandler简介及配置(mybatis源码篇)

Mybatis--02

mybatis中查询结果为空时不同返回类型对应返回值

mybatis 返回一个类加一个等值int该怎么配置

python实战应用讲解-numpy专题篇实用小技巧(附python示例代码)

MyBatis中Mapper的返回值类型