MyBatis的一对一,一对多,多对多的操作过程。
1.数据库表结构
t_user
CREATE TABLE `t_user` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=gbk
t_items
CREATE TABLE `t_items` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`price` varchar(10) DEFAULT NULL,
`detail` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk
t_orders
CREATE TABLE `t_orders` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`userid` int(10) NOT NULL,
`des` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `userid` (`userid`),
CONSTRAINT `t_orders_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `t_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk
t_ordersdetail
CREATE TABLE `t_ordersdetail` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`ordersid` int(10) NOT NULL,
`itemsid` int(10) NOT NULL,
`itemsnum` int(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `ordersid` (`ordersid`),
KEY `itemsid` (`itemsid`),
CONSTRAINT `t_ordersdetail_ibfk_1` FOREIGN KEY (`ordersid`) REFERENCES `t_user` (`id`),
CONSTRAINT `t_ordersdetail_ibfk_2` FOREIGN KEY (`itemsid`) REFERENCES `t_items` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gbk
2.一对一查询需求:查询一个订单对应的用户
在OrdersMapperCustom.xml中的配置:
在OrdersMapperCustom.java中的配置
//一对一查询
public List findOrderToUser();
在测试类中
//一对一查询测试
@Test
public void testOnetoOne() throws IOException {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建SqlsessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sessionFactory.openSession();
OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class);
List orders = ordersMapperCustom.findOrderToUser();
System.out.println(orders);
}
3.一对多的需求:查询一个订单对于多个订单详情
在Orders类中增加orderdetail的集合
在OrdersMapperCustom.xml中的配置:
在OrdersMapperCustom.java中的配置:
//一对多查询
public List findOdersAndOrdersDetail();
测试类:
//一对多查询测试
@Test
public void testOnetoMany() throws IOException {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建SqlsessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sessionFactory.openSession();
OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class);
List orders = ordersMapperCustom.findOdersAndOrdersDetail();
System.out.println(orders);
for(Orders order : orders) {
System.out.println(order.getId());
for(OrdersDetail detail: order.getOrdersDetaits()) {
System.out.println(detail.getOrdersid());
}
}
}
4.多对多需求:查询用户且与其对应购买的商品信息
在User类中添加商品信息
在OrdersMapperCustom.xml中的配置:
在OrdersMapperCustom.java中的配置:
//多对多查询
public List findUsertoitems();
测试类:
//多对多查询测试
@Test
public void testManytoMany() throws IOException {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建SqlsessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sessionFactory.openSession();
OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class);
List users = ordersMapperCustom.findUsertoitems();
System.out.println(users);
}
注意:一对一时使用的是
对多时使用的是
5.延迟加载的xml配置:
初始化时只执行select*from t_orders,当只有需要用到user时,会调用select="Mapper.UserMapper.findUserById"中的select * from t_user where id ="#{id}"这条语句。