用戶與訂單時一對多關係,再加上商品信息的話,訂單與商品之間就是多對多關係了 sql DROP DATABASE IF EXISTS testdb; USE testdb; / 用戶表,記錄用戶信息;用戶與訂單為一對多關係:一個用戶可擁有多個訂單 / DROP TABLE IF EXISTS ; CR ...
用戶與訂單時一對多關係,再加上商品信息的話,訂單與商品之間就是多對多關係了
DROP DATABASE IF EXISTS testdb;
USE testdb;
/*用戶表,記錄用戶信息;用戶與訂單為一對多關係:一個用戶可擁有多個訂單*/
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE tb_user(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(18),
loginname VARCHAR(18),
PASSWORD VARCHAR(18),
phone VARCHAR(18),
address VARCHAR(18)
);
INSERT INTO tb_user(username,loginname,PASSWORD,phone,address) VALUES('傑克','jack','123456','13920001616','廣州');
/*商品信息表;商品與訂單為多對多關係:一個訂單可有多個商品,一個商品也可出現在多個訂單中*/
DROP TABLE IF EXISTS `tb_article`;
CREATE TABLE tb_article(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(32),
price DOUBLE,
remark VARCHAR(18)
);
INSERT INTO tb_article(NAME,price,remark) VALUES('瘋狂Java講義',108.9,'李剛老師經典著作');
INSERT INTO tb_article(NAME,price,remark) VALUES('瘋狂Android講義',99.9,'李剛老師經典著作');
INSERT INTO tb_article(NAME,price,remark) VALUES('瘋狂iOS講義',89.9,'李剛老師經典著作');
INSERT INTO tb_article(NAME,price,remark) VALUES('SpringMVC+MyBatis企業開發',69.9,'肖文吉老師經典著作');
/*訂單表,記錄訂單編號,總金額,所屬用戶的id;訂單與用戶為多對一關係,與商品為多對多關係*/
DROP TABLE IF EXISTS `tb_order`;
CREATE TABLE tb_order(
id INT PRIMARY KEY AUTO_INCREMENT,
CODE VARCHAR(32),
total DOUBLE,
user_id INT,
FOREIGN KEY (user_id) REFERENCES tb_user(id)
);
INSERT INTO tb_order(CODE,total,user_id) VALUES('6aa3fa359ff14619b77fab5990940a2d',388.6,1);
INSERT INTO tb_order(CODE,total,user_id) VALUES('6aa3fa359ff14619b77fab5990940b3c',217.8,1);
/*中間表,用於記錄訂單對應的商品id*/
DROP TABLE IF EXISTS `tb_item`;
CREATE TABLE tb_item(
order_id INT,
article_id INT,
amount INT,
PRIMARY KEY(order_id,article_id),
FOREIGN KEY (order_id) REFERENCES tb_order(id),
FOREIGN KEY (article_id) REFERENCES tb_article(id)
);
INSERT INTO tb_item(order_id,article_id,amount) VALUES(1,1,1);
INSERT INTO tb_item(order_id,article_id,amount) VALUES(1,2,1);
INSERT INTO tb_item(order_id,article_id,amount) VALUES(1,3,2);
INSERT INTO tb_item(order_id,article_id,amount) VALUES(2,4,2);
INSERT INTO tb_item(order_id,article_id,amount) VALUES(2,1,1);
實體類User
package net.sonng.manytomany;
import java.util.List;
public class User {
private int id;
private String username;
private String loginname;
private String password;
private String phone;
private String address;
private List<Order> orders; //用戶與訂單:一對多關係,一個用戶可有多個訂單
//......
}
實體類Order
package net.sonng.manytomany;
import java.util.List;
public class Order {
private int id;
private String code;
private double total;
private int user_id;
private User user; //一個訂單隻屬於一個用戶
private List<Article> articles; //訂單與商品:多對多關係,一個訂單可包含多個商品,一個商品也可以出現在多個訂單中
//.....
}
實體類Article
package net.sonng.manytomany;
import java.util.List;
public class Article {
private int id;
private String name;
private double price;
private String remark;
private List<Order> orders; //商品與訂單:多對多關係
//.....
}
根據用戶id查詢用戶信息,並將其所有訂單信息一併查詢出來
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTDMapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="net.sonng.manytomany.UserDao">
<select id="selectUserById" parameterType="int" resultMap="userMap">
SELECT * FROM tb_user WHERE id=#{user_id}
</select>
<resultMap id="userMap" type="net.sonng.manytomany.User">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="loginname" column="loginname" />
<result property="password" column="password" />
<result property="phone" column="phone" />
<result property="address" column="address" />
<!-- 用戶與訂單一對多關係,用collection -->
<collection property="orders" column="id" javaType="ArrayList" ofType="net.sonng.manytomany.Order" select="net.sonng.manytomany.OrderDao.selectOrderByUserId" >
<id property="id" column="id" />
<result property="code" column="code" />
<result property="total" column="total" />
</collection>
</resultMap>
</mapper>
根據訂單id查詢訂單信息,一併將其所屬的用戶信息和包含的商品信息查詢出來
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTDMapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="net.sonng.manytomany.OrderDao">
<select id="selectOrderByUserId" parameterType="int" resultType="net.sonng.manytomany.Order" >
SELECT * FROM tb_order WHERE user_id=#{user_id}
</select>
<select id="selectOrderById" parameterType="int" resultMap="orderMap">
SELECT u.*,o.id AS oid,code,total,user_id FROM tb_order o,tb_user u WHERE o.user_id=u.id AND o.id=#{order_id}
</select>
<resultMap type="net.sonng.manytomany.Order" id="orderMap" >
<id property="id" column="id" />
<result property="code" column="code" />
<result property="total" column="total" />
<association property="user" javaType="net.sonng.manytomany.User">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="loginname" column="loginname" />
<result property="password" column="password" />
<result property="phone" column="phone" />
<result property="address" column="address" />
</association>
<collection property="articles" column="id" javaType="ArrayList" ofType="net.sonng.manytomany.Article" select="net.sonng.manytomany.ArticleDao.selectArticleByOrderId" >
<id property="id" column="id" />
<result property="name" column="name" />
<result property="price" column="price" />
<result property="remark" column="remark" />
</collection>
</resultMap>
</mapper>
根據訂單id,先從中間表item中查詢其包含的商品id,再查詢這些商品的信息
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTDMapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="net.sonng.manytomany.ArticleDao">
<select id="selectArticleByOrderId" parameterType="int" resultType="net.sonng.manytomany.Article">
SELECT * FROM tb_article WHERE id IN (SELECT article_id FROM tb_item WHERE order_id=#{order_id})
</select>
</mapper>
測試類
package net.sonng.test;
import java.util.List;
import net.sonng.manytomany.Article;
import net.sonng.manytomany.ArticleDao;
import net.sonng.manytomany.Order;
import net.sonng.manytomany.OrderDao;
import net.sonng.manytomany.User;
import net.sonng.manytomany.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args){
ApplicationContext ac=new ClassPathXmlApplicationContext("ac.xml");
System.out.println("測試用戶查詢:根據用戶id查詢用戶信息及其訂單信息");
UserDao userDao=ac.getBean("userDao",UserDao.class);
User user=userDao.selectUserById(1);
System.out.println("------用戶信息------");
System.out.println(user);
List<Order> orders=user.getOrders();
System.out.println("------該用戶下的訂單信息------");
for (Order order:orders){
System.out.println(order);
}
System.out.println("測試訂單查詢:根據訂單id查詢訂單信息,所屬用戶信息,訂單包含的商品信息");
OrderDao orderDao=ac.getBean("orderDao",OrderDao.class);
Order order=orderDao.selectOrderById(2);
System.out.println("------訂單信息------");
System.out.println(order);
System.out.println("------訂單所屬用戶信息------");
System.out.println(order.getUser());
List<Article> articles=order.getArticles();
System.out.println("------訂單包含的商品信息------");
for (Article article:articles) {
System.out.println(article);
}
}
}
輸出:
測試用戶查詢:根據用戶id查詢用戶信息及其訂單信息
------用戶信息------
User [id=1, username=傑克, loginname=jack, password=123456, phone=13920001616, address=廣州]
------該用戶下的訂單信息------
Order [id=1, code=6aa3fa359ff14619b77fab5990940a2d, total=388.6, user_id=1]
Order [id=2, code=6aa3fa359ff14619b77fab5990940b3c, total=217.8, user_id=1]
測試訂單查詢:根據訂單id查詢訂單信息,所屬用戶信息,訂單包含的商品信息
------訂單信息------
Order [id=1, code=6aa3fa359ff14619b77fab5990940b3c, total=217.8, user_id=0]
------訂單所屬用戶信息------
User [id=1, username=傑克, loginname=jack, password=123456, phone=13920001616, address=廣州]
------訂單包含的商品信息------
Article [id=1, name=瘋狂Java講義, price=108.9, remark=李剛老師經典著作]
Article [id=2, name=瘋狂Android講義, price=99.9, remark=李剛老師經典著作]
Article [id=3, name=瘋狂iOS講義, price=89.9, remark=李剛老師經典著作]