mybatis关联关系
一对多关联关系
一对多最简单的理解就是一个订单对应多个订单项
先用逆向生成工具生成订单表和订单项两张表的mapper和model
为了让实体类比较纯粹,所以实体类中只对数据库表的列段进行描述,额外需要查询的列段我们新建一个vo类,把其他元素放进去,比如查询订单的同时,想要查看所有订单项,那么这个订单项集合就是一个额外的元素
OrderVo
因为没写什么注解,所以可以直接继承,正常开发不建议直接继承实体类,最好把实体类所有的属性在重写一遍
package com.li.model.vo;
import com.li.model.Order;
import com.li.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
/**
* @author dragon
* @site
* @company
* @create 2019-09-26 12:02
*
*/
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.li.model.vo;
import com.li.model.Order;
import com.li.model.OrderItem;
/**
* @author dragon
* @site
* @company
* @create 2019-09-26 18:26
*/
public class OrderItemVo extends OrderItem {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
然后再Mapper中写一个查询订单的方法
OrderMapper
package com.li.mapper;
import com.li.model.Order;
import com.li.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@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);
OrderVo selectByOid(@Param("oid") Integer oid);
}
然后在OrderMapper.xml中配置好一对多关联关系的映射和刚写的方法的映射
<resultMap id="OrderVoMap" type="com.li.model.vo.OrderVo" >
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<collection property="orderItems" ofType="com.li.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>
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.li.mapper.OrderMapper" >
<resultMap id="BaseResultMap" type="com.li.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.li.model.vo.OrderVo" >
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<collection property="orderItems" ofType="com.li.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>
<select id="selectByOid" resultMap="OrderVoMap" parameterType="java.lang.Integer">
select * from t_hibernate_order o,t_hibernate_order_item oi
where o.order_id = oi.oid
and o.order_id = #{oid}
</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.li.model.Order" >
insert into t_hibernate_order (order_id, order_no)
values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.li.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.li.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.li.model.Order" >
update t_hibernate_order
set order_no = #{orderNo,jdbcType=VARCHAR}
where order_id = #{orderId,jdbcType=INTEGER}
</update>
</mapper>
然后再Mapper中写一个查询订单的方法
OrderItemMapper
package com.li.mapper;
import com.li.model.OrderItem;
import com.li.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@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);
OrderItemVo selectByOrderItemId(@Param("orderItemId") Integer orderItemId);
}
在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.li.mapper.OrderItemMapper" >
<resultMap id="BaseResultMap" type="com.li.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.li.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.li.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>
<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>
<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.li.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.li.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.li.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.li.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>
</mapper>
写好service就可以测试了
OneToManyService
package com.li.service;
import com.li.model.vo.OrderItemVo;
import com.li.model.vo.OrderVo;
/**
* @author dragon
* @site
* @company
* @create 2019-09-26 19:00
*/
public interface One2ManyService {
OrderVo selectByOid(Integer oid);
OrderItemVo selectByOrderItemId(Integer orderItemId);
}
One2ManyServiceImpl
package com.li.service.impl;
import com.li.mapper.OrderItemMapper;
import com.li.mapper.OrderMapper;
import com.li.model.vo.OrderItemVo;
import com.li.model.vo.OrderVo;
import com.li.service.One2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author dragon
* @site
* @company
* @create 2019-09-26 19:02
*/
@Service
public class One2ManyServiceImpl implements One2ManyService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Override
public OrderVo selectByOid(Integer oid) {
return orderMapper.selectByOid(oid);
}
@Override
public OrderItemVo selectByOrderItemId(Integer orderItemId) {
return orderItemMapper.selectByOrderItemId(orderItemId);
}
}
测试One2ManyServiceImplTest
package com.li.service.impl;
import com.li.SpringBaseTest;
import com.li.model.OrderItem;
import com.li.model.vo.OrderItemVo;
import com.li.model.vo.OrderVo;
import com.li.service.One2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
/**
* @author dragon
* @site
* @company
* @create 2019-09-26 19:06
*/
public class One2ManyServiceImplTest extends SpringBaseTest {
@Autowired
private One2ManyService one2ManyService;
@Test
public void selectByOid() {
OrderVo orderVo = one2ManyService.selectByOid(1);
System.out.println(orderVo);
for (OrderItem orderItem : orderVo.getOrderItems()) {
System.out.println(orderItem);
}
}
@Test
public void selectByOrderItemId() {
OrderItemVo orderItemVo = one2ManyService.selectByOrderItemId(2);
System.out.println(orderItemVo);
System.out.println(orderItemVo.getOrder());
}
}
多对多关联关系
多对多的配置和一对多是差不多的,多对多:多个一对多
这里是以一个书籍类别对应多本书,而一本书又对应多个书籍类别,通过一个中间表来实现多对多
一般开发中间表是很少改动的,所以最好把自己写的多表查的其他sql语句放进中间表中的mapper中,因为万一改动了数据库那么实体类又要重新逆向生成,那么你以前写的多表Sql就会消失,
涉及表:t_hibernate_book、t_hibernate_book_category、t_hibernate_category
一样先通过逆向工程生成对应的mapper
然后写几个vo类
HbookVo
package com.li.model.vo;
import com.li.model.Category;
import com.li.model.Hbook;
import java.util.ArrayList;
import java.util.List;
/**
* @author dragon
* @site
* @company
* @create 2019-09-26 19:32
*/
public class HbookVo extends Hbook {
private List categorys = new ArrayList<Category>();
public List getCategorys() {
return categorys;
}
public void setCategorys(List categorys) {
this.categorys = categorys;
}
}
CategoryVo
package com.li.model.vo;
import com.li.model.Category;
import com.li.model.Hbook;
import java.util.ArrayList;
import java.util.List;
/**
* @author dragon
* @site
* @company
* @create 2019-09-26 19:52
*/
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.li.mapper.HbookCategoryMapper" >
<resultMap id="BaseResultMap" type="com.li.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>
<!-- 描述一对多 -->
<resultMap id="HbookVoMap" type="com.li.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="categorys" ofType="com.li.model.Category">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
</collection>
</resultMap>
<!-- 描述一对多 -->
<resultMap id="CategoryVoMap" type="com.li.model.vo.CategoryVo" >
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
<collection property="hbooks" ofType="com.li.model.Hbook">
<result property="bookId" column="book_id"></result>
<result property="bookName" column="book_name"></result>
<result property="price" column="price"></result>
</collection>
</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>
<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>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_hibernate_book_category
where bcid = #{bcid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.li.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.li.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.li.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.li.model.HbookCategory" >
update t_hibernate_book_category
set bid = #{bid,jdbcType=INTEGER},
cid = #{cid,jdbcType=INTEGER}
where bcid = #{bcid,jdbcType=INTEGER}
</update>
</mapper>
HbookCategoryService
package com.li.service;
import com.li.model.vo.CategoryVo;
import com.li.model.vo.HbookVo;
/**
* @author dragon
* @site
* @company
* @create 2019-09-26 20:08
*/
public interface HbookCategoryService {
HbookVo queryByBid(Integer bid);
CategoryVo queryByCid(Integer cid);
}
HbookCategoryServiceImpl
package com.li.service.impl;
import com.li.mapper.HbookCategoryMapper;
import com.li.model.vo.CategoryVo;
import com.li.model.vo.HbookVo;
import com.li.service.HbookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author dragon
* @site
* @company
* @create 2019-09-26 20:09
*/
@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);
}
}
测试代码HbookCategoryServiceImplTest
package com.li.service.impl;
import com.li.SpringBaseTest;
import com.li.model.vo.HbookVo;
import com.li.service.HbookCategoryService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author dragon
* @site
* @company
* @create 2019-09-26 20:13
*/
public class HbookCategoryServiceImplTest extends SpringBaseTest {
@Autowired
private HbookCategoryService hbookCategoryService;
@Test
public void queryByBid() {
HbookVo hbookVo = hbookCategoryService.queryByBid(2);
System.out.println(hbookVo);
for (Object category : hbookVo.getCategorys()) {
System.out.println(category);
}
}
@Test
public void queryByCid() {
CategoryVo categoryVo = hbookCategoryService.queryByCid(2);
System.out.println(categoryVo);
for (Object category : categoryVo.getHbooks()) {
System.out.println(category);
}
}
}