课程目标
1、一对多关联关系
2、多对多关联关系
一对多
首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper
实体类
package com.ztb.ssm.model.vo;
import com.ztb.ssm.model.Order;
import com.ztb.ssm.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
/**
* @author Lebron
* @site
* @company
* @create 2019-10-23 上午 11:06
*/
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;
}
}
package com.ztb.ssm.model.vo;
import com.ztb.ssm.model.Order;
import com.ztb.ssm.model.OrderItem;
/**
* @authorLebron
* @site
* @company
* @create 2019-10-23 上午 11:07
*/
public class OrderItemVo extends OrderItem {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
OrderMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ztb.ssm.mapper.OrderMapper" >
<resultMap id="BaseResultMap" type="com.ztb.ssm.model.Order" >
<constructor >
<idArg column="order_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="order_no" jdbcType="VARCHAR" javaType="java.lang.String" />
</constructor>
</resultMap>
<resultMap id="OrderVoMap" type="com.ztb.ssm.model.vo.OrderVo">
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<!--<result property="orderItems"></result>-->
<collection property="orderItems" ofType="com.ztb.ssm.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>
<sql id="Base_Column_List" >
order_id, order_no
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_hibernate_order
where order_id = #{orderId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_hibernate_order
where order_id = #{orderId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.ztb.ssm.model.Order" >
insert into t_hibernate_order (order_id, order_no)
values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.ztb.ssm.model.Order" >
insert into t_hibernate_order
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="orderId != null" >
order_id,
</if>
<if test="orderNo != null" >
order_no,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="orderId != null" >
#{orderId,jdbcType=INTEGER},
</if>
<if test="orderNo != null" >
#{orderNo,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.ztb.ssm.model.Order" >
update t_hibernate_order
<set >
<if test="orderNo != null" >
order_no = #{orderNo,jdbcType=VARCHAR},
</if>
</set>
where order_id = #{orderId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.ztb.ssm.model.Order" >
update t_hibernate_order
set order_no = #{orderNo,jdbcType=VARCHAR}
where order_id = #{orderId,jdbcType=INTEGER}
</update>
<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>
</mapper>
OrderMapper.java
package com.ztb.ssm.mapper;
import com.ztb.ssm.model.Order;
import com.ztb.ssm.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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ztb.ssm.mapper.OrderItemMapper" >
<resultMap id="BaseResultMap" type="com.ztb.ssm.model.OrderItem" >
<constructor >
<idArg column="order_item_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="product_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="quantity" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="oid" jdbcType="INTEGER" javaType="java.lang.Integer" />
</constructor>
</resultMap>
<resultMap id="OrderItemVoMap" type="com.ztb.ssm.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.ztb.ssm.model.Order">
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
</association>
</resultMap>
<sql id="Base_Column_List" >
order_item_id, product_id, quantity, oid
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_hibernate_order_item
where order_item_id = #{orderItemId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_hibernate_order_item
where order_item_id = #{orderItemId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.ztb.ssm.model.OrderItem" >
insert into t_hibernate_order_item (order_item_id, product_id, quantity,
oid)
values (#{orderItemId,jdbcType=INTEGER}, #{productId,jdbcType=INTEGER}, #{quantity,jdbcType=INTEGER},
#{oid,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.ztb.ssm.model.OrderItem" >
insert into t_hibernate_order_item
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="orderItemId != null" >
order_item_id,
</if>
<if test="productId != null" >
product_id,
</if>
<if test="quantity != null" >
quantity,
</if>
<if test="oid != null" >
oid,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="orderItemId != null" >
#{orderItemId,jdbcType=INTEGER},
</if>
<if test="productId != null" >
#{productId,jdbcType=INTEGER},
</if>
<if test="quantity != null" >
#{quantity,jdbcType=INTEGER},
</if>
<if test="oid != null" >
#{oid,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.ztb.ssm.model.OrderItem" >
update t_hibernate_order_item
<set >
<if test="productId != null" >
product_id = #{productId,jdbcType=INTEGER},
</if>
<if test="quantity != null" >
quantity = #{quantity,jdbcType=INTEGER},
</if>
<if test="oid != null" >
oid = #{oid,jdbcType=INTEGER},
</if>
</set>
where order_item_id = #{orderItemId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.ztb.ssm.model.OrderItem" >
update t_hibernate_order_item
set product_id = #{productId,jdbcType=INTEGER},
quantity = #{quantity,jdbcType=INTEGER},
oid = #{oid,jdbcType=INTEGER}
where order_item_id = #{orderItemId,jdbcType=INTEGER}
</update>
<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>
</mapper>
OrderItemMapper.java
package com.ztb.ssm.mapper;
import com.ztb.ssm.model.OrderItem;
import com.ztb.ssm.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.java
package com.ztb.ssm.service;
import com.ztb.ssm.model.vo.OrderItemVo;
import com.ztb.ssm.model.vo.OrderVo;
import java.util.List;
/**
* @author Lebron
* @site
* @company
* @create 2019-10-23 上午 11:17
*/
public interface OneToManyService {
List<OrderItemVo> selectByorderItemId(Integer orderItemId );
List<OrderVo> selectByorderId(Integer orderId);
}
package com.ztb.ssm.service.impl;
import com.ztb.ssm.mapper.OrderItemMapper;
import com.ztb.ssm.mapper.OrderMapper;
import com.ztb.ssm.model.vo.OrderItemVo;
import com.ztb.ssm.model.vo.OrderVo;
import com.ztb.ssm.service.OneToManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @authorLebron
* @site
* @company
* @create 2019-10-23 下午 12:08
*/
@Service
public class OneToManyServiceImpl implements OneToManyService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Override
public List<OrderItemVo> selectByorderItemId(Integer orderItemId) {
return orderItemMapper.selectByorderItemId(orderItemId);
}
@Override
public List<OrderVo> selectByorderId(Integer orderId) {
return orderMapper.selectByorderId(orderId);
}
}
OneToManyServiceImplTest.java
package com.ztb.ssm.service.impl;
import com.ztb.ssm.SpringJunitBaseTest;
import com.ztb.ssm.model.OrderItem;
import com.ztb.ssm.model.vo.OrderItemVo;
import com.ztb.ssm.model.vo.OrderVo;
import com.ztb.ssm.service.OneToManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
* @author Lebron
* @site
* @company
* @create 2019-10-23 下午 12:10
*/
public class OneToManyServiceImplTest extends SpringJunitBaseTest {
@Autowired
private OneToManyService oneToManyService;
@Test
public void selectByorderItemId() {
List<OrderItemVo> orderItemVos = oneToManyService.selectByorderItemId(36);
OrderItemVo orderItemVo = orderItemVos.get(0);
System.out.println(orderItemVo);
System.out.println(orderItemVo.getOrder());
}
@Test
public void selectByorderId() {
List<OrderVo> orderVos = oneToManyService.selectByorderId(8);
OrderVo orderVo = orderVos.get(0);
System.out.println(orderVo);
for (OrderItem orderItem : orderVo.getOrderItems()) {
System.out.println(orderItem);
}
}
}
多对多
首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,这两张表对应的model与mapper
实体类
package com.ztb.ssm.model.vo;
import com.ztb.ssm.model.Hbook;
import com.ztb.ssm.model.Category;
import java.util.ArrayList;
import java.util.List;
/**
* @author Lebron
* @site
* @company Lakers
* @create 2019-10-23 下午 05:57
*/
public class HbookVo extends Hbook {
private List<Category> categories = new ArrayList<>();
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
}
package com.ztb.ssm.model.vo;
import com.ztb.ssm.model.Category;
import com.ztb.ssm.model.Hbook;
import java.util.ArrayList;
import java.util.List;
/**
* @author Lebron
* @site
* @company Lakers
* @create 2019-10-23 下午 06:00
*/
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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ztb.ssm.mapper.HbookCategoryMapper" >
<resultMap id="BaseResultMap" type="com.ztb.ssm.model.HbookCategory" >
<constructor >
<idArg column="bcid" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="cid" jdbcType="INTEGER" javaType="java.lang.Integer" />
</constructor>
</resultMap>
<sql id="Base_Column_List" >
bcid, bid, cid
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_hibernate_book_category
where bcid = #{bcid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_hibernate_book_category
where bcid = #{bcid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.ztb.ssm.model.HbookCategory" >
insert into t_hibernate_book_category (bcid, bid, cid
)
values (#{bcid,jdbcType=INTEGER}, #{bid,jdbcType=INTEGER}, #{cid,jdbcType=INTEGER}
)
</insert>
<insert id="insertSelective" parameterType="com.ztb.ssm.model.HbookCategory" >
insert into t_hibernate_book_category
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="bcid != null" >
bcid,
</if>
<if test="bid != null" >
bid,
</if>
<if test="cid != null" >
cid,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="bcid != null" >
#{bcid,jdbcType=INTEGER},
</if>
<if test="bid != null" >
#{bid,jdbcType=INTEGER},
</if>
<if test="cid != null" >
#{cid,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.ztb.ssm.model.HbookCategory" >
update t_hibernate_book_category
<set >
<if test="bid != null" >
bid = #{bid,jdbcType=INTEGER},
</if>
<if test="cid != null" >
cid = #{cid,jdbcType=INTEGER},
</if>
</set>
where bcid = #{bcid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.ztb.ssm.model.HbookCategory" >
update t_hibernate_book_category
set bid = #{bid,jdbcType=INTEGER},
cid = #{cid,jdbcType=INTEGER}
where bcid = #{bcid,jdbcType=INTEGER}
</update>
</mapper>
HbookCategoryMapper.java
package com.ztb.ssm.mapper;
import com.ztb.ssm.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);
}
ManyToManyService.java
package com.ztb.ssm.service;
import com.ztb.ssm.model.vo.CategoryVo;
import com.ztb.ssm.model.vo.HbookVo;
/**
* @author Lebron
* @site
* @company Lakers
* @create 2019-10-23 下午 07:38
*/
public interface ManyToManyService {
HbookVo selectByBid(Integer bid);
CategoryVo selectByCid(Integer cid);
}
package com.ztb.ssm.service.impl;
import com.ztb.ssm.mapper.CategoryMapper;
import com.ztb.ssm.mapper.HbookMapper;
import com.ztb.ssm.model.vo.CategoryVo;
import com.ztb.ssm.model.vo.HbookVo;
import com.ztb.ssm.service.ManyToManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author Lebron
* @site
* @company Lakers
* @create 2019-10-23 下午 07:39
*/
@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);
}
}
ManyToManyServiceImplTest .java
package com.ztb.ssm.service.impl;
import com.ztb.ssm.SpringJunitBaseTest;
import com.ztb.ssm.model.Category;
import com.ztb.ssm.model.Hbook;
import com.ztb.ssm.model.vo.CategoryVo;
import com.ztb.ssm.model.vo.HbookVo;
import com.ztb.ssm.service.ManyToManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
/**
* @author Lebron
* @site
* @company Lakers
* @create 2019-10-23 下午 07:41
*/
public class ManyToManyServiceImplTest extends SpringJunitBaseTest {
@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);
}
}
}