输出映射
接下来说说有关Mapper.xml配置文件中查询标签中关于返回值类型resultType与resultMap的一些内容
1.resultType
使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。
如果查询出来的列名和pojo中的属性名全部不一致,没有创建pojo对象。
只要查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象。
1.1输出简单类型
1.1.1需求
用户信息的综合查询列表总数,通过查询总数和上边用户综合查询列表才可以实现分页。
1.1.2mapper.xml
<mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper"> <!-- 用户信息综合查询 #{UserCustom.sex}取出包装对象中性别值 ${UserCustom.username}取得pojo包装对象中用户名称 --> <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="cn.edu.hpu.mybatis.PO.UserCustom"> select * from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%' </select> <!-- 用户信息综合查询总数 --> <select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int"> select count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%' </select> ......</mapper>
1.1.3mapper.java
//用户管理的Dao接口public interface UserMapper { //用户信息综合查询 public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception; //用户信息综合查询总数 public int findUserCount(UserQueryVo userQueryVo) throws Exception; ......}
1.1.4测试代码
//用户信息综合查询总数 @Test public void testFindUserCount() throws Exception{ SqlSession sqlSession=sqlSessionFactory.openSession(); //创建UserMapper代理对象 UserMapper userMapper=sqlSession.getMapper(UserMapper.class); //创建包装对象,设置查询条件 UserQueryVo userQueryVo=new UserQueryVo(); UserCustom userCustom=new UserCustom(); userCustom.setSex("男"); userCustom.setUsername("张三"); userQueryVo.setUserCustom(userCustom); //调用userMapper的方法 int count=userMapper.findUserCount(userQueryVo); System.out.println("总数为:"+count); }
测试结果:
总数为:2
输出日志:
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 7832149.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@778255]
DEBUG [main] - ==> Preparing: select count(*) from user where user.sex=? and user.username like '%张三%'
DEBUG [main] - ==> Parameters: 男(String)
DEBUG [main] - <== Total: 1
1.1.5小结
查询出来的结果集只有一行且一列,可以使用简单类型进行输出映射。(输出简单类型的要求)
1.2输出pojo对象和pojo列表
不管是输出的pojo单个对象还是一个列表(list中包括pojo),在mapper.xml中resultType指定的类型是一样的。
在mapper.java指定的方法返回值类型不一样:
(1)输出单个pojo对象,方法返回值是单个对象类型
(2)输出pojo对象list,方法返回值是List<Pojo>
生成的动态代理对象中是根据mapper方法的返回值类型确定是调用selectOne(返回单个对象调用)还是selectList (返回集合对象调用 ).
(3)输出hashmap
输出pojo对象可以改用hashmap输出类型,将输出的字段名称作为map的key,value为字段值。如果是集合,那就是list里面套了HashMap。
2.resultMap
mybatis中使用resultMap完成高级输出结果映射。
2.1resultMap使用方法
如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。
下面来进行实验,实验需求
2.2将下边的sql使用User完成映射
SELECT id id_,username username_ FROM USER WHERE id=#{value}
User类中属性名和上边查询列名不一致。
resultMap使用方法:(一下属性均定义在Mapper.xml映射文件中)
(1)定义resultMap
<!-- 定义resultType将select id id_,username _username from user和User类中的属性做一个映射关系 type:resultMap最终所映射的Java对象类型,可以使用别名id:对resultMap的唯一标识 --><resultMap type="user" id="userResultMap"> <!-- id表示查询结果集中唯一标识 column:查询出的列名 property:type所指定的POJO中的属性名 最终reslutMap对column和property做一个映射关系(对应关系) --> <id column="_id" property="id"/> <!-- 对普通列的映射定义 --> <result column="_username" property="username"/></resultMap>
(2)使用resultMap作为statement的输出映射类型
<!-- 使用resultMap进行输出映射 resultMap:指定定义的resultMap的id,如果这个resultMap在其它的mapper文件,前面需要加namespace --><select id="findUserByResultMap" parameterType="int" resultMap="userResultMap"> select id _id,username _username from user where id=#{value}</select>
(3)mapper接口类中添加相应方法
//用户管理的Dao接口public interface UserMapper { public User findUserByResultMap(int id) throws Exception; ......}
测试:
@Testpublic void testFindUserByResultMap() throws Exception{ SqlSession sqlSession=sqlSessionFactory.openSession(); //创建UserMapper代理对象 UserMapper userMapper=sqlSession.getMapper(UserMapper.class); //调用userMapper的方法 User user=userMapper.findUserByResultMap(1); System.out.println(user.getUsername());}
测试结果:
张三
输出日志:
EBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1465214.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@165b7e]
DEBUG [main] - ==> Preparing: select id _id,username _username from user where id=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
小结
使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。
如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。
转载请注明出处:http://blog.csdn.net/acmman/article/details/46509375
Original url: Access
Created at: 2019-04-12 17:08:20
Category: default
Tags: none
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
java windows火焰图_mob64ca12ec8020的技术博客_51CTO博客 - 在windows下不可行,不知道作者是怎样搞的 监听SpringBoot 服务启动成功事件并打印信息_监听springboot启动完毕-CSDN博客 SpringBoot中就绪探针和存活探针_management.endpoint.health.probes.enabled-CSDN博客 u2u转换板 - 嘉立创EDA开源硬件平台 Spring Boot 项目的轻量级 HTTP 客户端 retrofit 框架,快来试试它!_Java精选-CSDN博客 手把手教你打造一套最牛的知识笔记管理系统! - 知乎 - 想法有重合-理论可参考 安宇雨 闲鱼 机械键盘 客制化 开贴记录 文本 linux 使用find命令查找包含某字符串的文件_beijihukk的博客-CSDN博客_find 查找字符串 ---- mac 也适用 安宇雨 打字音 记录集合 B站 bilibili 自行搭建 开坑 真正的客制化 安宇雨 黑苹果开坑 查找工具包maven pom 引用地 工具网站 Dantelis 介绍的玩轴入坑攻略 --- 关于轴的一些说法 --- 非官方 ---- 心得而已 --- 长期开坑更新 [本人问题][新开坑位]关于自动化测试的工具与平台应用 机械键盘 开团 网站记录 -- 能做一个收集的程序就好了 不过现在没时间 -- 信息大多是在群里发的 - 你要让垃圾佬 都去一个地方看难度也是很大的 精神支柱 [超级前台]sprinbboot maven superdesk-app 记录 [信息有用] [环境准备] [基本完成] [sebp/elk] 给已创建的Docker容器增加新的端口映射 - qq_30599553的博客 - CSDN博客 [正在研究] Elasticsearch, Logstash, Kibana (ELK) Docker image documentation elasticsearch centos 安装记录 及 启动手记 正式服务器 39 elasticsearch 问题合集 不断更新 6.1.1 | 6.5.1 两个版本 博客程序 - 测试 - bug记录 等等问题 laravel的启动过程解析 - lpfuture - 博客园 OAuth2 Server PHP 用 Laravel 搭建带 OAuth2 验证的 RESTful 服务 | Laravel China 社区 - 高品质的 Laravel 和 PHP 开发者社区 利用Laravel 搭建oauth2 API接口 附 Unauthenticated 解决办法 - 煮茶的博客 - SegmentFault 思否 使用 OAuth2-Server-php 搭建 OAuth2 Server - 午时的海 - 博客园 基于PHP构建OAuth 2.0 服务端 认证平台 - Endv - 博客园 Laravel 的 Artisan 命令行工具 Laravel 的文件系统和云存储功能集成 浅谈Chromium中的设计模式--终--Observer模式 浅谈Chromium中的设计模式--二--pre/post和Delegate模式 浅谈Chromium中的设计模式--一--Chromium中模块分层和进程模型 DeepMind 4 Hacking Yourself README.md update 20211011
Laravel China 简书 知乎 博客园 CSDN博客 开源中国 Go Further Ryan是菜鸟 | LNMP技术栈笔记 云栖社区-阿里云 Netflix技术博客 Techie Delight Linkedin技术博客 Dropbox技术博客 Facebook技术博客 淘宝中间件团队 美团技术博客 360技术博客 古巷博客 - 一个专注于分享的不正常博客 软件测试知识传播 - 测试窝 有赞技术团队 阮一峰 语雀 静觅丨崔庆才的个人博客 软件测试从业者综合能力提升 - isTester IBM Java 开发 使用开放 Java 生态系统开发现代应用程序 pengdai 一个强大的博主 HTML5资源教程 | 分享HTML5开发资源和开发教程 蘑菇博客 - 专注于技术分享的博客平台 个人博客-leapMie 流星007 CSDN博客 - 舍其小伙伴 稀土掘金 Go 技术论坛 | Golang / Go 语言中国知识社区
最新评论