mybatis关联关系映射
一对多
首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper
实体类
OrderVo
package com.huang.model.vo; import com.huang.model.Order; import com.huang.model.OrderItem; import java.util.ArrayList; import java.util.List; /** * @auther 宇晨 * @company * @create 2019-10-23-12:01 */ public class OrderVo extends Order { private List<OrderItem> orderItems=new ArrayList<>(); public List<OrderItem> getOrderItems() { return orderItems; } public void setOrderItems(List<OrderItem> orderItems) { this.orderItems = orderItems; } }
OrderItemVo
package com.huang.model.vo; import com.huang.model.Order; import com.huang.model.OrderItem; /** * @auther 宇晨 * @company * @create 2019-10-23-12:03 */ public class OrderItemVo extends OrderItem { private Order order; public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } }
OrderMapper.xml
通过一个订单id,查询出订单信息的同时,查询出所有的订单项
<!--配置一对多关联关系映射--> <resultMap id="OrderVoMap" type="com.huang.model.vo.OrderVo"> <result property="orderId" column="order_id"></result> <result property="orderNo" column="order_no"></result> <!-- 一对多的关系 --> <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 --> <collection property="orderItems" ofType="com.huang.model.OrderItem"> <result property="orderItemId" column="order_item_id"></result> <result property="productId" column="product_id"></result> <result property="quantity" column="quantity"></result> <result property="oid" column="oid"></result> </collection> </resultMap> <!-- 一对多查询语句--> <select id="selectByOrderId" resultMap="OrderVoMap" parameterType="java.lang.Integer" > select * from t_hibernate_order o,t_hibernate_order_item oi where o.order_id=oi.oid and oi.oid=#{orderId} </select>
OrderMapper.java
package com.huang.mapper; import com.huang.model.Order; import com.huang.model.vo.OrderVo; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface OrderMapper { int deleteByPrimaryKey(Integer orderId); int insert(Order record); int insertSelective(Order record); Order selectByPrimaryKey(Integer orderId); int updateByPrimaryKeySelective(Order record); int updateByPrimaryKey(Order record); List<OrderVo> selectByOrderId(@Param("orderId")Integer orderId); }
OrderItemMapper.xml
通过一个订单id项,查询出订单id项信息的同时,查询出所属订单
<resultMap id="OrderItemVoMap" type="com.huang.model.vo.OrderItemVo"> <result property="orderItemId" column="order_item_id"></result> <result property="productId" column="product_id"></result> <result property="quantity" column="quantity"></result> <result property="oid" column="oid"></result> <!--<result property="orderItems"></result>--> <association property="order" javaType="com.huang.model.Order"> <result property="orderId" column="order_id"></result> <result property="orderNo" column="order_no"></result> </association> </resultMap> <select id="selectByOrderItemId" resultMap="OrderItemVoMap" parameterType="java.lang.Integer" > select * from t_hibernate_order o,t_hibernate_order_item oi where o.order_id=oi.oid and oi.order_item_id=#{orderItemId} </select>
OrderItemMapper.java
package com.huang.mapper; import com.huang.model.OrderItem; import com.huang.model.vo.OrderItemVo; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface OrderItemMapper { int deleteByPrimaryKey(Integer orderItemId); int insert(OrderItem record); int insertSelective(OrderItem record); OrderItem selectByPrimaryKey(Integer orderItemId); int updateByPrimaryKeySelective(OrderItem record); int updateByPrimaryKey(OrderItem record); List<OrderItemVo> selectByOrderItemId(@Param("orderItemId")Integer orderItemId); }
OneToManyService
package com.huang.service; import com.huang.model.vo.OrderItemVo; import com.huang.model.vo.OrderVo; import java.util.List; /** * @auther 宇晨 * @company * @create 2019-10-23-18:19 */ public interface OneToManyService { List<OrderVo> selectByOrderId(Integer orderId); List<OrderItemVo> selectByOrderItemId(Integer orderItemId); }
OneToManyServiceImpl
package com.huang.service.impl; import com.huang.mapper.OrderItemMapper; import com.huang.mapper.OrderMapper; import com.huang.model.vo.OrderItemVo; import com.huang.model.vo.OrderVo; import com.huang.service.OneToManyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @auther 宇晨 * @company * @create 2019-10-23-18:38 */ @Service public class OneToManyServiceImpl implements OneToManyService { @Autowired private OrderMapper orderMapper; @Autowired private OrderItemMapper orderItemMapper; @Override public List<OrderVo> selectByOrderId(Integer orderId) { return orderMapper.selectByOrderId(orderId); } @Override public List<OrderItemVo> selectByOrderItemId(Integer orderItemId) { return orderItemMapper.selectByOrderItemId(orderItemId); } }
测试
OneToManyServiceImplTest
package com.huang.service.impl; import com.huang.SpringBaseTest; import com.huang.model.OrderItem; import com.huang.model.vo.OrderItemVo; import com.huang.model.vo.OrderVo; import com.huang.service.OneToManyService; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import java.util.List; /** * @auther 宇晨 * @company * @create 2019-10-23-18:43 */ public class OneToManyServiceImplTest extends SpringBaseTest { @Autowired private OneToManyService oneToManyService; @Test public void selectByOrderId() { List<OrderVo> orderVos = oneToManyService.selectByOrderId(8); OrderVo orderVo=orderVos.get(0); System.out.println(orderVos); for (OrderItem orderItem : orderVo.getOrderItems()) { System.out.println(orderItem); } } @Test public void selectByOrderItemId() { List<OrderItemVo> orderItemVos = oneToManyService.selectByOrderItemId(36); OrderItemVo orderItemVo=orderItemVos.get(0); System.out.println(orderItemVo); System.out.println(orderItemVo.getOrder()); } }
运行结果
selectByOrderId
selectByOrderItemId
多对多
首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,
这两张表对应的model与mapper
实体类
HbookVo
package com.huang.model.vo; import com.huang.model.Category; import com.huang.model.Hbook; import java.util.ArrayList; import java.util.List; /** * @auther 宇晨 * @company * @create 2019-10-23-19:47 */ public class HbookVo extends Hbook { private List<Category> categories=new ArrayList<>(); public List<Category> getCategories(){ return categories; } public void setcagegories(List<Category> categories){ this.categories=categories; } }
CategoryVo
package com.huang.model.vo; import com.huang.model.Category; import com.huang.model.Hbook; import java.util.ArrayList; import java.util.List; /** * @auther 宇晨 * @company * @create 2019-10-23-20:04 */ public class CategoryVo extends Category { private List<Hbook> hbooks=new ArrayList<>(); public List<Hbook> getHbooks() { return hbooks; } public void setHbooks(List<Hbook> hbooks) { this.hbooks = hbooks; } }
HbookMapper.xml
通过一个书籍类别id,查询出书籍的信息的同时,还查询出这本书对应的类别
<resultMap id="HbookVoMap" type="com.huang.model.vo.HbookVo"> <result property="bookId" column="book_id"></result> <result property="bookName" column="book_name"></result> <result property="price" column="price"></result> <!--<result property="orderItems"></result>--> <collection property="categories" ofType="com.huang.model.Category"> <result property="categoryId" column="category_id"></result> <result property="categoryName" column="category_name"></result> </collection> </resultMap> <select id="selectByBid" resultMap="HbookVoMap" parameterType="java.lang.Integer" > select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c where b.book_id=bc.bid and bc.cid=c.category_id and b.book_id=#{bid} </select>
HbookMapper.java
package com.huang.mapper; import com.huang.model.Hbook; import com.huang.model.vo.HbookVo; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @Repository public interface HbookMapper { int deleteByPrimaryKey(Integer bookId); int insert(Hbook record); int insertSelective(Hbook record); Hbook selectByPrimaryKey(Integer bookId); int updateByPrimaryKeySelective(Hbook record); int updateByPrimaryKey(Hbook record); HbookVo selectByBid(@Param("bid") Integer bid); }
CategoryMapper.xml
<resultMap id="CategoryVoMap" type="com.huang.model.vo.CategoryVo"> <result property="categoryId" column="category_id"></result> <result property="categoryName" column="category_name"></result> <!--<result property="orderItems"></result>--> <collection property="hbooks" ofType="com.huang.model.Hbook"> <result property="bookId" column="book_id"></result> <result property="bookName" column="book_name"></result> <result property="price" column="price"></result> </collection> </resultMap> <!--通过一个类别id,查询出类别的信息的同时,还查询出这个类别对应的书籍--> <select id="selectByCid" resultMap="CategoryVoMap" parameterType="java.lang.Integer" > select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c where b.book_id=bc.bid and bc.cid=c.category_id and c.category_id=#{cid} </select>
CategoryMapper.java
package com.huang.mapper; import com.huang.model.Category; import com.huang.model.vo.CategoryVo; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @Repository public interface CategoryMapper { int deleteByPrimaryKey(Integer categoryId); int insert(Category record); int insertSelective(Category record); Category selectByPrimaryKey(Integer categoryId); int updateByPrimaryKeySelective(Category record); int updateByPrimaryKey(Category record); CategoryVo selectByCid(@Param("cid") Integer cid); }
ManyToManyService.java
package com.huang.service; import com.huang.model.vo.CategoryVo; import com.huang.model.vo.HbookVo; /** * @auther 宇晨 * @company * @create 2019-10-24-11:08 */ public interface ManyToManyService { HbookVo selectByBid(Integer bid); CategoryVo selectByCid(Integer cid); }
ManyToManyServiceImpl.java
package com.huang.service.impl; import com.huang.mapper.CategoryMapper; import com.huang.mapper.HbookMapper; import com.huang.model.vo.CategoryVo; import com.huang.model.vo.HbookVo; import com.huang.service.ManyToManyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @auther 宇晨 * @company * @create 2019-10-24-11:11 */ @Service public class ManyToManyServiceImpl implements ManyToManyService { @Autowired private HbookMapper hbookMapper; @Autowired private CategoryMapper categoryMapper; @Override public HbookVo selectByBid(Integer bid) { return hbookMapper.selectByBid(bid); } @Override public CategoryVo selectByCid(Integer cid) { return categoryMapper.selectByCid(cid); } }
测试
ManyToManyServiceImplTest.java
package com.huang.service.impl; import com.huang.SpringBaseTest; import com.huang.model.Category; import com.huang.model.Hbook; import com.huang.model.vo.CategoryVo; import com.huang.model.vo.HbookVo; import com.huang.service.ManyToManyService; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; /** * @auther 宇晨 * @company * @create 2019-10-24-11:13 */ public class ManyToManyServiceImplTest extends SpringBaseTest { @Autowired private ManyToManyService manyToManyService; @Test public void selectByBid() { HbookVo hbookVo=manyToManyService.selectByBid(8); System.out.println(hbookVo); for (Category category : hbookVo.getCategories()) { System.out.println(category); } } @Test public void selectByCid() { CategoryVo categoryVo = this.manyToManyService.selectByCid(8); System.out.println(categoryVo); for (Hbook hbook : categoryVo.getHbooks()) { System.out.println(hbook); } } }
测试结果
selectByBid
selectByCid