-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathOrderDao.xml
More file actions
72 lines (65 loc) · 4.04 KB
/
Copy pathOrderDao.xml
File metadata and controls
72 lines (65 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.devmgr.tutorial.OrderDao" >
<resultMap id="wholeOrderMap" type="cn.devmgr.tutorial.model.Order">
<id property="id" column="id" />
<result property="orderDate" column="order_date" />
<result property="orderType" column="order_type" typeHandler="org.apache.ibatis.type.EnumTypeHandler" />
<association property="consigneeAddress" javaType="cn.devmgr.tutorial.model.ConsigneeAddress" resultMap="orderAddress" />
<collection property="orderItems" column="id" ofType="cn.devmgr.tutorial.model.OrderItem" select="selectOrderItem">
</collection>
</resultMap>
<resultMap id="orderAddress" type="cn.devmgr.tutorial.model.ConsigneeAddress">
<result property="consignee" column="consignee" />
<result property="phone" column="phone" />
<result property="province" column="province" />
<result property="city" column="city" />
<result property="district" column="district" />
<result property="address" column="address" />
</resultMap>
<sql id="orderMainColumnsWithoutId"> consignee, phone, province, city, district, address, order_date, order_type, status</sql>
<select id="getOrderById" resultMap="wholeOrderMap">
select id, <include refid="orderMainColumnsWithoutId" />
from order_main
where id=#{id}
</select>
<select id="selectOrderItem" resultType="cn.devmgr.tutorial.model.OrderItem">
select id, order_id, product_id, product_name, num, price
from order_detail
where order_id=#{id}
</select>
<!-- 多个insert sql时,useGeneratedKeys="true" keyProperty="order.id" 不起作用,1个起作用
selectKey会起作用
-->
<insert id="insertOrder" useGeneratedKeys="true" keyProperty="order.id" parameterType="cn.devmgr.tutorial.model.Order">
<selectKey keyProperty="order.id" resultType="int">
select currval('order_main_id_seq');
</selectKey>
insert into order_main(<include refid="orderMainColumnsWithoutId" />)
values(#{order.consigneeAddress.consignee}, #{order.consigneeAddress.phone}, #{order.consigneeAddress.province},
#{order.consigneeAddress.city}, #{order.consigneeAddress.district}, #{order.consigneeAddress.address},
#{order.orderDate}, #{order.orderType, typeHandler=org.apache.ibatis.type.EnumTypeHandler}, 0);
<foreach collection="order.orderItems" item="item" index="index" open="" separator=";" close="">
insert into order_detail(order_id, product_id, product_name, num, price)
values(currval('order_main_id_seq'), #{item.productId}, #{item.productName}, #{item.num}, #{item.price});
</foreach>
</insert>
<!-- 查询product, 演示了使用ArrayTypeHandler和自定义的JsonTypeHandler -->
<resultMap type="cn.devmgr.tutorial.model.Product" id="product">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="price" property="price"/>
<result column="specs" property="specs" typeHandler="cn.devmgr.tutorial.typehandler.JsonTypeHandler" />
<result column="images" property="images" typeHandler="org.apache.ibatis.type.ArrayTypeHandler" />
</resultMap>
<select id="selectProductById" resultMap="product">
select id, name, price, specs, images
from product
where id=#{id}
</select>
<!-- 在insert中使用typeHandler;注意数组用的是自定义的ArrayTypeHandler -->
<insert id="insertProduct" parameterType="cn.devmgr.tutorial.model.Product">
insert into product(id, name, price, specs, images)
values(#{product.id}, #{product.name}, #{product.price}, #{product.specs, typeHandler=cn.devmgr.tutorial.typehandler.JsonTypeHandler}, #{product.images, typeHandler=cn.devmgr.tutorial.typehandler.ArrayTypeHandler} )
</insert>
</mapper>