博客
关于我
06 sql映射之返回值类型(resultType)
阅读量:232 次
发布时间:2019-03-01

本文共 1439 字,大约阅读时间需要 4 分钟。

文章目录

resultType属性就是指定返回值类型,这一章节主要介绍resultType属性

1 返回集合

如果返回值是一个集合,resultType并非指定为集合类型,而是指定为集合内元素的类型

比如:

List         selectAll();
select       *   from       tb_user

测试:

@Testpublic void test01() {       List         list = tbUserMapper.selectAll();    System.out.println(list);}

2 返回Map

  • key就是列名,value就是对应的数据,resultType指定为map
  • 比如:

    Map         selectById(@Param("id") Long id);
    select        *    from        tb_user    where        id = #{id}

    测试:

    @Testpublic void test02() {       Map         map = tbUserMapper.selectById(1L);    System.out.println(map);}

    输出结果:

    {   password=123456, update_time=2019-04-04 22:58:26.0, create_time=2019-04-04 22:58:26.0, phone=13100001111, id=1, username=kobe}
    1. 返回的map,key是主键,value是这个主键对应的数据呢
      • resultType指定为map
      • 在mapper接口中使用@MapKey注解指定,返回map的key
    2. @MapKey("id")Map         selectMap();
      select       *   from       tb_user

      测试:

      @Testpublic void test03() {       Map         map = tbUserMapper.selectMap();    for (Long id : map.keySet()) {           System.out.println(id + "--->" + map.get(id));    }}

      输出:

      1--->{   password=123456, update_time=2019-04-04 22:58:26.0, create_time=2019-04-04 22:58:26.0, phone=13100001111, id=1, username=kobe}5--->{   password=123456, update_time=2021-01-31 11:44:22.0, create_time=2021-01-31 11:44:22.0, phone=13011112222, id=5, username=t-mac}6--->{   password=123456, update_time=2021-01-31 11:44:56.0, create_time=2021-01-31 11:44:56.0, phone=13011112222, id=6, username=t-mac}

    转载地址:http://wsrv.baihongyu.com/

    你可能感兴趣的文章
    MySQL-redo日志
    查看>>
    MySQL-【1】配置
    查看>>
    MySQL-【4】基本操作
    查看>>
    Mysql-丢失更新
    查看>>
    Mysql-事务阻塞
    查看>>
    Mysql-存储引擎
    查看>>
    mysql-开启慢查询&所有操作记录日志
    查看>>
    MySQL-数据目录
    查看>>
    MySQL-数据页的结构
    查看>>
    MySQL-架构篇
    查看>>
    MySQL-索引的分类(聚簇索引、二级索引、联合索引)
    查看>>
    Mysql-触发器及创建触发器失败原因
    查看>>
    MySQL-连接
    查看>>
    mysql-递归查询(二)
    查看>>
    MySQL5.1安装
    查看>>
    mysql5.5和5.6版本间的坑
    查看>>
    mysql5.5最简安装教程
    查看>>
    mysql5.6 TIME,DATETIME,TIMESTAMP
    查看>>
    mysql5.6.21重置数据库的root密码
    查看>>
    Mysql5.6主从复制-基于binlog
    查看>>