需求分析
1.订单商品数据模型
(1).表
用户表user:记录了购买商品的用户信息
订单表orders:记录了用户所创建的订单信息
订单明细表orderdetail:记录了订单的详细信息
商品表item:记录了商品详细信息
(2).表与表之间的业务关系
在分析表与表之间的业务关系时,需要建立在某个具体业务的场景,先分析数据库级别之间有关系的表之间的业务关系。
- user —> orders:一个用户可以创建多个订单,一对多
- orders —> user:一个订单只由一个用户创建,一对一
- orders —> orderdetail:一个订单可以包括多个订单明细,一对多,因为一个订单可以购买多个商品,每个商品的购买信息存在orderdetail
- orderdetail —> orders:一个订单明细只能包括在一个订单中,一对一
- orderdetail —> itesms:一个订单明细只对应一个商品信息,一对一
- items –> orderdetail:一个商品可以包括在多个订单明细 ,一对多
2.数据准备
(1).订单表orders
CREATE TABLE `orders` (
`order_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`order_no` varchar(100) NOT NULL,
`note` varchar(100) NOT NULL,
`create_time` datetime(6) NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into orders values(1,1,"20221207173000000001","Steven买了一台电脑和两部手机","2022-12-07 17:30:00");
insert into orders values(2,2,"20221207173500000002","Sherry买了一部手机和两台电脑","2022-12-07 17:35:00");
(2).订单详情表orderdetail
CREATE TABLE `orderdetail` (
`order_detail_id` int(11) NOT NULL,
`order_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
`item_num` int(11) NOT NULL,
`create_time` datetime(6) NOT NULL,
PRIMARY KEY (`order_detail_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into orderdetail values(1,1,1,1,"2022-12-07 17:30:00");
insert into orderdetail values(2,1,2,2,"2022-12-07 17:30:00");
insert into orderdetail values(3,2,1,2,"2022-12-07 17:35:00");
insert into orderdetail values(4,2,2,1,"2022-12-07 17:35:00");
(3).商品表item
CREATE TABLE `item` (
`item_id` int(11) NOT NULL,
`item_name` varchar(100) NOT NULL,
`item_price` decimal(6,2) NOT NULL,
`create_time` datetime(6) NOT NULL,
PRIMARY KEY (`item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into item values(1,"电脑",9999.99,"2022-12-07 00:00:00");
insert into item values(2,"手机",6666.66,"2022-12-07 00:00:00");
关联查询
1.一对一(查询所有订单信息,关联查询下单用户信息)
(1).修改SQL映射文件
在mybatis.xml文件中,修改SQL映射文件,具体代码如下。
<!-- 2.在全局配置文件中加载SQL映射文件 -->
<mappers>
<mapper resource="mapper/OrdersUserMapper.xml"/>
</mappers>
(2).编写po类
在po目录下新建Orders类,具体代码如下。
public class Orders {
private Integer orderId;
private Integer userId;
private String orderNo;
private String note;
private Date createTime;
//get和set方法......
}
在bo目录下新建OrdersUser类,具体代码如下。
public class OrdersUser extends Orders {
private User user;
//get和set方法......
}
(3).编写Mapper接口
并在mapper目录下新建OrdersUserMapper接口,具体代码如下。
public interface OrdersUserMapper {
List<OrdersUser> findOrdersUser();
}
(4).编写SQL映射
在mapper目录下新建OrdersUserMapper.xml文件,具体代码如下。
<?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="com.steven.mapper.OrdersUserMapper">
<resultMap id="ordersUserMap" type="com.steven.bo.OrdersUser">
<id column="order_id" property="orderId"/>
<result column="order_no" property="orderNo"/>
<result column="note" property="note"/>
<association javaType="com.steven.po.User" property="user">
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="address" property="address" />
</association>
</resultMap>
<select id="findOrdersUser" resultMap="ordersUserMap">
SELECT a.*,b.username,b.address
FROM orders as a join user as b
on a.user_id = b.id
</select>
</mapper>
(5).编写测试类
Test类中调整获取Mapper和业务操作代码,具体代码如下。
public class Test {
public static void main(String[] args) throws Exception {
//1.获取MyBatis全局配置文件流
String resource = "config/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//2.根据MyBatis配置信息创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3.获取会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.获取Mapper
OrdersUserMapper ordersUserMapper = sqlSession.getMapper(OrdersUserMapper.class);
//5.业务操作
List<OrdersUser> ordersUserList = ordersUserMapper.findOrdersUser();
Iterator<OrdersUser> iterator = ordersUserList.iterator();
while (iterator.hasNext()) {
OrdersUser ordersUser = iterator.next();
System.out.println("orderNo:" + ordersUser.getOrderNo());
System.out.println("note:" + ordersUser.getNote());
System.out.println("username:" + ordersUser.getUser().getUsername());
System.out.println("address:" + ordersUser.getUser().getAddress());
System.out.println();
}
sqlSession.close();
}
}
orderNo:20221207173000000001
note:Steven买了一台电脑和两部手机
username:steven
address:nanjing
orderNo:20221207173500000002
note:Sherry买了一部手机和两台电脑
username:sherry
address:taizhou
(6).工程目录
2.一对多(查询所有订单信息关联查询订单明细信息)
(1).修改SQL映射文件
在mybatis.xml文件中,修改SQL映射文件,具体代码如下。
<!-- 2.在全局配置文件中加载SQL映射文件 -->
<mappers>
<mapper resource="mapper/OrdersOrderDetailMapper.xml"/>
</mappers>
(2).编写po类
在po目录下新建Orders类,具体代码如下。
public class OrderDetail {
private Integer orderDetailId;
private Integer orderId;
private Integer itemId;
private Integer itemNum;
private Date createTime;
//get和set方法......
}
在bo目录下新建OrdersOrderDetail类,具体代码如下。
public class OrdersUser extends Orders {
List<OrderDetail> orderDetailList;
//get和set方法......
}
(3).编写Mapper接口
并在mapper目录下新建OrdersOrderDetailMapper接口,具体代码如下。
public interface OrdersUserMapper {
List<OrdersOrderDetail> getOrdersOrderDetail();
}
(4).编写SQL映射
在mapper目录下新建OrdersOrderDetailMapper.xml文件,具体代码如下。
<?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="com.steven.mapper.OrdersOrderDetailMapper">
<resultMap id="ordersUserOrderDetailMap" type="com.steven.bo.OrdersOrderDetail">
<id column="order_id" property="orderId"/>
<result column="order_no" property="orderNo"/>
<result column="note" property="note"/>
<collection ofType="com.steven.po.OrderDetail" property="orderDetailList">
<id column="order_detail_id" property="orderDetailId"/>
<result column="orderDetail_id" property="orderDetailId"/>
<result column="order_id" property="orderId"/>
<result column="item_id" property="itemId"/>
<result column="item_num" property="itemNum"/>
</collection>
</resultMap>
<select id="getOrdersOrderDetail" resultMap="ordersUserOrderDetailMap">
select a.*,b.username,b.address,c.item_id,c.item_num
from orders as a inner join user as b on a.user_id = b.id
inner join orderDetail as c on a.order_id = c.order_id
</select>
</mapper>
(5).编写测试类
Test类中调整获取Mapper和业务操作代码,具体代码如下。
public class Test {
public static void main(String[] args) throws Exception {
//1.获取MyBatis全局配置文件流
String resource = "config/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//2.根据MyBatis配置信息创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3.获取会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.获取Mapper
OrdersOrderDetailMapper ordersOrderDetailMapper = sqlSession.getMapper(OrdersOrderDetailMapper.class);
//5.业务操作
List<OrdersOrderDetail> ordersOrderDetailList = ordersOrderDetailMapper.getOrdersOrderDetail();
Iterator<OrdersOrderDetail> oodIterator = ordersOrderDetailList.iterator();
while (oodIterator.hasNext()) {
OrdersOrderDetail ordersOrderDetail = oodIterator.next();
System.out.println("orderNo:" + ordersOrderDetail.getOrderNo());
System.out.println("note:" + ordersOrderDetail.getNote());
List<OrderDetail> orderDetailList = ordersOrderDetail.getOrderDetailList();
System.out.println("orderDetailList:" + orderDetailList.size()+"条");
Iterator<OrderDetail> odIterator = orderDetailList.iterator();
while (odIterator.hasNext()) {
OrderDetail orderDetail = odIterator.next();
System.out.print("orderId:" + orderDetail.getOrderId());
System.out.print(",itemId:" + orderDetail.getItemId());
System.out.println(",itemNum:" + orderDetail.getItemNum());
}
System.out.println();
}
sqlSession.close();
}
}
orderNo:20221207173000000001
note:Steven买了一台电脑和两部手机
orderDetailList:2条
orderId:1,itemId:1,itemNum:1
orderId:1,itemId:2,itemNum:2
orderNo:20221207173500000002
note:Sherry买了一部手机和两台电脑
orderDetailList:2条
orderId:2,itemId:1,itemNum:2
orderId:2,itemId:2,itemNum:1
(6).工程目录