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学习总结_13_Mybatis查询之resultMap和resultType区别的更多相关文章

  1. MyBatis学习总结_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. 查mysql字段中的数字记录

    select * from a where nameregexp '^[0-9]+$' ;

  2. 阿里云服务器mysql修改编码问题

    最近在学习struts+spring+hibernate,强烈推荐新手一本书:陈天河<轻量级web应用开发>,这本书是我见过的国内最好的书,初学者可以买本读读. 不说这个了,来说说我的问题 ...

  3. 用sql语句生成 数据字典

    SELECT 表名=case when a.colorder=1 then d.name else '' end, 表说明=case when a.colorder=1 then isnull(f.v ...

  4. sql数据库的表连接方式图文详解

    sql数据库表连接,主要分为:内连接.外连接(左连接.右连接 .全连接).交叉连接,今天统一整合一下,看看他们的区别.   首先建表填充值. 学生表:student(id,姓名,年龄,性别 ) 成绩表 ...

  5. Javascript对象的创建模式 -- 深入了解Javascript

    /* 一.模式1:命名空间(namespace) 优点:减少全局命名所需的数量,避免命名冲突或过度 */ // 更简洁的方式 var MYAPP = MYAPP || {}; //定义通用方法 MYA ...

  6. python 单元测试-unittest

    参考资料:https://docs.python.org/3.4/library/unittest.html#module-unittest 一张图解决问题: 涉及5块内容:case.suite.lo ...

  7. SqlServer Split函数

    Create FUNCTION [dbo].[SplitToTable] ( @SplitString nvarchar(max), @Separator nvarchar(10)=' ' ) RET ...

  8. jQuery(Keep for myself)

    jQuery API : http://www.w3cschool.cc/manual/jquery/ 1. jQuery是一个JavaScript函数库. jQuery是一个轻量级的"写的 ...

  9. Least Common Ancestors 分类: ACM TYPE 2014-10-19 11:24 84人阅读 评论(0) 收藏

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  10. 关于WSDL

    Message Operation 最核心的在于Operation 只要关心Operation就可以了,Operation只有Input, Output没有其他内容,是相对固定的.只要关心一下Inpu ...