zoukankan      html  css  js  c++  java
  • 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'}]
    

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

  • 相关阅读:
    通过SSIS监控远程服务器Windows服务并发送邮件报警!
    通过SSIS监控远程服务器磁盘空间并发送邮件报警!
    Jquery和雅虎的YQL服务实现天气预报功能!
    表字段或表名出现Mysql关键字或保留字导致问题 Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have
    Mybatis对象关系映射 one2one,one2many,many2many
    事务的传播属性及隔离级别 Spring
    正整数的二进制表示中1的个数计算(使用移位或者n&(n-1))
    char类型及ASCII码之间比较
    数据表记录包含表索引和数值(int范围的整数),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出
    写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。(多组同时输入 )
  • 原文地址:https://www.cnblogs.com/binarylei/p/9746984.html
Copyright © 2011-2022 走看看