mybatis一对多,多对多关联关系

it2025-09-11  4

mybatis一对多,多对多关联关系

hibernate与mybatis的区别一对多多对多

hibernate与mybatis的区别

1、 hibernate是全自动,而mybatis是半自动。

2、hibernate完全可以通过对象关系模型实现对数据库的操作,拥有完整的JavaBean对象与数据库的映射结构来自动生成sql。而mybatis仅有基本的字段映射,对象数据以及对象实际关系仍然需要通过手写sql来实现和管理。

我个人还是喜欢用mybatis,因为mybatis可以自己控制sql代码,而hibernate的sql是自动生成的,如果业务逻辑比较复杂容易把人绕晕

一对多:一个客户对应多个订单

多对一:多个订单对应一个客户(一个订单对应一个客户)

一对多

首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item 这两张表对应的model与mapper generatorConfig.xml 添加需要生成的表 OrderVo

package com.liuchunming.model.vo; import com.liuchunming.model.Order; import com.liuchunming.model.OrderItem; import java.util.ArrayList; import java.util.List; /** * @author liuchunming * @site www.liuchunming.com * @company xxx公司 * @create 2020-10-21 17:01 */ public class OrderVo extends Order { public List<OrderItem> orderItems =new ArrayList<>(); public List<OrderItem> getOrderItems() { return orderItems; } public void setOrderItems(List<OrderItem> orderItems) { this.orderItems = orderItems; } }

OrderItemVo

package com.liuchunming.model.vo; import com.liuchunming.model.Order; import com.liuchunming.model.OrderItem; /** * @author liuchunming * @site www.liuchunming.com * @company xxx公司 * @create 2020-10-21 17:03 */ public class OrderItemVo extends OrderItem { private Order order; public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } }

OrderMapper.java 新添加查询方法 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.liuchunming.mapper.OrderMapper" > <resultMap id="BaseResultMap" type="com.liuchunming.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.liuchunming.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.liuchunming.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.liuchunming.model.Order" > insert into t_hibernate_order (order_id, order_no) values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="com.liuchunming.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.liuchunming.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.liuchunming.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>

OrderItemMapper.java 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.liuchunming.mapper.OrderItemMapper" > <resultMap id="BaseResultMap" type="com.liuchunming.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.liuchunming.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> <!--<result property="orderItems"></result>--> <association property="order" javaType="com.liuchunming.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.liuchunming.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.liuchunming.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.liuchunming.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.liuchunming.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

package com.liuchunming.service; import com.liuchunming.model.vo.OrderItemVo; import com.liuchunming.model.vo.OrderVo; import java.util.List; /** * @author liuchunming * @site www.liuchunming.com * @company xxx公司 * @create 2020-10-21 21:42 */ public interface OneToManyService { List<OrderItemVo> selectByOrderItemId( Integer orderItemId); List<OrderVo> selectByOrderId(Integer orderId ); }

OneToManyServiceImpl

package com.liuchunming.service.impl; import com.liuchunming.mapper.OrderItemMapper; import com.liuchunming.mapper.OrderMapper; import com.liuchunming.model.vo.OrderItemVo; import com.liuchunming.model.vo.OrderVo; import com.liuchunming.service.OneToManyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author liuchunming * @site www.liuchunming.com * @company xxx公司 * @create 2020-10-21 22:04 */ @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

package com.liuchunming.service.impl; import com.liuchunming.SpringBaseTest; import com.liuchunming.model.OrderItem; import com.liuchunming.model.vo.OrderItemVo; import com.liuchunming.model.vo.OrderVo; import com.liuchunming.service.OneToManyService; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import java.util.List; import static org.junit.Assert.*; /** * @author liuchunming * @site www.liuchunming.com * @company xxx公司 * @create 2020-10-21 22:07 */ 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 HbookVo

package com.liuchunming.model.vo; import com.liuchunming.model.Category; import com.liuchunming.model.Hbook; import java.util.ArrayList; import java.util.List; /** * @author liuchunming * @site www.liuchunming.com * @company xxx公司 * @create 2020-10-21 22:21 */ 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; } }

CategoryVo

package com.liuchunming.model.vo; import com.liuchunming.model.Category; import com.liuchunming.model.Hbook; import java.util.ArrayList; import java.util.List; /** * @author liuchunming * @site www.liuchunming.com * @company xxx公司 * @create 2020-10-21 22:23 */ 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; } }

HbookMapper 添加方法 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.liuchunming.mapper.HbookMapper" > <resultMap id="BaseResultMap" type="com.liuchunming.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.liuchunming.model.vo.HbookVo"> <result property="bookId" column="book_id"></result> <result property="bookName" column="book_name"></result> <result property="price" column="price"></result> <!--<result property="orderItems"></result>--> <collection property="categories" ofType="com.liuchunming.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} </delete> <insert id="insert" parameterType="com.liuchunming.model.Hbook" > insert into t_hibernate_book (book_id, book_name, price ) values (#{bookId,jdbcType=INTEGER}, #{bookName,jdbcType=VARCHAR}, #{price,jdbcType=REAL} ) </insert> <insert id="insertSelective" parameterType="com.liuchunming.model.Hbook" > insert into t_hibernate_book <trim prefix="(" suffix=")" suffixOverrides="," > <if test="bookId != null" > book_id, </if> <if test="bookName != null" > book_name, </if> <if test="price != null" > price, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="bookId != null" > #{bookId,jdbcType=INTEGER}, </if> <if test="bookName != null" > #{bookName,jdbcType=VARCHAR}, </if> <if test="price != null" > #{price,jdbcType=REAL}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.liuchunming.model.Hbook" > update t_hibernate_book <set > <if test="bookName != null" > book_name = #{bookName,jdbcType=VARCHAR}, </if> <if test="price != null" > price = #{price,jdbcType=REAL}, </if> </set> where book_id = #{bookId,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.liuchunming.model.Hbook" > update t_hibernate_book set book_name = #{bookName,jdbcType=VARCHAR}, price = #{price,jdbcType=REAL} where book_id = #{bookId,jdbcType=INTEGER} </update> <select id="selectByBid" 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> </mapper>

CategoryMapper CategoryMapper.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.liuchunming.mapper.CategoryMapper" > <resultMap id="BaseResultMap" type="com.liuchunming.model.Category" > <constructor > <idArg column="category_id" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="category_name" jdbcType="VARCHAR" javaType="java.lang.String" /> </constructor> </resultMap> <resultMap id="CategoryVoMap" type="com.liuchunming.model.vo.CategoryVo"> <result property="categoryId" column="category_id"></result> <result property="categoryName" column="category_name"></result> <!--<result property="orderItems"></result>--> <collection property="hbooks" ofType="com.liuchunming.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" > category_id, category_name </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from t_hibernate_category where category_id = #{categoryId,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from t_hibernate_category where category_id = #{categoryId,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.liuchunming.model.Category" > insert into t_hibernate_category (category_id, category_name) values (#{categoryId,jdbcType=INTEGER}, #{categoryName,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="com.liuchunming.model.Category" > insert into t_hibernate_category <trim prefix="(" suffix=")" suffixOverrides="," > <if test="categoryId != null" > category_id, </if> <if test="categoryName != null" > category_name, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="categoryId != null" > #{categoryId,jdbcType=INTEGER}, </if> <if test="categoryName != null" > #{categoryName,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.liuchunming.model.Category" > update t_hibernate_category <set > <if test="categoryName != null" > category_name = #{categoryName,jdbcType=VARCHAR}, </if> </set> where category_id = #{categoryId,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.liuchunming.model.Category" > update t_hibernate_category set category_name = #{categoryName,jdbcType=VARCHAR} where category_id = #{categoryId,jdbcType=INTEGER} </update> <select id="selectBycid" 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> </mapper>

ManyToManyService

package com.liuchunming.service; import com.liuchunming.model.vo.CategoryVo; import com.liuchunming.model.vo.HbookVo; /** * @author liuchunming * @site www.liuchunming.com * @company xxx公司 * @create 2020-10-21 22:49 */ public interface ManyToManyService { CategoryVo selectBycid(Integer cid); HbookVo selectByBid( Integer bid); }

ManyToManyServiceImpl

package com.liuchunming.service.impl; import com.liuchunming.mapper.CategoryMapper; import com.liuchunming.mapper.HbookMapper; import com.liuchunming.model.vo.CategoryVo; import com.liuchunming.model.vo.HbookVo; import com.liuchunming.service.ManyToManyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author liuchunming * @site www.liuchunming.com * @company xxx公司 * @create 2020-10-21 22:50 */ @Service public class ManyToManyServiceImpl implements ManyToManyService { @Autowired private HbookMapper hbookMapper; @Autowired private CategoryMapper categoryMapper; @Override public CategoryVo selectBycid(Integer cid) { return categoryMapper.selectBycid(cid); } @Override public HbookVo selectByBid(Integer bid) { return hbookMapper.selectByBid(bid); } }

ManyToManyServiceImplTest

package com.liuchunming.service.impl; import com.liuchunming.SpringBaseTest; import com.liuchunming.model.Category; import com.liuchunming.model.Hbook; import com.liuchunming.model.vo.CategoryVo; import com.liuchunming.model.vo.HbookVo; import com.liuchunming.service.ManyToManyService; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; /** * @author liuchunming * @site www.liuchunming.com * @company xxx公司 * @create 2020-10-21 22:53 */ public class ManyToManyServiceImplTest extends SpringBaseTest { @Autowired private ManyToManyService manyToManyService; @Test public void selectBycid() { CategoryVo categoryVo = this.manyToManyService.selectBycid(8); System.out.println(categoryVo); for (Hbook hbook : categoryVo.getHbooks()) { System.out.println(hbook); } } @Test public void selectByBid() { HbookVo hbookVo = manyToManyService.selectByBid(8); System.out.println(hbookVo); for (Category category : hbookVo.getCategories()) { System.out.println(category); } } }

测试

最新回复(0)