目录
一对多关联关系
多对多关联关系
一对多关联关系
首先最经典的案例体现一对多就是一个订单对应多个订单项
所以我们先用逆向生成工具生成订单表和订单项两张表的mapper和model
为了让实体类比较纯粹,所以我们的实体类中只对数据库表的列段进行描述,额外需要查询的列段我们新进一个vo类,把那些元素放进去,比如我们查询订单的同时,想要查看所有订单项,那么这个订单项集合就是一个额外的元素
OrderVo
因为我们没写什么注解,所以可以直接继承,正常开发不建议直接继承实体类,最好把实体类所有的属性在重写一遍
package com.lyl.ssm.model.vo;
import com.lyl.ssm.model.Order;
import com.lyl.ssm.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
/**
* @authorlyl
* @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;
}
}
OrderItemVo
package com.lyl.ssm.model.vo;
import com.lyl.ssm.model.Order;
import com.lyl.ssm.model.OrderItem;
/**
* @authorlyl
* @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;
}
}
然后再Mapper中写一个查询订单的方法
OrderMapper
package com.lyl.ssm.mapper;
import com.lyl.ssm.model.Order;
import com.lyl.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);
}
然后在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.lyl.ssm.mapper.OrderMapper" >
<resultMap id="BaseResultMap" type="com.lyl.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.lyl.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.lyl.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=