Advanced mapping of Mybatis study notes (6)

Data Model Analysis

1.useryee 和 orders:
useryee --->orders: A user can create multiple orders, one-to-many;
orders --->user: An order is only created by one user, one-to-one.

2.orders 和 orderdetail:
orders ---> orderdetail: An order can include multiple order details, because an order can purchase multiple items, and the purchase information of each item is recorded in orderdetail, a one-to-many relationship;
orderdetail ---> orders: an order detail Can only be included in one order, one for one.

3.orderdetail 和 products:
orderdetail ---> products: an order detail corresponds to only one product information, one-to-one
products ---> orderdetail: a product can be included in multiple order details, one-to-many

4. Tables without relationships at the database level
Then analyze whether there is a business relationship between tables that have no relationship at the database level: A relationship
can be established between orders and products through the orderdetail table;

user and products have a many-to-many relationship.


5. Individual data sheet data





one-to-one query

1. Add the User property to the Orders class:



2.userMapper.xml:




3.UserMapper.java:



4.UserMapperTest.java:



5. Test results:
[Orders [id=1, userId=1, number=1000010, createTime=Thu Apr 19 17:30:29 CST 2018, note=null, user=User [id=1, name=Lawxf, sex=male, address=null]], Orders [id=2, userId=1, number=1000011, createTime=Thu Apr 19 17:30:29 CST 2018, note=null, user=User [id=1, name=Lawxf, sex=male, address=null]], Orders [id=3, userId=2, number=1000012, createTime=Thu Apr 19 17:30:29 CST 2018, note=null, user=User [id=2, name=Yellia, sex=femal, address=null]]]



one-to-many query

1. Orders add the orderDetail property:



2. userMapper.xml:



3.UserMapper.java:



4. UserMapperTest.java:



5. Test results:

[Orders [id=2, userId=1, number=1000011, createTime=Thu Apr 19 17:30:29 CST 2018, note=null, user=User [id=1, name=Lawxf, sex=male, address=null], orderDetails=[OrderDetail [id=3, ordersId=2, productsId=3, productsNum=4], OrderDetail [id=4, ordersId=2, productsId=2, productsNum=3]]], Orders [id=3, userId=2, number=1000012, createTime=Thu Apr 19 17:30:29 CST 2018, note=null, user=User [id=2, name=Yellia, sex=femal, address=null], orderDetails=[OrderDetail [id=1, ordersId=3, productsId=1, productsNum=1], OrderDetail [id=2, ordersId=3, productsId=2, productsNum=3]]]]

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326611283&siteId=291194637