前言
利用Mybatis实现一对一、一对多、多对多的数据模型。
一对一
statement
1 2 3 4 5 6 7 8 9 10 11 12
| <select id="findOrdersUserResultMap" resultMap="OrdersUserResultMap"> SELECT orders.*, user.username, user.sex, user.address FROM orders, user WHERE orders.user_id = user.id </select>
|
resultMap
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
|
<resultMap type="com.iot.mybatis.po.Orders" id="OrdersUserResultMap">
<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="user" javaType="com.iot.mybatis.po.User">
<id column="user_id" property="id"/> <result column="username" property="username"/> <result column="sex" property="sex"/> <result column="address" property="address"/> </association> </resultMap>
|
pojo
在orders类中添加User属性
mapper.java
1 2
| public List<Orders> findOrdersUserResultMap()throws Exception;
|
一对多
statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <select id="findOrdersAndOrderDetailResultMap" resultMap="OrdersAndOrderDetailResultMap"> SELECT orders.*, user.username, user.sex, user.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.orders_id FROM orders, user, orderdetail WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id </select>
|
resultMap
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
|
<resultMap type="com.iot.mybatis.po.Orders" id="OrdersAndOrderDetailResultMap" extends="OrdersUserResultMap">
<collection property="orderdetails" ofType="com.iot.mybatis.po.Orderdetail">
<id column="orderdetail_id" property="id"/> <result column="items_id" property="itemsId"/> <result column="items_num" property="itemsNum"/> <result column="orders_id" property="ordersId"/> </collection>
</resultMap>
|
pojo
在orders中添加list订单明细属性
1 2
| private List<Orderdetail> orderdetails;
|
mapper.java
1 2
| public List<Orders> findOrdersAndOrderDetailResultMap()throws Exception;
|
多对多
statement
映射思路
在user类中添加订单列表属性List orderslist,将用户创建的订单映射到orderslist
在Orders中添加订单明细列表属性Listorderdetials,将订单的明细映射到orderdetials
在OrderDetail中添加Items属性,将订单明细所对应的商品映射到Items
resultMap
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
| <resultMap type="com.iot.mybatis.po.User" id="UserAndItemsResultMap"> <id column="user_id" property="id"/> <result column="username" property="username"/> <result column="sex" property="sex"/> <result column="address" property="address"/>
<collection property="ordersList" ofType="com.iot.mybatis.po.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"/>
<collection property="orderdetails" ofType="com.iot.mybatis.po.Orderdetail"> <id column="orderdetail_id" property="id"/> <result column="items_id" property="itemsId"/> <result column="items_num" property="itemsNum"/> <result column="orders_id" property="ordersId"/>
<association property="items" javaType="com.iot.mybatis.po.Items"> <id column="items_id" property="id"/> <result column="items_name" property="name"/> <result column="items_detail" property="detail"/> <result column="items_price" property="price"/> </association> </collection> </collection> </resultMap>
|
mapper.java
1 2
| public List<User> findUserAndItemsResultMap()throws Exception;
|