使用resultMap实现ibatis复合数据结构查询(1.多重属性查询;2.属性中含有列表查询)
2017-07-05 19:41
471 查看
以订单为例(订单详情包括了订单的基本信息,配送物流信息,商品信息),直接上代码:
1.多重属性查询
java实体
class="orderDetail" 即为上面的类,有个属性logistics的类型是复合类型Logistics
以下是订单详情的resultMap:
当中"<result property="logistics.consignee" column="consignee"/> ",logistics.consignee就能够用对象訪问的写法来查询,consignee是logistics的属性,logistics是orderDetail的属性.注意:column的名称应该与sql语句查询的列名一致.
sql查询语句例如以下:
2.属性中含有列表查询
orderDetail有个属性orderGoods的类型是List复合类型,应在resultMap的属性定义中加入属性select="getOrderGoodsList",getOrderGoodsList也是一个sql查询
注意:上面column="orderSn"指的是orderSn作为sql的查询參数. 看下这个 getOrderGoodsList查询
1.多重属性查询
java实体
public class OrderDetail { @XmlElement(required = true) protected String orderSn; @XmlElement(required = true) protected String orderAmount; @XmlElement(required = true) protected String orderStatus; @XmlElement(required = true) protected String orderAddTime; @XmlElement(required = true) protected Logistics logistics; @XmlElement(required = true) protected OrderGoods orderGoods; @XmlElement(required = true) protected List<OrderDetail> listData; <pre name="code" class="java"> ... getter and setter...
}
class="orderDetail" 即为上面的类,有个属性logistics的类型是复合类型Logistics
以下是订单详情的resultMap:
<resultMap id="OrderInfoMap" class="orderDetail"> <result property="orderSn" column="orderSn"/> <result property="orderAmount" column="orderAmount"/> <result property="orderStatus" column="orderStatus"/> <result property="orderStatus" column="orderAddTime"/> <result property="logistics.consignee" column="consignee"/> <!-- <result property="logistics.receiver" column="receiver"/> --> <result property="logistics.country" column="country"/> <result property="logistics.province" column="province"/> <result property="logistics.city" column="city"/> <result property="logistics.district" column="district"/> <result property="logistics.address" column="address"/> <result property="logistics.mobile" column="mobile"/> <result property="logistics.shippingName" column="shippingName"/> <result property="orderGoods.listData" select="wxShopOrder.getOrderGoodsList" column="orderSn"/> </resultMap>
当中"<result property="logistics.consignee" column="consignee"/> ",logistics.consignee就能够用对象訪问的写法来查询,consignee是logistics的属性,logistics是orderDetail的属性.注意:column的名称应该与sql语句查询的列名一致.
sql查询语句例如以下:
<select id="getOrderDetail" parameterClass="string" resultMap="OrderInfoMap" > select order_sn as orderSn, order_amount as orderAmount, order_status as orderStatus, add_time as orderAddTime, consignee, country, b.region_name province, c.region_name city, d.region_name district, address, mobile, shipping_name as shippingName from ecs_order_info o left join ecs_region b on o.province=b.region_id left join ecs_region c on o.city=c.region_id left join ecs_region d on o.district=d.region_id where order_sn=#orderSn# </select>
2.属性中含有列表查询
orderDetail有个属性orderGoods的类型是List复合类型,应在resultMap的属性定义中加入属性select="getOrderGoodsList",getOrderGoodsList也是一个sql查询
<result property="orderGoods.listData" select="wxShopOrder.getOrderGoodsList" column="orderSn"/>
注意:上面column="orderSn"指的是orderSn作为sql的查询參数. 看下这个 getOrderGoodsList查询
<!-- 通过订单号获取订单商品列表 --> <select id="getOrderGoodsList" parameterClass="string" resultMap="orderGoodsMap"> select goods_name as goodsName, goods_number as goodsNumber from ecs_order_goods where order_id= (select order_id from ecs_order_info where order_sn=#orderSn#) </select>
相关文章推荐
- 使用resultMap实现ibatis复合数据结构查询(1.多重属性查询;2.属性中含有列表查询)
- mybatis使用resultMap实现多对多查询 (需求:商品信息和订单明细有多对多的关系)
- ibatis resultMap groupBy属性使用
- MyBatis Review——使用resultType和resultMap实现一对一查询
- ibatis使用resultMap处理级联查询
- MyBatis Review——使用resultType和resultMap实现一对一查询
- mybatis 使用resultMap实现关联数据的查询(association 和collection )
- iBatis resultMap groupBy属性使用心得[转载]
- sharepoint之-使用DataView与Form Web Part实现列表的关键字查询
- iBatis的AutoResultMap 默认配置使用时候出现的问题
- Ibatis的resultMap和查询数据的对应关系
- 使用plist(属性列表)实现数据读写
- mybatis处理查询map列表属性为null的问题,而导致查询map无该key对象
- ibatis Map作为resultClass時,必須指定具体的实现类
- [SharePoint Designer技巧-1]使用DataView与Form Web Part实现列表的关键字查询
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- [Java Web]ibatis使用queryForMap实现数据查找等操作
- 使用DataView与Form Web Part实现列表的关键字查询
- 动手改造Ibatis,使其支持文件系统存储数据列 之 看我如何给ResultMap增加属性