1、一对多关联关系
2、多对多关联关系
首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
t_hibernate_book、t_hibernate_book_category、t_hibernate_category
这五张表对应的model与mapper
配置mybatis-generator插件生成文件位置
修改generatorConfig.xml配置文件的生成目录(mapper和model)及对应生成关系
创建实体类:
package com.lingerqi.model.vo; import com.lingerqi.model.Horder; import com.lingerqi.model.Horderitem; import java.util.ArrayList; import java.util.List; /** * @author xyls * @create 2019-10-23 10:43 */ public class OrderVo extends Horder { private List<Horderitem> horderitems = new ArrayList<>(); public List<Horderitem> getHorderitems() { return horderitems; } public void setHorderitems(List<Horderitem> horderitems) { this.horderitems = horderitems; } @Override public String toString() { return "OrderVo{" + "horderitems=" + horderitems + '}'; } }
package com.lingerqi.model.vo; import com.lingerqi.model.Horder; import com.lingerqi.model.Horderitem; /** * @author xyls * @create 2019-10-23 10:46 */ public class HorderitemVo extends Horderitem { private Horder horder; public Horder getHorder() { return horder; } public void setHorder(Horder horder) { this.horder = horder; } @Override public String toString() { return "HorderitemVo{" + "horder=" + horder + '}'; } }
HorderMapper.xml:
<resultMap id="OrderVoMap" type="com.lingerqi.model.vo.OrderVo" > <result property="orderId" column="order_id"></result> <result property="orderNo" column="order_no"></result> <collection property="orderItems" ofType="com.lingerqi.model.Horderitem"> <result property="orderItemId" column="order_item_id"></result> <result property="oid" column="oid"></result> <result property="productId" column="product_id"></result> <result property="quantity" column="quantity"></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.ordre_id=oi.oid and oi.order_id=#{orderId} </select>
@Repository public interface HorderMapper { List<OrderVo> selectByOrderId(@Param("orderId") Integer orderId); }
HorderitemMapper.xml
<resultMap id="OrderItemVoMap" type="com.lingerqi.model.vo.HorderitemVo"> <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.lingerqi.model.Horder"> <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.ordre_id=oi.oid and oi.order_item_id =#{orderItemId} </select>
@Repository public interface HorderitemMapper { List<HorderitemVo> selectByOrderItemId(@Param("orderItemId") Integer orderItemId); }
package com.lingerqi.Service; import com.lingerqi.model.vo.HorderitemVo; import com.lingerqi.model.vo.OrderVo; import java.util.List; /** * @auther xyls * @create 2019-10-23-18:19 */ public interface OneToManyService { List<OrderVo> selectByOrderId(Integer orderId); List<HorderitemVo> selectByOrderItemId(Integer orderItemId); }
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()); } }
package com.lingerqi.mapper; import com.lingerqi.model.HbookCategory; 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); }
package com.lingerqi.service.impl;
import com.lingerqi.service.ManyToManyService;
import com.lingerqi.mapper.CategoryMapper;
import com.lingerqi.mapper.HbookMapper;
import com.lingerqi.model.vo.CategoryVo;
import com.lingerqi.model.vo.HbookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @auther xyls
* @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);
}
}
package com.lingerqi.service.impl; import com.lingerqi.SpringBaseTest; import com.lingerqi.model.Category; import com.lingerqi.model.Hbook; import com.lingerqi.model.vo.CategoryVo; import com.lingerqi.model.vo.HbookVo; import com.lingerqi.service.ManyToManyService; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; /** * @auther xyls * @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); } } }