Mybatis中resultMap和resultType的区别
MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性,而当我们提供的返回类型是resultMap的时候,将数据库中列数据复制到对象的相应属性上,可以用于复制查询,两者不能同时用。
1、resultType
返回单个实例
<select id="selectUser" parameterType="int" resultType="User">
select * from user where id = #{id}
</select>
返回List集合
<select id="selectUserAll" resultType="User" > <!-- resultMap="userMap" -->
select * from user
</select>
2、resultMap
简单查询:
<resultMap type="User" id="userMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
</resultMap>
column:数据库中列名称,property:类中属性名称
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值得数据类型是非自定义的,即jdk的提供的类型
resultMap :
映射实体类的数据类型
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(*) fromperson</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;@Overridepublic String toString() {return "Person [personId=" + personId + ", name=" + name + ", gender="+ gender + ", personAddr=" + personAddr + ", birthday="+ birthday + "]";} }
测试类
public class PersonTest {SqlSessionFactory sqlSessionFactory;@Beforepublic void setUp() throws Exception {// 读取资源流InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");// 初始化session工厂sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);}@Testpublic void selectPersonById() {// 创建一个sqlsessionSqlSession session = sqlSessionFactory.openSession();try {Person p = session.selectOne("com.hao947.sql.mapper.PersonMapper.selectPersonById", 1);System.out.println(p);} finally {session.close();}}@Testpublic void selectPersonCount() {// 创建一个sqlsessionSqlSession session = sqlSessionFactory.openSession();try {Integer p = session.selectOne("com.hao947.sql.mapper.PersonMapper.selectPersonCount");System.out.println(p);} finally {session.close();}}@Testpublic void selectPersonByIdWithMap() {// 创建一个sqlsessionSqlSession 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的区别
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap resultType是直接表示返回类型的,而resultMap则是对外部ResultM ...
- 【MyBatis】resultMap和resultType的区别
mybatis中resultMap和resultType的区别 mybatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap.resultType是 ...
- mybatis中resultMap和resultType区别,三分钟读懂
先说结论: resultmap与resulttype的区别为:对象不同.描述不同.类型适用不同. 说人话就是,resultmap和resulttype功能差不多,但是resultmap功能更强大 re ...
- Mybatis中resultMap和resultType
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接 表示返回类型的,而resultMap则是对外部Result ...
- mybatis中resultMap和resultType的详细用法
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接 表示返回类型的,而resultMap则是对外部Result ...
- ibatis中resultMap和resultType使用区别
resultMap 1.resultMap的值为映射的返回值自定义节点Id名称 resultType 1.resultType的值为返回值的类型,且为一列 ---------------------- ...
- php 数据类型 map,ResultMap和ResultType的区别
ResultMap和ResultType的区别 使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType:另一种就是resultMap: 1.re ...
- resultMap与resultType的区别
resultmap与resulttype的区别为:对象不同.描述不同.类型适用不同. 一.对象不同 1.resultmap:resultMap如果查询出来的列名和pojo的属性名不一致,通过定义一个r ...
- MyBatis中resultMap详解
MyBatis 中 resultMap 详解 resultMap 是 Mybatis 最强大的元素之一,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中.如在实际应用中,有一个表 ...
最新文章
- 无需「域外」文本,微软:NLP就应该针对性预训练
- ASP.NET MVC 5 入门教程 (2) 控制器Controller
- pyrealsense2 sensor.get_option_description 报错 RuntimeError: object doesn't support option #14
- 只看不说-CCTV的客户端关键字
- 服务器开机提示修复,电脑开机提示自动修复怎么办?win10电脑开机提示自动修复教程...
- [C#] 如何分析stackoverflow等clr错误
- web报表工具FineReport使用中遇到的常见报错及解决办法(二)
- Hive 计算 yyyyMMdd 之间的时间差
- 合抱之木,生于毫末。九层之台,起于累土。千里之行,始于足下
- 使用vue实现的人物关系图谱
- 河南民办计算机大学排名,2021河南民办大学排名 河南最好的民办本科高校有哪些...
- 每周一荐:《盗梦空间》《不能承受的生命之轻》
- 小程序_OTA升级的一些情况说明
- Golang + HTML5 实现多文件上传
- 情报研判分析系统开发,可视化大数据研判平台建设
- java 数据结构和算法 排序
- android编辑按钮图标,修改 Floating Action Button 图标
- (Field II仿真)合成发射孔径超声成像
- Flume之生产正确的使用方式一(Singel Agent)
- 用户画像构建(理论篇)
热门文章
- C语言中 sqrt(); 函数的最全用法总结,最全!!!
- Android必学之AsyncTask,多线程AsyncTask,详细AsyncTask使用教程
- 计算机专业毕业设计题目大全文库,计算机专业毕业设计论文题目.doc
- Seaborn的调色板(palette)
- 【实验】实验课总结2 实验一
- mysql udf 执行命令_mylab_sys_exec UDF调用mysql外部系统命令(For linux)
- 软件测试黑盒测试代码,软件测试黑盒测试代码.doc
- 二十七、74LS148芯片解析
- MATLAB中的bsxfun函数
- 1036. 跟奥巴马一起编程(15)