Mybatis---查询使用resultType和resultMap的区别
来源:互联网 发布:linux snmp获取不到cpu 编辑:程序博客网 时间:2024/05/22 14:33
介绍:
本篇文章将介绍用mybatis实现查询时使用resultType和使用resultMap的区别
需求:
查询订单信息,关联查询创建订单的用户信息。
sql语句:
需要查询订单的所有属性和用户的姓名/性别/地址
SELECT orders.*, USER.username, USER.sex, USER.address FROM orders, USER WHERE orders.user_id = user.id
一、使用resultType
1、创建pojo:
我们需要将查询到的结果映射到pojo中,所以这个pojo应该包括所有的查询列名。那么已经存在order.java和user.java都不能满足需要,因此需要创建一个新的pojo,这个新的pojo可以继承已经存在的pojo,这样需要写的字段就少了一些,具体继承那个那??原则是继承包括查询字段较多的pojo类,所以毫无疑问选择继承order.java,因为我们要查询order的所有信息。
创建的新的pojo类OrdersCustom.java
//通过此类映射订单和用户查询的结果,让此类继承包括 字段较多的pojo类public class OrdersCustom extends Orders{//添加用户属性/*USER.username, USER.sex, USER.address */private String username;private String sex;private String address;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}
2、mapper.xml
<!-- 查询订单关联查询用户信息 --><select id="findOrdersUser" resultType="cn.itcast.mybatis.po.OrdersCustom">SELECTorders.*,USER.username,USER.sex,USER.addressFROMorders,USERWHERE orders.user_id = user.id</select>
3、mapper.java
//查询订单关联查询用户信息public List<OrdersCustom> findOrdersUser()throws Exception;
4、单元测试
@Test public void testFindOrdersUser() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); // 创建代理对象OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class);// 调用maper的方法List<OrdersCustom> list = ordersMapperCustom.findOrdersUser();System.out.println(list);sqlSession.close();}
二、使用resultMap
1、思路
在使用resultType时我们把查询的数据映射到新的pojo中,使用resultMap是将查询结果中的订单信息映射到Orders对象中,但是Orders对象中没有用户的字段怎么办???因此需要在Orders类中添加User属性,将关联查询出来的用户信息映射到Orders对象中的user属性中。
Orders.java
public class Orders { private Integer id; private Integer userId; private String number; private Date createtime; private String note; //用户信息 private User user; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number == null ? null : number.trim(); } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getNote() { return note; } public void setNote(String note) { this.note = note == null ? null : note.trim(); } public User getUser() {return user; } public void setUser(User user) {this.user = user; } }
2、mapper.xml
<1>定义resultMap
<!-- 订单查询关联用户的resultMap将整个查询的结果映射到cn.itcast.mybatis.po.Orders中 --><resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap"><!-- 配置映射的订单信息 --><!-- id:指定查询列中的唯 一标识,订单信息的中的唯 一标识,如果有多个列组成唯一标识,配置多个id column:订单信息的唯 一标识 列 property:订单信息的唯 一标识 列所映射到Orders中哪个属性 --><id column="id" property="id"/><result column="user_id" property="userId"/><result column="number" property="number"/><result column="createtime" property="createtime"/><result column="note" property="note"/><!-- 配置映射的关联的用户信息 --><!-- association:用于映射关联查询单个对象的信息 property:要将关联查询的用户信息映射到Orders中哪个属性--><association property="user" javaType="cn.itcast.mybatis.po.User"><!-- id:关联查询用户的唯 一标识column:指定唯 一标识用户信息的列javaType:映射到user的哪个属性 --><id column="user_id" property="id"/><result column="username" property="username"/><result column="sex" property="sex"/><result column="address" property="address"/></association></resultMap>
<2>statement定义
<!-- 查询订单关联查询用户信息,使用resultmap --><select id="findOrdersUserResultMap" resultMap="OrdersUserResultMap">SELECTorders.*,USER.username,USER.sex,USER.addressFROMorders,USERWHERE orders.user_id = user.id</select>
3、mapper.java
//查询订单关联查询用户使用resultMappublic List<Orders> findOrdersUserResultMap()throws Exception;
4、测试
@Testpublic void testFindOrdersUserResultMap() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession();// 创建代理对象OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class);// 调用maper的方法List<Orders> list = ordersMapperCustom.findOrdersUserResultMap();System.out.println(list);sqlSession.close();}
区别:
上面两种方法都实现了查询,那resultType和resultMap有什么区别那??
resultType:使用resultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。
如果没有查询结果的特殊要求建议使用resultType。
resultMap:需要单独定义resultMap,实现有点麻烦,如果对查询结果有特殊的要求,使用resultMap可以完成将关联查询映射pojo的属性中。
resultMap可以实现延迟加载,resultType无法实现延迟加载
总结:
以上就是这篇博客的所有内容,若有不确切的地方,欢迎指正!
- Mybatis---查询使用resultType和resultMap的区别
- Mybatis查询之resultMap和resultType区别
- Mybatis查询之resultMap和resultType区别
- Mybatis中的resultType和resultMap的使用及区别
- mybatis中resultType和resultMap使用时的区别
- mybatis中resultMap和resultType的区别
- mybatis resultType和resultMap的区别
- MyBatis中ResultMap和ResultType的区别
- MyBatis中resultType和resultMap的区别
- Mybatis: resultType和resultMap的区别
- Mybatis中的resultType和resultMap的区别
- mybatis中resulttype和resultmap的区别
- MyBatis中resultType和resultMap的区别
- MyBatis中resultMap和resultType的使用
- Mybatis的resultType和resultMap
- Mybatis中resultMap和resultType使用联系与区别
- Mybatis的查询操作之resultType和resultMap
- resultType和resultMap的区别
- SSH框架搭建
- go get安装第三方包的前提条件和步骤
- JS的各种注意点
- 背景图片全部全景展示
- DataFrame删除特定列
- Mybatis---查询使用resultType和resultMap的区别
- 欢迎使用CSDN-markdown编辑器
- 纯虚函数
- C语言检测某键是否被按下(监视热键)[GetKeyState]
- HDU 3530 --- Subsequence 单调队列
- 懒省事的小明
- intellij idea怎么打开outline,即像Eclipse中的在右边显示某个类的所有方法
- 那些酷炫的RecyclerView开源库整理
- restful风格,restcontroller与controller