Mybatis关联关系
一对多
首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper
代码:
orderVo
package com.cjh.model.vo; import com.cjh.model.Order; import com.cjh.model.OrderItem; import java.util.ArrayList; import java.util.List; /** * @author * @site * @company * @create 2019-09-26 11:35 */ 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.cjh.model.vo; import com.cjh.model.Order; import com.cjh.model.OrderItem; /** * @author * @site * @company * @create 2019-09-26 11:37 */ public class OrderItemVo extends OrderItem { private Order order; public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } }
OrderMapper.xml:
<resultMap id="OrderVoMap" type="com.cjh.model.vo.OrderVo" > <result property="orderId" column="order_id"></result> <result property="orderNo" column="order_no"></result> <collection property="orderItems" ofType="com.cjh.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="selectByOid" resultMap="OrderVoMap" parameterType="java.lang.Integer"> select * from t_hibernate_order o,t_hibernate_order_item oi where o.order_id = oi.oid and o.order_id = #{oid} </select>
OrderMapper
package com.cjh.mapper; import com.cjh.model.Order; import com.cjh.model.vo.OrderVo; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @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); OrderVo selectByOid(@Param("oid") Integer oid); }
OrederItemMapper.xml
<resultMap id="OrderItemVoMap" type="com.cjh.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> <association property="order" javaType="com.cjh.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
package com.cjh.mapper; import com.cjh.model.OrderItem; import com.cjh.model.vo.OrderItemVo; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @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); OrderItemVo selectByOrderItemId(@Param("orderItemId") Integer orderItemId); }
One2MAnyService
package com.cjh.service; import com.cjh.model.vo.OrderItemVo; import com.cjh.model.vo.OrderVo; /** * @author * @site * @company * @create 2019-09-26 18:17 */ public interface One2ManyService { OrderVo selectByOid(Integer oid); OrderItemVo selectByOrderItemId(Integer orderItemId); }
One2MAnyServiceImpl:
package com.cjh.service.Impl; import com.cjh.mapper.OrderItemMapper; import com.cjh.mapper.OrderMapper; import com.cjh.model.vo.OrderItemVo; import com.cjh.model.vo.OrderVo; import com.cjh.service.One2ManyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author * @site * @company * @create 2019-09-26 18:19 */ @Service public class One2ManyServiceImpl implements One2ManyService { @Autowired private OrderMapper orderMapper; @Autowired private OrderItemMapper orderItemMapper; @Override public OrderVo selectByOid(Integer oid) { return orderMapper.selectByOid(oid); } @Override public OrderItemVo selectByOrderItemId(Integer orderItemId) { return orderItemMapper.selectByOrderItemId(orderItemId); } }
测试:
package com.cjh.service.Impl; import com.cjh.SpringBaseTest; import com.cjh.model.OrderItem; import com.cjh.model.vo.OrderVo; import com.cjh.service.One2ManyService; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; /** * @author * @site * @company * @create 2019-09-26 18:24 */ public class One2ManyServiceImplTest extends SpringBaseTest { @Autowired private One2ManyService one2ManyService; @Test public void selectByOid() { OrderVo orderVo = one2ManyService.selectByOid(3); System.out.println(orderVo); for (OrderItem orderItem : orderVo.getOrderItems()) { System.out.println(orderItem); } } @Test public void selectByOrderItemId() { } }
多对多
首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,这三张表对应的model与mapper
HbookVo
package com.cjh.model.vo; import com.cjh.model.Category; import com.cjh.model.Hbook; import java.util.ArrayList; import java.util.List; /** * @author * @site * @company * @create 2019-09-26 18:45 */ public class HbookVo extends Hbook { private List categories = new ArrayList<Category>(); public List getCategories() { return categories; } public void setCategories(List categories) { this.categories = categories; } }
CategoryVo
package com.cjh.model.vo; import com.cjh.model.Category; import com.cjh.model.Hbook; import java.util.ArrayList; import java.util.List; /** * @author * @site * @company * @create 2019-09-26 18:49 */ 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; } }
HbookCategoryMapper.xml
<resultMap id="HbookVoMap" type="com.cjh.model.vo.HbookVo" > <result property="bookId" column="book_id"></result> <result property="bookName" column="book_name"></result> <result property="price" column="price"></result> <collection property="categories" ofType="com.cjh.model.Category"> <result property="categoryId" column="catefgory_id"></result> <result property="categoryName" column="catefgory_name"></result> </collection> </resultMap> <resultMap id="CategoryVoMap" type="com.cjh.model.vo.CategoryVo"> <result property="categoryId" column="category_id"></result> <result property="categoryName" column="category_name"></result> <collection property="hbooks" ofType="com.cjh.model.Hbook"> <result property="bookId" column="book_id"></result> <result property="bookName" column="book_name"></result> </collection> </resultMap> <select id="querByBid" 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> <select id="queryByCid" 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>
HbookCategoryMapper
package com.cjh.mapper; import com.cjh.model.HbookCategory; import com.cjh.model.vo.CategoryVo; import com.cjh.model.vo.HbookVo; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @Repository public interface HbookCategoryMapper { int deleteByPrimaryKey(Integer bcid); int insert(HbookCategory record); int insertSelective(HbookCategory record); HbookCategory selectByPrimaryKey(Integer bcid); int updateByPrimaryKeySelective(HbookCategory record); int updateByPrimaryKey(HbookCategory record); HbookVo querByBid(@Param("bid") Integer bid); CategoryVo queryByCid(@Param("cid") Integer cid); }
HbookCategoryService
package com.cjh.service; import com.cjh.model.vo.CategoryVo; import com.cjh.model.vo.HbookVo; /** * @author * @site * @company * @create 2019-09-26 19:19 */ public interface HbookCategoryService { HbookVo querByBid( Integer bid); CategoryVo queryByCid( Integer cid); }
HbookCategoryServiceImpl
package com.cjh.service.Impl; import com.cjh.mapper.HbookCategoryMapper; import com.cjh.model.vo.CategoryVo; import com.cjh.model.vo.HbookVo; import com.cjh.service.HbookCategoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author * @site * @company * @create 2019-09-26 19:20 */ @Service public class HbookCategoryServiceImpl implements HbookCategoryService { @Autowired private HbookCategoryMapper hbookCategoryMapper; @Override public HbookVo querByBid(Integer bid) { return hbookCategoryMapper.querByBid(bid); } @Override public CategoryVo queryByCid(Integer cid) { return hbookCategoryMapper.queryByCid(cid); } }
测试:
package com.cjh.service.Impl; import com.cjh.SpringBaseTest; import com.cjh.model.Hbook; import com.cjh.model.vo.CategoryVo; import com.cjh.model.vo.HbookVo; import com.cjh.service.HbookCategoryService; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; /** * @author * @site * @company * @create 2019-09-26 19:24 */ public class HbookCategoryServiceImplTest extends SpringBaseTest { @Autowired private HbookCategoryService hbookCategoryService; @Test public void querByBid() { HbookVo hbookVo = hbookCategoryService.querByBid(3); System.out.println(hbookVo); for (Object category : hbookVo.getCategories()) { System.out.println(category); } } @Test public void queryByCid() { CategoryVo categoryVo = hbookCategoryService.queryByCid(3); System.out.println(categoryVo); for (Hbook hbook : categoryVo.getHbooks()) { System.out.println(hbook); } } }
QueryByBid:
QueryByCid: