MyBatis Review——使用resultType和resultMap实现一对一查询
2016-05-23 13:15
585 查看
例如:
查询订单信息,关联查询创建订单的用户信息。
查询语句:
SELECT
orders.*, USER .username ,USER .sex,
USER .address
FROM
orders,
USER
WHERE
orders.user_id = USER .id
查询结果:
订单信息:
用于接收结果的pojo:
为了将查询结果映射到pojo中,pojo必须包括所有查询列名。在原始orders对象不能接受所有查询字段的时候,定义OrdersCustom来进行扩展字段。
之后将resutlType在xml中配置为我们定义的新的pojo类:
<!-- 查询订单关联查询用户 -->
<select id="findOrdersUser" resultType="cn.itcast.mybatis.po.OrdersCustom">
SELECT
orders.*, USER
.username ,USER .sex,
USER .address
FROM
orders,
USER
WHERE
orders.user_id =
USER .id
</select>
2,使用resultMap接受返回结果
使用resultMap将查询结果中的订单信息映射到Orders对象中,在orders类中添加User属性,将关联查询出来的用户信息映射到orders对象中的user属性中。
配置resultMap:
<!-- 定义订单查询关联用户的resultMap -->
<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap">
<!-- 配置映射的订单信息 -->
<id column="id" property="id"/><!-- 订单信息的唯一标识 --><!-- 如果有多个列,组成唯一标识,配置多个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">
<!-- 关联查询的用户的唯一标识 column:指定唯一标识用户信息的列 -->
<id column="user_id" property="id" />
<result column="username" property="username" />
<result column="sex" property="sex" />
<result column="address" property="address" />
</association>
</resultMap>
xml中输出结果配置:
<!-- 查询订单关联查询用户 :使用resultMap -->
<select id="findOrdersUserResultMap" resultMap="OrdersUserResultMap">
SELECT
orders.*, USER
.username ,USER .sex,
USER .address
FROM
orders,
USER
WHERE
orders.user_id =
USER .id
</select>
小结:
实现一对一查询,使用上面两种方式作为输出:
resultType:使用resultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。如果没有查询结果的特殊要求建议使用resultType。 resultMap:需要单独定义resultMap,实现有点麻烦,如果对查询结果有特殊的要求,使用resultMap可以完成将关联查询映射pojo的属性中。 结果上的区别:resultMap可以实现延迟加载,resultType无法实现延迟加载。
查询订单信息,关联查询创建订单的用户信息。
查询语句:
SELECT
orders.*, USER .username ,USER .sex,
USER .address
FROM
orders,
USER
WHERE
orders.user_id = USER .id
查询结果:
1,使用resultType接受输出结果
用户信息:订单信息:
用于接收结果的pojo:
为了将查询结果映射到pojo中,pojo必须包括所有查询列名。在原始orders对象不能接受所有查询字段的时候,定义OrdersCustom来进行扩展字段。
之后将resutlType在xml中配置为我们定义的新的pojo类:
<!-- 查询订单关联查询用户 -->
<select id="findOrdersUser" resultType="cn.itcast.mybatis.po.OrdersCustom">
SELECT
orders.*, USER
.username ,USER .sex,
USER .address
FROM
orders,
USER
WHERE
orders.user_id =
USER .id
</select>
2,使用resultMap接受返回结果
使用resultMap将查询结果中的订单信息映射到Orders对象中,在orders类中添加User属性,将关联查询出来的用户信息映射到orders对象中的user属性中。配置resultMap:
<!-- 定义订单查询关联用户的resultMap -->
<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap">
<!-- 配置映射的订单信息 -->
<id column="id" property="id"/><!-- 订单信息的唯一标识 --><!-- 如果有多个列,组成唯一标识,配置多个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">
<!-- 关联查询的用户的唯一标识 column:指定唯一标识用户信息的列 -->
<id column="user_id" property="id" />
<result column="username" property="username" />
<result column="sex" property="sex" />
<result column="address" property="address" />
</association>
</resultMap>
xml中输出结果配置:
<!-- 查询订单关联查询用户 :使用resultMap -->
<select id="findOrdersUserResultMap" resultMap="OrdersUserResultMap">
SELECT
orders.*, USER
.username ,USER .sex,
USER .address
FROM
orders,
USER
WHERE
orders.user_id =
USER .id
</select>
小结:
实现一对一查询,使用上面两种方式作为输出:
resultType:使用resultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。如果没有查询结果的特殊要求建议使用resultType。 resultMap:需要单独定义resultMap,实现有点麻烦,如果对查询结果有特殊的要求,使用resultMap可以完成将关联查询映射pojo的属性中。 结果上的区别:resultMap可以实现延迟加载,resultType无法实现延迟加载。
相关文章推荐
- 多台机器下使用同一个IOS证书
- MyBatis Review——使用resultType和resultMap实现一对一查询
- linux基础(五)
- 浅析DNS解析过程
- 反思
- Android studio 配置出炫酷logcat颜色
- POJ 2763 Housewife Wind (树链剖分 有修改单边权)
- 回调函数及数组中sort()方法实现排序的原理
- 关于linux Centos 7一个网卡配置多个IP的方法
- 神经网络学习之参数的设置原则
- YYModel 源码解读(二)之YYClassInfo.h (1)
- UI控件笔记(十七):UI之UI阶段控件常用方法大全总结
- 为tableView中的一组cell添加边框
- ios 获取当前屏幕的ViewController
- 发现一个不错的网址,ios大牛级开发学习路线
- Centos7安装完毕后重启提示Initial setup of CentOS Linux 7 (core)的解决方法
- 对象数组
- php数组合并两个数组(一个数组做键名,另一个做值)
- 高德地图-AndroidStadio-No implementation found for long com.autonavi.amap.mapcore.MapCore.nativeNewInsta
- java设计模式——工厂模式