【MyBatis框架点滴】——MyBatis输出映射
2016-06-01 20:38
363 查看
上文说到,MyBatis中,通过parameterType指定输入参数的类型。这里说一下用resultType和resultMap来指定输出参数的类型。
resultType
resultType是最常用的指定输入参数类型的方式,一般用来指定输出简单类型和pojo。
一、指定输出简单类型
映射文件:
实现:
测试:
二、指定输入pojo
用resultType指定输入参数的类型为pojo时,有三种情况:
1、查询出来的列名与pojo中的属性名完全一致,结果会创建pojo对象,映射成功。
User.java
映射文件:
当查询的字段与User的属性名完全一致时,查询结果会自动映射为pojo对象,且每个属性都会有值,如下:
2、查询出来的列名与pojo中的属性名部分一致,查询结果会自动映射为pojo对象,且部分属性会有值;
映射文件:
3、查询出来的列名与pojo中的属性名完全不一致,则不会创建对象,结果为null;
这时,输出的user直接为null:
需要注意的是,当输出类型为pojo时,不管查询结果为单个pojo对象还是集合,resultType的属性值都写成pojo的类型。
resultType还可以指定输出类型为Map,当指定输出类型为Map时,默认映射的结果集中列名为key值,查询到的数据为value。
resultMap
上面resultType指定输出类型为pojo时,查询结果集的列名可能与pojo的字段不一致,因此可以使用resultMap为列名和pojo的字段之间作一个映射关系。
比如上面的第2中情况,用resultType指定输出类型为pojo时,查询出来的列名与pojo中的属性名部分一致,查询结果会自动映射为pojo对象,但只有部分属性会有值。可以利用resultMap为其余不对应的列名与字段建立映射关系,使最终查询到的每列的值都可以一 一对应到pojo对象中。
映射文件:
如上,在
配置后,查询结果如下:
总结
使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。
如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。
【 转载请注明出处——胡玉洋《【MyBatis框架点滴】——MyBatis输出映射》】
resultType
resultType是最常用的指定输入参数类型的方式,一般用来指定输出简单类型和pojo。
一、指定输出简单类型
映射文件:
<select id="findUserByUserName" parameterType="string" resultType="int"> select count(*) from user where username =#{username} </select>
实现:
/** * 根据姓名查询用户数量 * @param username * @return * @throws Exception */ @Override public int findUserByUserName(String username) throws Exception { SqlSession session=sqlSessionFactory.openSession(); int result=session.selectOne("com.danny.mybatis.findUserByUserName",username); session.close(); return result; }
测试:
@Test public void testQueryByUserName() throws Exception { UserDao userDao = new UserDaoImpl(sqlSessionFactory); int result = userDao.findUserByUserName("DannyHoo"); System.out.println(result); }
二、指定输入pojo
用resultType指定输入参数的类型为pojo时,有三种情况:
1、查询出来的列名与pojo中的属性名完全一致,结果会创建pojo对象,映射成功。
User.java
public class User implements Serializable { private int id; private String username; private int sex; private Date birthday; private String address; //getter、setter }
映射文件:
<select id="findUserById" parameterType="int" resultType="com.danny.mybatis.po.User"> select id,username,sex,birthday,address from user where id=#{value} </select>
当查询的字段与User的属性名完全一致时,查询结果会自动映射为pojo对象,且每个属性都会有值,如下:
2、查询出来的列名与pojo中的属性名部分一致,查询结果会自动映射为pojo对象,且部分属性会有值;
映射文件:
<select id="findUserById" parameterType="int" resultType="com.danny.mybatis.po.User"> select id,username as name,sex as,birthday as birthdate,address from user where id=#{value} </select>
3、查询出来的列名与pojo中的属性名完全不一致,则不会创建对象,结果为null;
<select id="findUserById" parameterType="int" resultType="com.danny.mybatis.po.User"> select id as number,username as name,sex as s,birthday as birthdate,address as addr from user where id=#{value} </select>
这时,输出的user直接为null:
需要注意的是,当输出类型为pojo时,不管查询结果为单个pojo对象还是集合,resultType的属性值都写成pojo的类型。
resultType还可以指定输出类型为Map,当指定输出类型为Map时,默认映射的结果集中列名为key值,查询到的数据为value。
resultMap
上面resultType指定输出类型为pojo时,查询结果集的列名可能与pojo的字段不一致,因此可以使用resultMap为列名和pojo的字段之间作一个映射关系。
比如上面的第2中情况,用resultType指定输出类型为pojo时,查询出来的列名与pojo中的属性名部分一致,查询结果会自动映射为pojo对象,但只有部分属性会有值。可以利用resultMap为其余不对应的列名与字段建立映射关系,使最终查询到的每列的值都可以一 一对应到pojo对象中。
映射文件:
<!--定义User的resultMap--> <resultMap type="com.danny.mybatis.po.User" id="userResultMap"> <id column="name" property="username"/> <id column="birthdate" property="birthday"/> </resultMap> <!--User查询语句--> <select id="findUserById" parameterType="int" resultMap="userResultMap"> select id,username as name,sex,birthday as birthdate,address from user where id=#{value} </select>
如上,在
<select></select>的同级添加
<resultMap></resultMap>标签,为两个不对应的属性配置映射关系。
配置后,查询结果如下:
总结
使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。
如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。
【 转载请注明出处——胡玉洋《【MyBatis框架点滴】——MyBatis输出映射》】
相关文章推荐
- 插件管理框架 for Delphi(一)
- 使用CSS框架布局的缺点和优点小结
- 一起动手编写Android图片加载框架
- 基于.NET平台常用的框架和开源程序整理
- 列举PHP的Yii 2框架的开发优势
- Windows窗体的.Net框架绘图技术实现方法
- 浅谈JavaScript 框架分类
- 轻量级javascript 框架Backbone使用指南
- javascript实现框架高度随内容改变的方法
- JS刷新框架外页面七种实现代码
- 超赞的动手创建JavaScript框架的详细教程
- 深入探讨前端框架react
- mapper--图片热点区域高亮组件官方站点
- js验证框架实现代码分享
- jQuery的框架介绍
- 简单介绍不用库(框架)自己写ajax
- 利用ASP.NET MVC+EasyUI+SqlServer搭建企业开发框架
- asp.net4.0框架下验证机制失效的原因及处理办法
- 插件管理框架 for Delphi(二)
- 零基础学习AJAX之AJAX框架