今日目标:
- 一对多关联关系
- 多对多关联关系
本篇博客是在前四篇的基础上在进行进一步开发的
目录结构:
准备工作(使用逆向生成工具生成mapper及model)数据库中所需的数据库表,五张表
使用逆向生成工具生成mapper和model
一对多关联关系
利用order和orderitem两个表,来展示一对多的关联关系,一个订单中有多个订单项,多个订单项对应一个订单
1 建立两个vo类来存储一对多的关联属性(OrderVo:订单一方,OrderItemVo:订单项多方)
orderVo
package com.lgs.vo; import com.lgs.model.Order; import com.lgs.model.OrderItem; import java.util.ArrayList; import java.util.List; //vo类是为了不破坏本身的类的封装性,跟数据库中的字段一一映射的 public class OrderVo extends Order { // 一个订单中有多个订单项,所以用集合 private ListorderItems = new ArrayList<>(); public List getOrderItems() { return orderItems; } public void setOrderItems(List orderItems) { this.orderItems = orderItems; } }
OrderItemVo
package com.lgs.vo; import com.lgs.model.Order; import com.lgs.model.OrderItem; public class OrderItemVo extends OrderItem { // 一个订单项对应一个订单 private Order order; public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } }
2 分别在OrderMapper.xml、OrderItemMapper.xml中添加映射关系配置 和 连表查,所以需要两边都要添加一个dao方法及配置sql语句
OrderMapper.xml
OrderItemMapper.xml
2 OrderMapper.java: 和 OrderItemMapper.java:的配置
OrderMapper.java:
OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);
OrderItemMapper.java:
OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
3 建立One2ManyService.java类:
package com.lgs.service; import com.lgs.vo.OrderItemVo; import com.lgs.vo.OrderVo; public interface One2ManyService { //通过查询订单关联查询出对应的订单项信息 OrderVo queryOrderVoByOrderId(Integer orderId); //查询订单项,关联查询出所属的订单 OrderItemVo queryOrderItemVoByOrderItemId(Integer OrderItemId); }
4 建立One2ManyServiceImpl.java实现类:
package com.lgs.service.impl; import com.lgs.mapper.OrderItemMapper; import com.lgs.mapper.OrderMapper; import com.lgs.service.One2ManyService; import com.lgs.vo.OrderItemVo; import com.lgs.vo.OrderVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class One2ManyServiceImpl implements One2ManyService { @Autowired private OrderMapper orderMapper; @Autowired private OrderItemMapper orderItemMapper; @Override public OrderVo queryOrderVoByOrderId(Integer orderId) { return orderMapper.queryOrderVoByOrderId(orderId); } @Override public OrderItemVo queryOrderItemVoByOrderItemId(Integer OrderItemId) { return orderItemMapper.queryOrderItemVoByOrderItemId(OrderItemId); } }
5 测试
package com.lgs.service.impl; import com.lgs.model.OrderItem; import com.lgs.service.One2ManyService; import com.lgs.vo.OrderItemVo; import com.lgs.vo.OrderVo; import junit.framework.TestCase; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class One2ManyServiceImplTest extends TestCase { @Autowired private One2ManyService one2ManyService; @Test public void queryOrderVoByOrderId() { OrderVo orderVo = this.one2ManyService.queryOrderVoByOrderId(10); System.out.println(orderVo); for (OrderItem orderItem : orderVo.getOrderItems()) { System.out.println(orderItem); } } @Test public void queryOrderItemVoByOrderItemId() { OrderItemVo orderItemVo = this.one2ManyService.queryOrderItemVoByOrderItemId(49); System.out.println(orderItemVo); System.out.println(orderItemVo.getOrder()); } }
通过查询订单关联查询出对应的订单项信息
查询订单项, 关联所属的订单
多对多关联关系前面介绍了一对多的关系,那么接下来多对多的映射关系就简单多了,用两个connection配置
为展示多对多的关联关系,利用一本书对应多种类别,一种类别对应多本书的概念
1 建立两个vo类来存储多对多的关联属性(HBookVo:书籍,CategoryVo:书籍种类 都是多方,所以都用集合)
HBookVo.java:
package com.lgs.vo; import com.lgs.model.Category; import com.lgs.model.Hbook; import java.util.ArrayList; import java.util.List; public class HbookVo extends Hbook { private Listcategories = new ArrayList<>(); public List getCategories() { return categories; } public void setCategories(List categories) { this.categories = categories; } }
CategoryVo.java:
package com.lgs.vo; import com.lgs.model.Category; import com.lgs.model.Hbook; import java.util.ArrayList; import java.util.List; public class CategoryVo extends Category { private Listhbooks = new ArrayList<>(); public List getHbooks() { return hbooks; } public void setHbooks(List hbooks) { this.hbooks = hbooks; } }
2 在HBookCategoryMapper.xml中添加映射关系配置 和 连表查,所以需要添加一个dao方法及配置sql语句
HBookCategoryMapper.xml(配置两个多对多和连表查的SQL):
3 HBookCategoryMapper.java(添加两个查询方法):
HbookVo queryByBookId(@Param("bookId") Integer bookId); CategoryVo queryByCid(@Param("cid") Integer cid);
4 建立Many2ManyService.java类:
package com.lgs.service; import com.lgs.vo.CategoryVo; import com.lgs.vo.HbookVo; import org.apache.ibatis.annotations.Param; public interface Many2ManyService { HbookVo queryByBookId(Integer bookId); CategoryVo queryByCid(Integer cid); }
5 建立Many2ManyServiceImpl.java实现类:
package com.lgs.service.impl; import com.lgs.mapper.HbookCategoryMapper; import com.lgs.service.Many2ManyService; import com.lgs.vo.CategoryVo; import com.lgs.vo.HbookVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class Many2ManyServiceImpl implements Many2ManyService { @Autowired private HbookCategoryMapper hbookCategoryMapper; @Override public HbookVo queryByBookId(Integer bookId) { return hbookCategoryMapper.queryByBookId(bookId); } @Override public CategoryVo queryByCid(Integer cid) { return hbookCategoryMapper.queryByCid(cid); } }
6 测试
package com.lgs.service.impl; import com.lgs.model.Category; import com.lgs.model.Hbook; import com.lgs.service.Many2ManyService; import com.lgs.vo.CategoryVo; import com.lgs.vo.HbookVo; import junit.framework.TestCase; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class Many2ManyServiceImplTest extends TestCase { @Autowired private Many2ManyService many2ManyService; @Test public void queryByBookId() { HbookVo hbookVo = many2ManyService.queryByBookId(8); System.out.println(hbookVo); for (Category category : hbookVo.getCategories()) { System.out.println(category); } } @Test public void queryByCid() { CategoryVo categoryVo = many2ManyService.queryByCid(8); System.out.println(categoryVo); for (Hbook hbook : categoryVo.getHbooks()) { System.out.println(hbook); } } }
根据书的id查看书有哪些类别
根据类别查询此书类别的书还有哪些
OK!到这就结束了,希望能帮到你!!!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)