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

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

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

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

1.1.2mapper.xml
[html]  view plain  copy
  1. <mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper">  
  2.       
  3.     <!-- 用户信息综合查询   
  4.     #{UserCustom.sex}取出包装对象中性别值  
  5.     ${UserCustom.username}取得pojo包装对象中用户名称  
  6.     -->  
  7.     <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo"   
  8.                                 resultType="cn.edu.hpu.mybatis.PO.UserCustom">  
  9.         select * from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'  
  10.     </select>  
  11.       
  12.     <!-- 用户信息综合查询总数 -->  
  13.     <select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">  
  14.         select count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'  
  15.     </select>  
  16.     ......  
  17. </mapper>  

1.1.3mapper.java
[java]  view plain  copy
  1. //用户管理的Dao接口  
  2. public interface UserMapper {  
  3.       
  4.     //用户信息综合查询  
  5.     public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception;  
  6.       
  7.     //用户信息综合查询总数  
  8.     public int findUserCount(UserQueryVo userQueryVo) throws Exception;  
  9.     ......  
  10. }  

1.1.4测试代码
[java]  view plain  copy
  1. //用户信息综合查询总数  
  2.     @Test  
  3.     public void testFindUserCount() throws Exception{  
  4.           
  5.         SqlSession sqlSession=sqlSessionFactory.openSession();  
  6.           
  7.         //创建UserMapper代理对象  
  8.         UserMapper userMapper=sqlSession.getMapper(UserMapper.class);  
  9.           
  10.         //创建包装对象,设置查询条件  
  11.         UserQueryVo userQueryVo=new UserQueryVo();  
  12.         UserCustom userCustom=new UserCustom();  
  13.         userCustom.setSex("男");  
  14.         userCustom.setUsername("张三");  
  15.         userQueryVo.setUserCustom(userCustom);  
  16.           
  17.         //调用userMapper的方法  
  18.         int count=userMapper.findUserCount(userQueryVo);  
  19.           
  20.         System.out.println("总数为:"+count);  
  21.     }  

测试结果:
总数为:2

输出日志:
[plain]  view plain  copy
  1. DEBUG [main] - Opening JDBC Connection  
  2. DEBUG [main] - Created connection 7832149.  
  3. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@778255]  
  4. DEBUG [main] - ==>  Preparing: select count(*) from user where user.sex=? and user.username like '%张三%'   
  5. DEBUG [main] - ==> Parameters: 男(String)  
  6. 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

[html]  view plain  copy
  1. <!-- 定义resultType  
  2. 将select id id_,username _username from user和User类中的属性做一个映射关系  
  3.       
  4. type:resultMap最终所映射的Java对象类型,可以使用别名  
  5. id:对resultMap的唯一标识   
  6. -->  
  7. <resultMap type="user" id="userResultMap">  
  8.     <!-- id表示查询结果集中唯一标识   
  9.     column:查询出的列名  
  10.     property:type所指定的POJO中的属性名  
  11.     最终reslutMap对column和property做一个映射关系(对应关系)  
  12.     -->  
  13.     <id column="_id" property="id"/>  
  14.     <!-- 对普通列的映射定义 -->  
  15.     <result column="_username" property="username"/>  
  16. </resultMap>  

(2)使用resultMap作为statement的输出映射类型
[html]  view plain  copy
  1. <!-- 使用resultMap进行输出映射   
  2.     resultMap:指定定义的resultMap的id,如果这个resultMap在其它的mapper文件,前面需要加namespace  
  3.     -->  
  4. <select id="findUserByResultMap" parameterType="int" resultMap="userResultMap">  
  5.     select id _id,username _username from user where id=#{value}  
  6. </select>  

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

[java]  view plain  copy
  1. //用户管理的Dao接口  
  2. public interface UserMapper {  
  3.       
  4.     public User findUserByResultMap(int id) throws Exception;  
  5.     ......  
  6. }  

测试:
[java]  view plain  copy
  1. @Test  
  2. public void testFindUserByResultMap() throws Exception{  
  3.           
  4.     SqlSession sqlSession=sqlSessionFactory.openSession();  
  5.           
  6.     //创建UserMapper代理对象  
  7.     UserMapper userMapper=sqlSession.getMapper(UserMapper.class);  
  8.           
  9.     //调用userMapper的方法  
  10.     User user=userMapper.findUserByResultMap(1);  
  11.           
  12.     System.out.println(user.getUsername());  
  13. }  

测试结果:
张三

输出日志:

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


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

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

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值