MyBatis关联关系
Posted 小蜜疯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis关联关系相关的知识,希望对你有一定的参考价值。
一、一对多关联关系
1.通过generatorConfig.xml中首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper
<table schema="" tableName="t_hibernate_Book" domainObjectName="HBook" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> </table> <table schema="" tableName="t_hibernate_order" domainObjectName="Order" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> </table> <table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> </table> <table schema="" tableName="t_hibernate_category" domainObjectName="Category" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> </table> <table schema="" tableName="t_hibernate_book_category" domainObjectName="BookCategory" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> </table>
Order.java(订单实体)
package com.hmc.ssm.model;
public class Order {
private Integer orderId;
private String orderNo;
public Order(Integer orderId, String orderNo) {
this.orderId = orderId;
this.orderNo = orderNo;
}
public Order() {
super();
}
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
@Override
public String toString() {
return "Order{" +
"orderId=" + orderId +
", orderNo=\'" + orderNo + \'\\\'\' +
\'}\';
}
}
OrderMapper
@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.ningjie.ssm.mapper.OrderMapper" > <resultMap id="BaseResultMap" type="com.hmc.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.hmc.ssm.model.vo.OrderVo"> <result property="orderId" column="order_id"></result> <result property="orderNo" column="order_no"></result> <collection property="orderItems" ofType="com.hmc.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.hmc.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.hmc.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.hmc.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.hmc.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>
用下面的OrderVo类继承订单
用一个list集合来放置多方(
OrderItem
)
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; } }
OrderItem
public class OrderItem {
private Integer orderItemId;
private Integer productId;
private Integer quantity;
private Integer oid;
public OrderItem(Integer orderItemId, Integer productId, Integer quantity, Integer oid) {
this.orderItemId = orderItemId;
this.productId = productId;
this.quantity = quantity;
this.oid = oid;
}
public OrderItem() {
super();
}
public Integer getOrderItemId() {
return orderItemId;
}
public void setOrderItemId(Integer orderItemId) {
this.orderItemId = orderItemId;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public Integer getOid() {
return oid;
}
public void setOid(Integer oid) {
this.oid = oid;
}
@Override
public String toString() {
return "OrderItem{" +
"orderItemId=" + orderItemId +
", productId=" + productId +
", quantity=" + quantity +
", oid=" + oid +
\'}\';
}
}
OrderItemVo.java
public class OrderItemVo extends OrderItem {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
OrderItemMapper
@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); }
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.hmc.ssm.mapper.OrderItemMapper" > <resultMap id="BaseResultMap" type="com.hmc.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.hmc.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.hmc.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.hmc.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.hmc.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.hmc.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.hmc.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>
OneToManyService
public interface OneToManyService { List<OrderItemVo> selectByOrderItemId(Integer orderItemId); List<OrderVo> selectByOrderId(Integer orderId); }
OneToManyServiceImpl
@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
public class OneToManyServiceImplTest extends SpringBaseTest { @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
Hbook
public class HBook {
private Integer bookId;
private String bookName;
private Float price;
public HBook(Integer bookId, String bookName, Float price) {
this.bookId = bookId;
this.bookName = bookName;
this.price = price;
}
public HBook() {
super();
}
public Integer getBookId() {
return bookId;
}
public void setBookId(Integer bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
@Override
public String toString() {
return "HBook{" +
"bookId=" + bookId +
", bookName=\'" + bookName + \'\\\'\' +
", price=" + price +
\'}\';
}
}
HbookVo
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; } }
HbookMapper
@Repository
public interface HBookMapper {
int deleteByPrimaryKey(Integer bookId);
int insert(HBook record);
int insertSelective(HBook record);
HBook selectByPrimaryKey(Integer bookId);
int updateByPrimaryKeySelective(HBook record);
int updateByPrimaryKey(HBook record);
HbookVo selectByBid(@Param("bid") Integer bid);
}
HbookMapper.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.ningjie.ssm.mapper.HBookMapper" > <resultMap id="BaseResultMap" type="com.hmc.ssm.model.HBook" > <constructor > <idArg column="book_id" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="book_name" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="price" jdbcType="REAL" javaType="java.lang.Float" /> </constructor> </resultMap> <resultMap id="HbookVoMap" type="com.hmc.ssm.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="categories" ofType="com.hmc.ssm.model.Category"> <result property="categoryId" column="category_id"></result> <result property="categoryName" column="category_Name"></result> </collection> </resultMap> <sql id="Base_Column_List" > book_id, book_name, price </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from t_hibernate_book where book_id = #{bookId,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from t_hibernate_book where book_id = #{bookId,jdbcType=INTEGER} SpringBoot与Mybatis整合(包含generate自动生成代码工具,数据库表一对一,一对多,关联关系中间表的查询)