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. int preg_match( string pattern

    preg_match -- 进行正则表达式匹配.并且只匹配一次,注意与preg_match_all区别. int preg_match( string pattern, string subject ...

  2. javascript中的location的用法

    javascript中的location.href有很多种用法,主要如下. self.location.href="/url" 当前页面打开URL页面 location.href= ...

  3. canvas实现粒子星空连线

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>离 ...

  4. MySQL-慢日志slow log

    文件结构 1.目录结构 drwxrwxr-x mysql mysql Mar bin drwxrwxr-x mysql mysql Dec : binlogdir -rw-r--r-- mysql m ...

  5. 01 语言基础+高级:1-2 面向对象和封装_day06【类与对象、封装、构造方法】

    day06[类与对象.封装.构造方法] 面向对象类与对象三大特征——封装构造方法 能够理解面向对象的思想能够明确类与对象关系能够掌握类的定义格式能够掌握创建对象格式,并访问类中的成员能够完成手机类的练 ...

  6. PAT Advanced 1052 Linked List Sorting (25) [链表]

    题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...

  7. JavaEE--调用 WSDL -- httpclient 4.x.x

    参考:http://aperise.iteye.com/blog/2223454   http://blog.csdn.net/chenleixing/article/details/43456987 ...

  8. 2017年3月25日工作日志:Jquery使用小结[绑定事件判断、select标签、军官证正则]

    jQuery获取DOM绑定事件 在1.8.0版本之前,我们要想获取某个DOM绑定的事件处理程序可以这样: $.data(domObj,'events');//或者$('selector').data( ...

  9. list交集、差集、并集、去重并集

    // 交集 List<String> intersection = list1.stream().filter(item -> list2.contains(item)).colle ...

  10. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第六天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...