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

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

resultMap :

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

id: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 入门之resultMap与resultType解说实例的更多相关文章

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

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

  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第七篇【resultMap、resultType、延迟加载】

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

  6. mybatis的resultMap与resultType的区别

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

  7. Mybatis中resultMap与resultType区别

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

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

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

  9. Mybatis入门及Dao开发方式

    本节内容: Mybatis介绍 使用jdbc编程问题总结 Mybatis架构 Mybatis入门程序 Dao的开发方式 SqlMapConfig.xml文件说明 一.Mybatis介绍 MyBatis ...

随机推荐

  1. 关于viewports 设备像素比 密度

    首先追溯到像素,第一个麻烦事像素的总量问题,同样的大小的屏幕像素可以差很远,像素大小更小的导致内容也变小   在小屏幕上如果展示巨大的桌面网页,诺基亚的做法是首先载入完整的桌面网页,然后缩放至设备屏幕 ...

  2. ④bootstrap列表使用基础案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. mysql 运维常见操作

    初始安装并赋予密码:   [root@Alinx html]# yum install -y mysql mysql-server                         #安装mysql可与 ...

  4. 如何实现border-width:0.5px;

    工作中遇到了一个产品需求,要求把列表分割线改成0.5px,直接写成border:0.5px solid #cccccc;是不符合规范的写法,会存在Android和IOS手机上的兼容问题,故,我们可以利 ...

  5. sublime text3添加右键打开的操作

    前一段重新安装了Sublime Text3,不过一直不在右键菜单中,所以决定添加,有如下2种方法. 方法一(推荐). 把以下代码,复制到SublimeText3的安装目录,然后重命名为:sublime ...

  6. Openstack的打包方法

    使用setup.cfg和setup.py进行管理 1.setup.py文件内容 # THIS FILE IS MANAGED BY THE GLOBAL REQUIREMENTS REPO - DO ...

  7. python3学习笔记(3)

    一.内置函数补充1.callable()检测传递的参数是否可以被调用.def f1() pass可以被调用f2 = 123不可以被调用2.chr()和ord()chr()将ascii码转换成字符,or ...

  8. ASP.NET异常处理机制

    try{ //获取并使用资源,可能出现异常}catch(DivideByZeroException de){}catch(ArithmeticException ae){}catch(Exceptio ...

  9. 正确释放Vector的内存

    http://blog.jobbole.com/37700/ 今天在看微博的时候, 有人提出了一个对于Vector内存泄露的疑问( Link). 博主采用 Vector存储一些数据,但是发现在执行 c ...

  10. Android Context讲解(转)

    博客出处 前言:本文是我读<Android内核剖析>第7章 后形成的读书笔记 ,在此向欲了解Android框架的书籍推荐此书. 大家好, 今天给大家介绍下我们在应用开发中最熟悉而陌生的朋友 ...