package com.xfz.ssm.model.vo;
import com.xfz.ssm.model.Order;
import com.xfz.ssm.model.OrderItem;
import java.util.List;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2019-11-21 19:12
*/
public class OrderVo extends Order {
private List orderItems;
public List getOrderItems() {
return orderItems;
}
public void setOrderItems(List orderItems) {
this.orderItems = orderItems;
}
}
OrderItemVo.java
package com.xfz.ssm.model.vo;
import com.xfz.ssm.model.Order;
import com.xfz.ssm.model.OrderItem;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2019-11-21 19:18
*/
public class OrderItemVo extends OrderItem {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
OrderItemMapper.xml
OrderItemMapper.xml
4.2 OrderMapper.java
package com.xfz.ssm.mapper;
import com.xfz.ssm.model.Order;
import com.xfz.ssm.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;
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 queryOrderVoByOrderId(@Param("orderId") Integer orderId);
}
OrderItemMapper.java
package com.xfz.ssm.mapper;
import com.xfz.ssm.model.OrderItem;
import com.xfz.ssm.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
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 queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
}
4.3 写一个接口(OneToManyService)
package com.xfz.ssm.service;
import com.xfz.ssm.model.vo.OrderItemVo;
import com.xfz.ssm.model.vo.OrderVo;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2019-11-21 19:41
*/
public interface OneToManyService {
OrderVo queryOrderVoByOrderId(Integer orderId);
OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId);
}
4.4 实现所写接口(OneToManyServiceImpl )
package com.xfz.ssm.service.impl;
import com.xfz.ssm.mapper.OrderItemMapper;
import com.xfz.ssm.mapper.OrderMapper;
import com.xfz.ssm.model.vo.OrderItemVo;
import com.xfz.ssm.model.vo.OrderVo;
import com.xfz.ssm.service.OneToManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2019-11-21 19:59
*/
@Service
public class OneToManyServiceImpl implements OneToManyService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Override
public OrderVo queryOrderVoByOrderId(Integer orderId) {
return orderMapper.queryOrderVoByOrderId(orderId);
}
@Override
public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
}
}
4.5 编写测试类(OneToManyServiceImplTest )
package com.xfz.ssm.service.impl;
import com.xfz.ssm.SpringBaseTest;
import com.xfz.ssm.model.OrderItem;
import com.xfz.ssm.model.vo.OrderItemVo;
import com.xfz.ssm.model.vo.OrderVo;
import com.xfz.ssm.service.OneToManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2019-11-21 20:05
*/
public class OneToManyServiceImplTest extends SpringBaseTest {
@Autowired
private OneToManyService oneToManyService;
@Test
public void queryOrderVoByOrderId() {
OrderVo orderVo=oneToManyService.queryOrderVoByOrderId(8);
System.out.println(orderVo.getOrderNo());
for (OrderItem orderItem : orderVo.getOrderItems()) {
System.out.println(orderItem);
}
}
@Test
public void queryOrderItemVoByOrderItemId() {
OrderItemVo orderItemVo = oneToManyService.queryOrderItemVoByOrderItemId(36);
System.out.println(orderItemVo);
System.out.println(orderItemVo.getOrder().getOrderNo());
}
}
package com.xfz.ssm.model.vo;
import com.xfz.ssm.model.Category;
import com.xfz.ssm.model.Hbook;
import java.util.List;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2019-11-21 20:18
*/
public class HbookVo extends Hbook {
private List categories;
public List getCategories() {
return categories;
}
public void setCategories(List categories) {
this.categories = categories;
}
}
CategoryVo.java
package com.xfz.ssm.model.vo;
import com.xfz.ssm.model.Category;
import com.xfz.ssm.model.Hbook;
import java.util.List;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2019-11-21 20:26
*/
public class CategoryVo extends Category {
private List hbooks;
public List getHbooks() {
return hbooks;
}
public void setHbooks(List hbooks) {
this.hbooks = hbooks;
}
}
bcid, bid, cid
delete from t_hibernate_book_category
where bcid = #{bcid,jdbcType=INTEGER}
insert into t_hibernate_book_category (bcid, bid, cid
)
values (#{bcid,jdbcType=INTEGER}, #{bid,jdbcType=INTEGER}, #{cid,jdbcType=INTEGER}
)
insert into t_hibernate_book_category
bcid,
bid,
cid,
#{bcid,jdbcType=INTEGER},
#{bid,jdbcType=INTEGER},
#{cid,jdbcType=INTEGER},
update t_hibernate_book_category
bid = #{bid,jdbcType=INTEGER},
cid = #{cid,jdbcType=INTEGER},
where bcid = #{bcid,jdbcType=INTEGER}
update t_hibernate_book_category
set bid = #{bid,jdbcType=INTEGER},
cid = #{cid,jdbcType=INTEGER}
where bcid = #{bcid,jdbcType=INTEGER}
package com.xfz.ssm.mapper;
import com.xfz.ssm.model.HbookCategory;
import com.xfz.ssm.model.vo.CategoryVo;
import com.xfz.ssm.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
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 queryByBookId(@Param("bookId") Integer bookId);
CategoryVo queryByCid(@Param("cid") Integer cid);
}
package com.xfz.ssm.service;
import com.xfz.ssm.model.vo.CategoryVo;
import com.xfz.ssm.model.vo.HbookVo;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2019-11-21 21:00
*/
public interface ManyToManyService {
HbookVo queryByBookId(Integer bookId);
CategoryVo queryByCid(Integer cid);
}
package com.xfz.ssm.service.impl;
import com.xfz.ssm.mapper.HbookCategoryMapper;
import com.xfz.ssm.model.vo.CategoryVo;
import com.xfz.ssm.model.vo.HbookVo;
import com.xfz.ssm.service.ManyToManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2019-11-21 21:03
*/
@Service
public class ManyToManyServiceImpl implements ManyToManyService {
@Autowired
private HbookCategoryMapper hbookCategoryMapper;
@Override
public HbookVo queryByBookId(Integer bookId) {
return hbookCategoryMapper.queryByBookId(bookId);
}
@Override
public CategoryVo queryByCid(Integer cid) {
return hbookCategoryMapper.queryByCid(cid);
}
}
package com.xfz.ssm.service.impl;
import com.xfz.ssm.SpringBaseTest;
import com.xfz.ssm.model.Category;
import com.xfz.ssm.model.Hbook;
import com.xfz.ssm.model.vo.CategoryVo;
import com.xfz.ssm.model.vo.HbookVo;
import com.xfz.ssm.service.ManyToManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
/**
* @author xfz
* @site www.xfz.com
* @company zking
* @create 2019-11-21 21:06
*/
public class ManyToManyServiceImplTest extends SpringBaseTest {
@Autowired
private ManyToManyService manyToManyService;
@Test
public void queryByBookId() {
HbookVo hbookVo = manyToManyService.queryByBookId(8);
System.out.println(hbookVo.getBookName());
for (Category category : hbookVo.getCategories()) {
System.out.println(category.getCategoryName());
}
}
@Test
public void queryByCid() {
CategoryVo categoryVo = manyToManyService.queryByCid(8);
System.out.println(categoryVo.getCategoryName());
for (Hbook hbook : categoryVo.getHbooks()) {
System.out.println(hbook.getBookName());
}
}
}