【Mybatis框架】输出映射-resultType与resultMap

输出映射
接下来说说有关Mapper.xml配置文件中查询标签中关于返回值类型resultType与resultMap的一些内容

1.resultType
使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。
如果查询出来的列名和pojo中的属性名全部不一致,没有创建pojo对象。
只要查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象。

1.1输出简单类型
1.1.1需求
用户信息的综合查询列表总数,通过查询总数和上边用户综合查询列表才可以实现分页。

1.1.2mapper.xml

<mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper">

	<!-- 用户信息综合查询
	#{UserCustom.sex}取出包装对象中性别值
	${UserCustom.username}取得pojo包装对象中用户名称
	-->
	<select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo"
								resultType="cn.edu.hpu.mybatis.PO.UserCustom">
		select * from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
	</select>

	<!-- 用户信息综合查询总数 -->
	<select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">
		select count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
	</select>
	......
</mapper>

1.1.3mapper.java

//用户管理的Dao接口
public interface UserMapper {

	//用户信息综合查询
	public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception;

	//用户信息综合查询总数
	public int findUserCount(UserQueryVo userQueryVo) throws Exception;
	......
}

1.1.4测试代码

//用户信息综合查询总数
	@Test
	public void testFindUserCount() throws Exception{

		SqlSession sqlSession=sqlSessionFactory.openSession();

		//创建UserMapper代理对象
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

		//创建包装对象,设置查询条件
		UserQueryVo userQueryVo=new UserQueryVo();
		UserCustom userCustom=new UserCustom();
		userCustom.setSex("男");
		userCustom.setUsername("张三");
		userQueryVo.setUserCustom(userCustom);

		//调用userMapper的方法
		int count=userMapper.findUserCount(userQueryVo);

		System.out.println("总数为:"+count);
	}

测试结果:
总数为:2

输出日志:

DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 7832149.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@778255]
DEBUG [main] - ==>  Preparing: select count(*) from user where user.sex=? and user.username like '%张三%'
DEBUG [main] - ==> Parameters: 男(String)
DEBUG [main] - <==      Total: 1

1.1.5小结
查询出来的结果集只有一行且一列,可以使用简单类型进行输出映射。(输出简单类型的要求)

1.2输出pojo对象和pojo列表

不管是输出的pojo单个对象还是一个列表(list中包括pojo),在mapper.xml中resultType指定的类型是一样的。
在mapper.java指定的方法返回值类型不一样:
(1)输出单个pojo对象,方法返回值是单个对象类型
 
(2)输出pojo对象list,方法返回值是List<Pojo>

生成的动态代理对象中是根据mapper方法的返回值类型确定是调用selectOne(返回单个对象调用)还是selectList (返回集合对象调用 ).

(3)输出hashmap
输出pojo对象可以改用hashmap输出类型,将输出的字段名称作为map的key,value为字段值。如果是集合,那就是list里面套了HashMap。

2.resultMap
mybatis中使用resultMap完成高级输出结果映射。

2.1resultMap使用方法
如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。

下面来进行实验,实验需求
2.2将下边的sql使用User完成映射
SELECT id id_,username username_ FROM USER WHERE id=#{value}

User类中属性名和上边查询列名不一致。

resultMap使用方法:(一下属性均定义在Mapper.xml映射文件中)
(1)定义resultMap

<!-- 定义resultType
将select id id_,username _username from user和User类中的属性做一个映射关系

type:resultMap最终所映射的Java对象类型,可以使用别名
id:对resultMap的唯一标识
-->
<resultMap type="user" id="userResultMap">
	<!-- id表示查询结果集中唯一标识
	column:查询出的列名
	property:type所指定的POJO中的属性名
	最终reslutMap对column和property做一个映射关系(对应关系)
	-->
	<id column="_id" property="id"/>
	<!-- 对普通列的映射定义 -->
	<result column="_username" property="username"/>
</resultMap>

(2)使用resultMap作为statement的输出映射类型

<!-- 使用resultMap进行输出映射
	resultMap:指定定义的resultMap的id,如果这个resultMap在其它的mapper文件,前面需要加namespace
	-->
<select id="findUserByResultMap" parameterType="int" resultMap="userResultMap">
	select id _id,username _username from user where id=#{value}
</select>

(3)mapper接口类中添加相应方法

//用户管理的Dao接口
public interface UserMapper {

	public User findUserByResultMap(int id) throws Exception;
	......
}

测试:

@Test
public void testFindUserByResultMap() throws Exception{

	SqlSession sqlSession=sqlSessionFactory.openSession();

	//创建UserMapper代理对象
	UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

	//调用userMapper的方法
	User user=userMapper.findUserByResultMap(1);

	System.out.println(user.getUsername());
}

测试结果:
张三

输出日志:

EBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1465214.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@165b7e]
DEBUG [main] - ==>  Preparing: select id _id,username _username from user where id=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 1

小结
使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。

如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。

转载请注明出处:http://blog.csdn.net/acmman/article/details/46509375

时间: 2024-08-03 19:24:32

【Mybatis框架】输出映射-resultType与resultMap的相关文章

后台(40)——MyBatis输出映射resultType以及resultMap

探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Android多分辨率适配框架(3)- 使用指南 自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View

MyBatis Review——输入输出映射

一,输入映射     mybatis的输入映射通过parameterType指定,可以为简单类型,包装类型,hashmap类型.           1,简单类型                     <select id="findUserByName" parameterType="java.lang.String" resultType="cn.itcast.mybatis.po.User"> SELECT * FROM `u

MyBatis Review——使用resultType和resultMap实现一对一查询

      例如:                  查询订单信息,关联查询创建订单的用户信息.      查询语句:              SELECT orders.*, USER .username ,USER .sex, USER .address FROM orders, USER WHERE orders.user_id = USER .id        查询结果:     1,使用resultType接受输出结果                  用户信息:         

【MyBatis框架】高级映射-一对一查询

一对一查询 根据上面我们分析的订单商品数据模型(链接:12.订单商品数据模型-分析思路.txt),我们来写一下有关一对一的查询,分别使用了resultType和resultMap指定输出参数类型 1.一对一查询使用resultType指定输出参数类型 1.1需求 查询订单信息,关联查询创建订单的用户信息 1.2resultType 1.2.1sql语句 确定查询的主表:订单表 确定查询的关联表:用户表关联查询使用内链接?还是外链接?由于orders表中有一个外键(user_id),通过外键关联查

MyBatis中关于resultType和resultMap的区别介绍_java

MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的(对应着我们的model对象中的实体),而resultMap则是对外部ResultMap的引用(提前定义了db和model之间的隐射key-->value关系),但是resultType跟resultMap不能同时存在. 在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值.

Java的MyBatis框架中Mapper映射配置的使用及原理解析_java

Mapper的内置方法model层就是实体类,对应数据库的表.controller层是Servlet,主要是负责业务模块流程的控制,调用service接口的方法,在struts2就是Action.Service层主要做逻辑判断,Dao层是数据访问层,与数据库进行对接.至于Mapper是mybtis框架的映射用到,mapper映射文件在dao层用. 下面是介绍一下Mapper的内置方法: 1.countByExample ===>根据条件查询数量 int countByExample(UserEx

Mybatis中的resultType和resultMap

一.概述 MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在. 在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值. ①当提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给r

详解Java的MyBatis框架中SQL语句映射部分的编写_java

1.resultMapSQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对应的接口类的路径.写好SQL语句映射文件后,需要在MyBAtis配置文件mappers标签中引用,例如: <mappers> <mapper resource="com/liming/manager/data/mappers/UserMapper.xml" /> <mapper resource="com/liming/manag

Mybatis中的resultType和resultMap查询操作实例详解_java

resultType和resultMap只能有一个成立,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,resultMap解决复杂查询是的映射问题.比如:列名和对象属性名不一致时可以使用resultMap来配置:还有查询的对象中包含其他的对象等. MyBatisConfig.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configura