数据表导入数据库中
t_hibernate_order表
t_hibernate_order_item表
通过mybatis-generator插件生成dao、mapper、model
修改实体类
配置mybatis关联映射
一对多
<resultMap id="OrderVoMap" type="com.caoluo.model.vo.OrderVo" >
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<collection property="orderItems" ofType="com.caoluo.model.OrderItem">
<!-- 一对多的关系 -->
<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<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>
注意事项,使用左外连接而非内连接!!!
多对一
<resultMap id="OrderItemVoMap" type="com.caoluo.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.caoluo.model.Order">
<!-- 多对一的关系 -->
<!-- property: 指的是属性的值, javaType:指的是属性的类型-->
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
</association>
</resultMap>
sql语句
测试代码
多对多
HbookVo
public class HbookVo extends Hbook {
private List<Category> category =new ArrayList<>();
public List<Category> getCategory() {
return category;
}
public void setCategory(List<Category> category) {
this.category = category;
}
}
CategoryVo
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.liuwenwu.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="category" ofType="com.liuwenwu.model.Category">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
</collection>
</resultMap>
<resultMap id="CategoryVoMap" type="com.liuwenwu.model.vo.CategoryVo" >
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
<collection property="hbooks" ofType="com.liuwenwu.model.Hbook">
<result property="bookId" column="book_id"></result>
<result property="bookName" column="book_name"></result>
<result property="price" column="price"></result>
</collection>
</resultMap>
<select id="queryByBid" 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
@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 queryByBid(@Param("bid") Integer bid);
CategoryVo queryByCid(@Param("cid") Integer cid);
}
HbookCategoryService
public interface HbookCategoryService {
HbookVo queryByBid(Integer bid);
CategoryVo queryByCid(Integer cid);
}
HbookCategoryServiceImpl
@Service
public class HbookCategoryServiceImpl implements HbookCategoryService {
@Autowired
private HbookCategoryMapper hbookCategoryMapper;
@Override
public HbookVo queryByBid(Integer bid) {
return hbookCategoryMapper.queryByBid(bid);
}
@Override
public CategoryVo queryByCid(Integer cid) {
return hbookCategoryMapper.queryByCid(cid);
}
}
测试代码:
public class HbookCategoryServiceImplTest extends SpringBaseTest {
@Autowired
private HbookCategoryService hbookCategoryService;
@Test
public void queryByBid() {
HbookVo hbookVo = hbookCategoryService.queryByBid(1);
System.out.println(hbookVo);
for (Category category : hbookVo.getCategory()) {
System.out.println(category);
}
}
@Test
public void queryByCid() {
CategoryVo categoryVo = hbookCategoryService.queryByCid(1);
System.out.println(categoryVo);
for (Hbook hbook : categoryVo.getHbooks()) {
System.out.println(hbook);
}
}
}