Mybatis 入门之resultMap与resultType解说实例
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解说实例的更多相关文章
- Mybatis 入门之resultMap与resultType讲解实例
resultMap:适合使用返回值是自定义实体类的情况 resultType:适合使用返回值得数据类型是非自定义的,即jdk的提供的类型 resultMap : type:映射实体类的数据类型 id: ...
- mybatis中的resultMap与resultType、parameterMap与 parameterType的区别
Map:映射:Type:Java类型 resultMap 与 resultType.parameterMap 与 parameterType的区别在面试的时候被问到的几率非常高,项目中出现了一个小b ...
- Mybatis使用时 resultMap与resultType、parameterMap与 parameterType的区别
Map:映射:Type:Java类型 resultMap 与 resultType.parameterMap 与 parameterType的区别在面试的时候被问到的几率非常高,出现的次数到了令人 ...
- MyBatis学习总结(13)——Mybatis查询之resultMap和resultType区别
MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性 ...
- Mybatis第七篇【resultMap、resultType、延迟加载】
resultMap 有的时候,我们看别的映射文件,可能看不到以下这么一段代码: <resultMap id="userListResultMap" type="us ...
- mybatis的resultMap与resultType的区别
一.概述MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部Res ...
- Mybatis中resultMap与resultType区别
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultM ...
- Mybatis实现一对一查询 对ResultType和ResultMap分析
实现一对一查询: ResultMap:使用ResultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加 列名对应的属性,即可完成映射. 如果没有查询结果的特殊要求建议使用Resul ...
- Mybatis入门及Dao开发方式
本节内容: Mybatis介绍 使用jdbc编程问题总结 Mybatis架构 Mybatis入门程序 Dao的开发方式 SqlMapConfig.xml文件说明 一.Mybatis介绍 MyBatis ...
随机推荐
- HTML学习笔记 CSS表格及轮廓案例 第八节 (原创)参考使用表
#tb, tb1, tr, th, td { border: 5px solid blue; /*加边框*/ padding: 5px; /*内边距*/ } #tb1 { border-collaps ...
- 都说 WebP 厉害,究竟厉害在哪里?
之前在< WebP 的前世今生 >一文中,介绍了 WebP 图片格式是由 Google 基于 VP8 视频编码格式研发的,同时提供有损压缩和无损压缩两种格式,那么今天就来看看 WebP 有 ...
- Spring+Spring MVC+MyBatis框架集成
目录 一.新建一个基于Maven的Web项目 二.创建数据库与表 三.添加依赖包 四.新建POJO实体层 五.新建MyBatis SQL映射层 六.JUnit测试数据访问 七.完成Spring整合My ...
- 如何编写通用的 Helper Class
Github: https://github.com/nzbin/snack-helper Docs: https://nzbin.github.io/snack-helper 前言 什么是 help ...
- WebSocket小插件
一.WebSocket小介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通信 ...
- SQLSERVER实现更改表名,更改列名,更改约束代码
1.修改表名 格式:sp_rename tablename,newtablename ? 1 sp_rename tablename,newtablename 2.修改字段名 格式:sp_rename ...
- PHP和java比较
这样从几个方面来看:一.运行机制:Java代码被编译成字节码后,会在虚拟机里由JIT进行二次编译成为本地码,据传言其执行速度可以和C++相媲美,经过我自己测试,用Java实现一个简单的Memcache ...
- [转载] java多线程学习-java.util.concurrent详解(二)Semaphore/FutureTask/Exchanger
转载自http://janeky.iteye.com/blog/770393 ------------------------------------------------------------- ...
- python进阶------进程线程(三)
python中的进程 1.multiprocessing模块 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进 ...
- Java8 ArrayList源码分析
java.util.ArrayList是最常用的工具类之一, 它是一个线程不安全的动态数组. 本文将对JDK 1.8.0中ArrayList实现源码进行简要分析. ArrayList底层采用Objec ...