使用java+mybaits技术实现: 订单表和商品表的一对多查询 ,并打印(订单和商品之间关系是多对多关系) 用户表和订单表的一对一查询,并打印(假设一个用户就一个订单
时间: 2023-11-22 16:56:46 浏览: 51
Java工程mybatis实现多表查询过程详解
首先,我们需要创建订单表和商品表的实体类,并在订单表实体类中添加一个商品列表属性,如下所示:
Order.java:
```
public class Order {
private int id;
private int userId;
private List<Product> productList;
// getter and setter methods
}
```
Product.java:
```
public class Product {
private int id;
private String name;
private double price;
// getter and setter methods
}
```
然后,我们需要在数据库中创建订单表、商品表和订单商品关系表,并在订单表中添加一个外键指向订单商品关系表,如下所示:
```
CREATE TABLE `order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_order_user_id` (`user_id`),
CONSTRAINT `fk_order_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`price` double NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `order_product` (
`order_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
PRIMARY KEY (`order_id`,`product_id`),
KEY `fk_order_product_order_id` (`order_id`),
KEY `fk_order_product_product_id` (`product_id`),
CONSTRAINT `fk_order_product_order_id` FOREIGN KEY (`order_id`) REFERENCES `order` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_order_product_product_id` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
接下来,我们需要在MyBatis中配置两个Mapper接口,分别实现订单表和商品表的查询,如下所示:
OrderMapper.java:
```
public interface OrderMapper {
Order findOrderById(int id);
}
```
ProductMapper.java:
```
public interface ProductMapper {
List<Product> findProductsByOrderId(int orderId);
}
```
然后,我们需要在OrderMapper.xml中实现查询订单表和商品表的一对多关系,如下所示:
```
<select id="findOrderById" resultType="Order">
SELECT o.id, o.user_id, p.id AS product_id, p.name, p.price
FROM `order` o
INNER JOIN order_product op ON o.id = op.order_id
INNER JOIN product p ON op.product_id = p.id
WHERE o.id = #{id}
</select>
```
最后,我们需要在UserMapper中实现查询用户表和订单表的一对一关系,如下所示:
UserMapper.java:
```
public interface UserMapper {
User findUserById(int id);
}
```
UserMapper.xml:
```
<select id="findUserById" resultType="User">
SELECT u.id, u.name, u.email, o.id AS order_id
FROM user u
LEFT JOIN `order` o ON u.id = o.user_id
WHERE u.id = #{id}
</select>
```
最后,我们可以在Java代码中调用这些Mapper接口,并打印查询结果,如下所示:
```
OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 查询订单表和商品表的一对多关系
Order order = orderMapper.findOrderById(1);
List<Product> productList = productMapper.findProductsByOrderId(order.getId());
order.setProductList(productList);
System.out.println("订单信息:");
System.out.println(order.getId() + ": " + order.getProductList());
// 查询用户表和订单表的一对一关系
User user = userMapper.findUserById(1);
System.out.println("用户信息:");
System.out.println(user.getId() + ": " + user.getOrder().getId());
```
阅读全文