一对多
首先先用逆向生成工具生成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