MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性,而当我们提供的返回类型是resultMap的时候,将数据库中列数据复制到对象的相应属性上,可以用于复制查询,两者不能同时用。

1、resultType

返回单个实例

<select id="selectUser" parameterType="int" resultType="User">

select * from user where id = #{id}

</select>
返回List集合

<select id="selectUserAll" resultType="User" > <!-- resultMap="userMap" -->
select * from user
</select>

2、resultMap

简单查询:

<resultMap type="User" id="userMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
</resultMap>
column:数据库中列名称,property:类中属性名称

 

resultMap:适合使用返回值是自定义实体类的情况

resultType:适合使用返回值得数据类型是非自定义的,即jdk的提供的类型

resultMap : 

映射实体类的数据类型

resultMap的唯一标识

column: 库表的字段名

property: 实体类里的属性名

配置映射文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:当前库表映射文件的命名空间,唯一的不能重复 -->
<mapper namespace="com.hao947.sql.mapper.PersonMapper">
<!-- type:映射实体类的数据类型 id:resultMap的唯一标识 -->
<resultMap type="person" id="BaseResultMap">
<!-- column:库表的字段名 property:实体类里的属性名 -->
<id column="person_id" property="personId" />
<result column="name" property="name" />
<result column="gender" property="gender" />
<result column="person_addr" property="personAddr" />
<result column="birthday" property="birthday" />
</resultMap>
<!--id:当前sql的唯一标识
parameterType:输入参数的数据类型
resultType:返回值的数据类型
#{}:用来接受参数的,如果是传递一个参数#{id}内容任意,如果是多个参数就有一定的规则,采用的是预编译的形式select
* from person p where p.id = ? ,安全性很高 --> <!-- sql语句返回值类型使用resultMap -->
<select id="selectPersonById" parameterType="java.lang.Integer"
resultMap="BaseResultMap">
select * from person p where p.person_id = #{id}
</select>
<!-- resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型 -->
<select id="selectPersonCount" resultType="java.lang.Integer">
select count(*) from
person
</select> <select id="selectPersonByIdWithMap" parameterType="java.lang.Integer"
resultType="java.util.Map">
select * from person p where p.person_id= #{id}
</select> </mapper>

实体类Person.Java

<pre name="code" class="java">package com.hao947.model;
import java.util.Date;
public class Person {
private Integer personId;
private String name;
private Integer gender;
private String personAddr;
private Date birthday;
@Override
public String toString() {
return "Person [personId=" + personId + ", name=" + name + ", gender="
+ gender + ", personAddr=" + personAddr + ", birthday="
+ birthday + "]";
}
}

测试类

public class PersonTest {
SqlSessionFactory sqlSessionFactory;
@Before
public void setUp() throws Exception {
// 读取资源流
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
// 初始化session工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
} @Test
public void selectPersonById() {
// 创建一个sqlsession
SqlSession session = sqlSessionFactory.openSession();
try {
Person p = session.selectOne(
"com.hao947.sql.mapper.PersonMapper.selectPersonById", 1);
System.out.println(p);
} finally {
session.close();
}
}
@Test
public void selectPersonCount() {
// 创建一个sqlsession
SqlSession session = sqlSessionFactory.openSession();
try {
Integer p = session.selectOne(
"com.hao947.sql.mapper.PersonMapper.selectPersonCount");
System.out.println(p);
} finally {
session.close();
}
}
@Test
public void selectPersonByIdWithMap() {
// 创建一个sqlsession
SqlSession session = sqlSessionFactory.openSession();
try {
Map<String ,Object> map = session.selectOne(
"com.hao947.sql.mapper.PersonMapper.selectPersonByIdWithMap",1);
System.out.println(map);
} finally {
session.close();
}
}
}

MyBatis学习总结_Mybatis查询之resultMap和resultType区别的更多相关文章

  1. MyBatis学习总结_13_Mybatis查询之resultMap和resultType区别

    MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性 ...

  2. MyBatis学习总结(13)——Mybatis查询之resultMap和resultType区别

    MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性 ...

  3. MyBatis3系列__05查询补充&resultMap与resultType区别

    1.查询补充 当你查询一条记录并且是简单查询时,情况相对简单,可以参考以下的例子: public Employee getEmpById(Integer id); 对应的xml文件中: <sel ...

  4. mybatis学习笔记(14)-查询缓存之中的一个级缓存

    mybatis学习笔记(14)-查询缓存之中的一个级缓存 标签: mybatis mybatis学习笔记14-查询缓存之中的一个级缓存 查询缓存 一级缓存 一级缓存工作原理 一级缓存測试 一级缓存应用 ...

  5. Mybatis中resultMap与resultType区别

    MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultM ...

  6. mybatis多表关联查询之resultMap单个对象

    resultMap的n+1方式实现多表查询(多对一) 实体类 创建班级类(Clazz)和学生类(Student),并在Student中添加一个Clazz类型的属性,用于表示学生的班级信息. mappe ...

  7. Mybatis学习笔记-复杂查询

    多个学生,对应一个老师 对于学生而言,关联:多个学生关联一个老师[多对一] 对于老师而言,集合:一个老师,有多个学生[一对多] 复杂查询环境搭建 数据库搭建 CREATE TABLE `teacher ...

  8. Mybatis学习记录(四)----resultMap的使用

    resultMap使用方法 如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系. 1.定义resultMap 2.使用resultMap ...

  9. Mybatis学习笔记导航

    Mybatis小白快速入门 简介 本人是一个Java学习者,最近才开始在博客园上分享自己的学习经验,同时帮助那些想要学习的uu们,相关学习视频在小破站的狂神说,狂神真的是我学习到现在觉得最GAN的老师 ...

随机推荐

  1. python高级-生成器(17)

    1. 什么是⽣成器 通过列表⽣成式,我们可以直接创建⼀个列表.但是,受到内存限制,列表容量肯定是有限的.⽽且,创建⼀个包含100万个元素的列表,不仅占⽤很⼤的存储空间,如果我们仅仅需要访问前⾯⼏个元素 ...

  2. 【Spark篇】---Spark中Action算子

    一.前述 Action类算子也是一类算子(函数)叫做行动算子,如foreach,collect,count等.Transformations类算子是延迟执行,Action类算子是触发执行.一个appl ...

  3. 12.Flask-Restful

    定义Restful的视图 安装:pip install flask-restful 如果使用Flask-restful,那么定义视图函数的时候,就要继承flask_restful.Resourse类, ...

  4. 火狐l浏览器所有版本

    最近在群里看到好多人需要火狐浏览器的低版本,而且都是跪求,更有甚者是高额悬赏,因此给大家一个链接,免费的,免费的,免费的!!!重要的事说三遍,拿走不谢~~ 火狐所有版本,了解一下,有需要的自行下载. ...

  5. AspNetCore 使用NLog日志,NLog是基于.NET平台开的类库!(又一神器)

    NLog是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码. NLog是一个简单灵活的.NET日志记录类库.通过使用NLog,我们可以在任何一种.NET语言中 ...

  6. bootstrap4的出现(或这篇文章可以叫做bs4与bs3的区别)

    前言:在bootstrap4出现之后修改了bootstrap3的不方便之处,让使用框架的前端开发者更加便捷..(bootstrap下文中简称为bs) 一.栅格系统 相对于原来的bs3,bs4具有了范围 ...

  7. curl 错误总结

    证书域名与访问的域名不一致 问题:curl SSL: certificate subject name 'luffichen_server.tencent.com' does not match ta ...

  8. 基于 Nginx 的 HTTPS 性能优化实践

    前言 分享一个卓见云的较多客户遇到HTTPS优化案例. 随着相关浏览器对HTTP协议的“不安全”.红色页面警告等严格措施的出台,以及向 iOS 应用的 ATS 要求和微信.支付宝小程序强制 HTTPS ...

  9. Spring Boot 2.x(九):遇到跨域不再慌

    什么是跨域 首先,我们需要了解一下一个URL是怎么组成的: // 协议 + 域名(子域名 + 主域名) + 端口号 + 资源地址 http: + // + www.baidu.com + :8080/ ...

  10. SpringBoot系列——Spring-Data-JPA(升级版)

    前言 在上篇博客中:SpringBoot系列——Spring-Data-JPA:https://www.cnblogs.com/huanzi-qch/p/9970545.html,我们实现了单表的基础 ...