转载请标明出处:https://www.cnblogs.com/Dreamice/

首先,SQL语句执行后返回的结果可以使用 Map 存储,也可以使用 POJO 存储。

一、使用Map存储结果集

下面分别是映射文件以及测试文件代码,其他文件省略

通过resultType="Map"的定义,返回Map

<!-- 查询所有书籍信息:通过resultType="Map"的方式 -->
<!-- 查询所有书籍信息:通过resultType="Map"的方式 -->
<select id="selectAllBookByTypeMap" resultType="Map">
select * from book
</select>
@org.junit.Test
public void test1() {
@Autowired
private BookService bookService;
List<Map> books = bookService.selectAllBookByTypeMap();
System.out.println("通过resultType=\"Map\"的方式:");
System.out.println(books);
}

运行结果:

二、使用Pojo存储结果集

定义Pojo类,特意定义name1属性,为了和数据库中的字段名name不一致

Book.java

public class Book {
private Integer id;
private String name1;
private String author; //省略getter和setter函数
}

1、使用resultType="Map"

使用resultType="Map"进行查询时,可以通过使用别名的方式,保证自动映射到pojo类的属性名

   <!-- 查询所有书籍信息:通过resultType="*.*.pojo"的方式 -->
<select id="selectAllBookByTypePojo" resultType="com.dreamice.pojo.Book">
select id,name name1,author from book
</select>

2、使用resultMap

在映射文件中定义resultMap

<!-- 查询所有书籍信息:通过resultMap="*.*.pojo"的方式 -->
<select id="selectAllBookByRMPojo" resultMap="bookResultMap">
select * from book
</select> <!-- 定义resultMap-->
<resultMap id="bookResultMap" type="com.dreamice.pojo.Book">
<result property="name1" column="name"/>
</resultMap>

转载请标明出处:https://www.cnblogs.com/Dreamice/

@org.junit.Test
public void test1() {
@Autowired
private BookService bookService;
List<Book> booksPojo = bookService.selectAllBookByTypePojo();
System.out.println("通过resultType=\"*.*.pojo\"的方式:"); System.out.println(booksPojo);
System.out.println(booksPojo.get(0).getName1()); List<Book> booksRMPojo = bookService.selectAllBookByRMPojo();
System.out.println("通过resultMap的方式:");
System.out.println(booksRMPojo);
System.out.println(booksRMPojo.get(1).getName1());
System.out.println(booksRMPojo.get(1).getAuthor());
}

运行结果:

三、区别

所以,区别主要有:

1、查询结果为Map时,使用resultType;

2、简单查询且结果为Pojo类,也可以使用resultType,另外,查询字段名与Pojo属性名不一致,可以通过使用别名的方式;

3、复杂的映射或级联,可以使用resultMap;

转载请标明出处:https://www.cnblogs.com/Dreamice/

ResultMap和ResultType到底有什么区别?的更多相关文章

  1. ResultMap和ResultType在使用中的区别

    在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 resultType:当使 ...

  2. 转载:ResultMap和ResultType在使用中的区别

    在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 resultType:当使 ...

  3. ResultMap和ResultType在使用中的区别、MyBatis中Mapper的返回值类型

    在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 resultType:当使 ...

  4. mybatis的resultMap与resultType的区别

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

  5. mybatis中的resultMap与resultType、parameterMap与 parameterType的区别

    Map:映射:Type:Java类型 resultMap 与 resultType.parameterMap 与  parameterType的区别在面试的时候被问到的几率非常高,项目中出现了一个小b ...

  6. Mybatis使用时 resultMap与resultType、parameterMap与 parameterType的区别

    Map:映射:Type:Java类型  resultMap 与 resultType.parameterMap 与  parameterType的区别在面试的时候被问到的几率非常高,出现的次数到了令人 ...

  7. Mybatis中resultMap与resultType区别

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

  8. Mybatis第七篇【resultMap、resultType、延迟加载】

    resultMap 有的时候,我们看别的映射文件,可能看不到以下这么一段代码: <resultMap id="userListResultMap" type="us ...

  9. Union和Union All到底有什么区别

    以前一直不知道Union和Union All到底有什么区别,今天来好好的研究一下,网上查到的结果是下面这个样子,可是还是不是很理解,下面将自己亲自验证: Union:对两个结果集进行并集操作,不包括重 ...

随机推荐

  1. Using sudo inside a docker container

    https://stackoverflow.com/questions/25845538/using-sudo-inside-a-docker-container FROM ubuntu:12.04 ...

  2. Sublime Text 3 快捷键的汇总

    Sublime Text 3非常实用,但是想要用好,一些快捷键不可或缺,所以转了这个快捷键汇总. 选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按 ...

  3. Shell遍历目前下后缀名为.xml的文件并替换文件内容

    1.shell查找 .xml文件 find /home/esoon/test/external/ -type f -name '*.xml' 2.替换方法 sed -i "s/10.111. ...

  4. .net core ioc

    -------------------------------------- 2. ---------------------------- -----------aop

  5. rest framework-视图和路由-长期维护

    ###############   三种视图    ############### # 第一种方法:使用mixins # class AuthorView(mixins.ListModelMixin, ...

  6. Linux的iptables菜鸟初学

    什么是iptables? iptables是linux下的命令行工具,操控的是linux的防火墙,这个防火墙叫netfilter.通俗的说应该是用户通过iptables把安全设定设置给netfilte ...

  7. ISIS

    R1到R6配置ip和环回口 交换机不用配置 R6多加10.0.1.1 10.0.2.1 10.0.3.1 三个环回口 需求: 1.假如你是公司A网络管理员,公司A网络如图所示,现公司A要求如下:() ...

  8. 常用的GIT

    # 初始化相关 git init git add . git commit -m "test001" git remote origin https://github.com/fa ...

  9. github简单操作

    配置用户名: git config --global user.name 名.姓 配置用户邮件:git config --global user.email 名.姓@avatarmind.com 查看 ...

  10. [LC] 103. Binary Tree Zigzag Level Order Traversal

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...