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

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

resultMap :

type:映射实体类的数据类型

id:resultMap的唯一标识

column:库表的字段名

property:实体类里的属性名

配置映射文件:

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

实体类Person.java

  1. <pre name="code" class="java">package com.hao947.model;
  2. import java.util.Date;
  3. public class Person {
  4. private Integer personId;
  5. private String name;
  6. private Integer gender;
  7. private String personAddr;
  8. private Date birthday;
  9. @Override
  10. public String toString() {
  11. return "Person [personId=" + personId + ", name=" + name + ", gender="
  12. + gender + ", personAddr=" + personAddr + ", birthday="
  13. + birthday + "]";
  14. }
  15. }

测试类

  1. public class PersonTest {
  2. SqlSessionFactory sqlSessionFactory;
  3. @Before
  4. public void setUp() throws Exception {
  5. // 读取资源流
  6. InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
  7. // 初始化session工厂
  8. sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
  9. }
  10. @Test
  11. public void selectPersonById() {
  12. // 创建一个sqlsession
  13. SqlSession session = sqlSessionFactory.openSession();
  14. try {
  15. Person p = session.selectOne(
  16. "com.hao947.sql.mapper.PersonMapper.selectPersonById", 1);
  17. System.out.println(p);
  18. } finally {
  19. session.close();
  20. }
  21. }
  22. @Test
  23. public void selectPersonCount() {
  24. // 创建一个sqlsession
  25. SqlSession session = sqlSessionFactory.openSession();
  26. try {
  27. Integer p = session.selectOne(
  28. "com.hao947.sql.mapper.PersonMapper.selectPersonCount");
  29. System.out.println(p);
  30. } finally {
  31. session.close();
  32. }
  33. }
  34. @Test
  35. public void selectPersonByIdWithMap() {
  36. // 创建一个sqlsession
  37. SqlSession session = sqlSessionFactory.openSession();
  38. try {
  39. Map<String ,Object> map = session.selectOne(
  40. "com.hao947.sql.mapper.PersonMapper.selectPersonByIdWithMap",1);
  41. System.out.println(map);
  42. } finally {
  43. session.close();
  44. }
  45. }
  46. }

Mybatis 入门之resultMap与resultType讲解实例的更多相关文章

  1. Mybatis 入门之resultMap与resultType解说实例

    resultMap:适合使用返回值是自己定义实体类的情况 resultType:适合使用返回值得数据类型是非自己定义的,即jdk的提供的类型 resultMap : type:映射实体类的数据类型 i ...

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

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

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

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

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

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

  5. mybatis入门,基本案例和xml讲解

    mybatis入门 先举一个入门案例 1)创建一个mybatis-day01这么一个javaweb工程或java工程 2)导入mybatis和mysql/oracle的jar包到/WEB-INF/li ...

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

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

  7. mybatis的resultMap与resultType的区别

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

  8. Mybatis中resultMap与resultType区别

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

  9. Mybatis实现一对一查询 对ResultType和ResultMap分析

    实现一对一查询: ResultMap:使用ResultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加 列名对应的属性,即可完成映射. 如果没有查询结果的特殊要求建议使用Resul ...

随机推荐

  1. CodeForces 382B 数学推导

    这个题目题意简单,但是TLE得哭哭的... 输入 a b w x c五个数,最终要使得c<=a, 每一秒可以进行一个操作,如果b>=x,则 b=b-x,同时 c--;如果b<x,则a ...

  2. java -jar 和 java -cp 的区别

    https://blog.csdn.net/weixin_38653290/article/details/84647019 1.pom中build指定mainClass 但是 META-INF\MA ...

  3. ruoyi LogUtils

    package com.ruoyi.framework.util; import java.io.PrintWriter; import java.io.StringWriter; import ja ...

  4. java使用HSSFWorkbook下载Excel表格

    @RequestMapping(value = "/exportVectorExcelN", method = RequestMethod.GET) @ResponseBody @ ...

  5. 反射(hasattr和setattr和delattr)

    反射(hasattr和setattr和delattr) 一.反射在类中的使用 反射就是通过字符串来操作类或者对象的属性, 反射本质就是在使用内置函数,其中反射有以下四个内置函数: hasattr:通过 ...

  6. TFRecord 的使用

    什么是 TFRecord PS:这段内容摘自 http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/reading_data.html 一 ...

  7. vim下看C++代码

    看C++代码, 缺少合适的编辑器,捣鼓vim. 安装Vundle, 用于插件管理 git clone https://github.com/VundleVim/Vundle.vim.git ~/.vi ...

  8. dynamic用法

    https://www.cnblogs.com/lgx5/p/9650203.html https://www.cnblogs.com/gygtech/p/9915367.html

  9. F - Filter Gym - 100553F (bitset用法)

    题目链接:http://codeforces.com/gym/100553/attachments/download/2885/20142015-acmicpc-northeastern-europe ...

  10. Java线程池面试

    New Thread的弊端 每次new Thread会新建对象,性能差 线程缺乏统一管理,可能无限制的新建线程,相互竞争,有可能占用过多系统资源导致死机或OOM 缺少更多功能,如更多执行.定期执行.线 ...