• mybatis关联关系映射


    目录

    一、一对多关联关系

    1、首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item

    这两张表对应的model与mapper

    2、 编写一对多一方面OrderMapper.xml配置文件

    3、创建vo类(包含关联表的信息)

    4、在biz、impl里都添加方法

    5、创建测试类

    6、测试

     7、编写一对多多方面OrderItemMapper.xml配置文件

    二、多对多关联关系

    1、理论:多对多的核心就是两个一对多

    2、逆向生成代码

     3、创建vo类(包含关联表的信息)

    4、配置公共xml文件

    5、测试类

    6、运行


    一、一对多关联关系

    1、首先先用逆向生成工具生成t_hibernate_ordert_hibernate_order_item

    这两张表对应的model与mapper

    1. <table schema="" tableName="t_hibernate_order" domainObjectName="Order"
    2. enableCountByExample="false" enableDeleteByExample="false"
    3. enableSelectByExample="false" enableUpdateByExample="false">
    4. table>
    5. <table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"
    6. enableCountByExample="false" enableDeleteByExample="false"
    7. enableSelectByExample="false" enableUpdateByExample="false">
    8. table>

     逆向生成

    2、 编写一对多一方面OrderMapper.xml配置文件

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    2. <mapper namespace="com.zking.ssm.mapper.OrderMapper" >
    3. <resultMap id="BaseResultMap" type="com.zking.ssm.model.Order" >
    4. <constructor >
    5. <arg column="order_id" jdbcType="VARCHAR" javaType="java.lang.String" />
    6. <arg column="order_no" jdbcType="VARCHAR" javaType="java.lang.String" />
    7. constructor>
    8. resultMap>
    9. <resultMap id="OrdervVoMap" type="com.zking.ssm.model.vo.OrdervVo" >
    10. <result column="order_id" property="orderId">result>
    11. <result column="order_no" property="orderNo">result>
    12. <collection property="orderItems" ofType="com.zking.ssm.model.OrderItem">
    13. <result column="order_item_id" property="orderItemId">result>
    14. <result column="product_id" property="productId">result>
    15. <result column="quantity" property="quantity">result>
    16. <result column="oid" property="oid">result>
    17. collection>
    18. resultMap>
    19. <select id="queryOrderVoByOrderId" resultMap="OrdervVoMap" parameterType="java.lang.Integer">
    20. select * from t_hibernate_order o,t_hibernate_order_item oi
    21. where o.order_id = oi.oid
    22. and o.order_id = #{orderId}
    23. select>
    24. <insert id="insert" parameterType="com.zking.ssm.model.Order" >
    25. insert into t_hibernate_order (order_id, order_no)
    26. values (#{orderId,jdbcType=VARCHAR}, #{orderNo,jdbcType=VARCHAR})
    27. insert>
    28. <insert id="insertSelective" parameterType="com.zking.ssm.model.Order" >
    29. insert into t_hibernate_order
    30. <trim prefix="(" suffix=")" suffixOverrides="," >
    31. <if test="orderId != null" >
    32. order_id,
    33. if>
    34. <if test="orderNo != null" >
    35. order_no,
    36. if>
    37. trim>
    38. <trim prefix="values (" suffix=")" suffixOverrides="," >
    39. <if test="orderId != null" >
    40. #{orderId,jdbcType=VARCHAR},
    41. if>
    42. <if test="orderNo != null" >
    43. #{orderNo,jdbcType=VARCHAR},
    44. if>
    45. trim>
    46. insert>
    47. mapper>

    3、创建vo类(包含关联表的信息)

    1. package com.zking.ssm.model.vo;
    2. import com.zking.ssm.model.Order;
    3. import com.zking.ssm.model.OrderItem;
    4. import java.util.List;
    5. /**
    6. * @author 白未
    7. * @site 3185579318
    8. * @company xxx公司
    9. * @create  2022-08-16 9:21
    10. *
    11. * vo类不仅仅包含当前表的信息还能包含相关联表的信息
    12. *
    13. * 当前订单会有一个或者多个订单项,多个订单项必须要集合进行接收
    14. */
    15. public class OrdervVo extends Order {
    16. //通过订单号查询本次订单的详细信息及对应的所有订单项信息
    17. private List orderItems;
    18. public List getOrderItems() {
    19. return orderItems;
    20. }
    21. public void setOrderItems(List orderItems) {
    22. this.orderItems = orderItems;
    23. }
    24. }

    给实体类添加注解@Data(相当于添加了set/get方法,同时添加了tostring方法)

    1. package com.zking.ssm.model;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import lombok.NoArgsConstructor;
    5. //t_hibernate_order
    6. //相当于添加了set/get方法,同时添加了tostring方法
    7. //@NoArgsConstructor
    8. //@AllArgsConstructor
    9. @Data
    10. public class Order {
    11. private String orderId;
    12. private String orderNo;
    13. }

    4、在biz、impl里都添加方法

    1. package com.zking.ssm.biz;
    2. import com.zking.ssm.model.Order;
    3. import com.zking.ssm.model.vo.OrdervVo;
    4. import org.apache.ibatis.annotations.Param;
    5. public interface OrderBiz {
    6. int insert(Order record);
    7. int insertSelective(Order record);
    8. OrdervVo queryOrderVoByOrderId(Integer orderId);
    9. }

    在impl里添加@Service注解

    1. package com.zking.ssm.biz.impl;
    2. import com.zking.ssm.biz.OrderBiz;
    3. import com.zking.ssm.mapper.OrderMapper;
    4. import com.zking.ssm.model.Order;
    5. import com.zking.ssm.model.vo.OrdervVo;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. /**
    9. * @author 白未
    10. * @site 3185579318
    11. * @company xxx公司
    12. * @create  2022-08-16 9:55
    13. */
    14. @Service
    15. public class OrderBizImpl implements OrderBiz {
    16. @Autowired
    17. private OrderMapper orderMapper;
    18. @Override
    19. public int insert(Order record) {
    20. return 0;
    21. }
    22. @Override
    23. public int insertSelective(Order record) {
    24. return 0;
    25. }
    26. @Override
    27. public OrdervVo queryOrderVoByOrderId(Integer orderId) {
    28. return orderMapper.queryOrderVoByOrderId(orderId);
    29. }
    30. }

    5、创建测试类

    1. package com.zking.ssm.biz.impl;
    2. import com.zking.ssm.biz.OrderBiz;
    3. import com.zking.ssm.model.vo.OrdervVo;
    4. import org.apache.logging.log4j.core.config.Order;
    5. import org.junit.Test;
    6. import org.junit.runner.RunWith;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.test.context.ContextConfiguration;
    9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    10. import static org.junit.Assert.*;
    11. /**
    12. * @author 白未
    13. * @site 3185579318
    14. * @company xxx公司
    15. * @create  2022-08-16 9:58
    16. */
    17. @RunWith(SpringJUnit4ClassRunner.class)
    18. @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
    19. public class OrderBizImplTest {
    20. @Autowired
    21. private OrderBiz orderBiz;
    22. @Test
    23. public void queryOrderVoByOrderId() {
    24. OrdervVo order = orderBiz.queryOrderVoByOrderId(9);
    25. System.out.println(order);
    26. order.getOrderItems().forEach(System.out::println);
    27. }
    28. }

    6、测试

     7、编写一对多多方面OrderItemMapper.xml配置文件

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    2. <mapper namespace="com.zking.ssm.mapper.OrderItemMapper" >
    3. <resultMap id="BaseResultMap" type="com.zking.ssm.model.OrderItem" >
    4. <constructor >
    5. <arg column="order_item_id" jdbcType="VARCHAR" javaType="java.lang.String" />
    6. <arg column="product_id" jdbcType="VARCHAR" javaType="java.lang.String" />
    7. <arg column="quantity" jdbcType="VARCHAR" javaType="java.lang.String" />
    8. <arg column="oid" jdbcType="VARCHAR" javaType="java.lang.String" />
    9. constructor>
    10. resultMap>
    11. <resultMap id="OrderItemVoMap" type="com.zking.ssm.model.vo.OrderItemVo" >
    12. <result column="order_item_id" property="orderItemId">result>
    13. <result column="product_id" property="productId">result>
    14. <result column="quantity" property="quantity">result>
    15. <result column="oid" property="oid">result>
    16. <association property="order" javaType="com.zking.ssm.model.Order">
    17. <result column="order_id" property="orderId">result>
    18. <result column="order_no" property="orderNo">result>
    19. association>
    20. resultMap>
    21. <select id="queryOrderItemVoByOrderItemId" resultMap="OrderItemVoMap" parameterType="java.lang.Integer">
    22. select * from t_hibernate_order o,t_hibernate_order_item oi
    23. where o.order_id = oi.oid
    24. and oi.order_item_id = #{orderItemId}
    25. select>
    26. <insert id="insert" parameterType="com.zking.ssm.model.OrderItem" >
    27. insert into t_hibernate_order_item (order_item_id, product_id, quantity,
    28. oid)
    29. values (#{orderItemId,jdbcType=VARCHAR}, #{productId,jdbcType=VARCHAR}, #{quantity,jdbcType=VARCHAR},
    30. #{oid,jdbcType=VARCHAR})
    31. insert>
    32. <insert id="insertSelective" parameterType="com.zking.ssm.model.OrderItem" >
    33. insert into t_hibernate_order_item
    34. <trim prefix="(" suffix=")" suffixOverrides="," >
    35. <if test="orderItemId != null" >
    36. order_item_id,
    37. if>
    38. <if test="productId != null" >
    39. product_id,
    40. if>
    41. <if test="quantity != null" >
    42. quantity,
    43. if>
    44. <if test="oid != null" >
    45. oid,
    46. if>
    47. trim>
    48. <trim prefix="values (" suffix=")" suffixOverrides="," >
    49. <if test="orderItemId != null" >
    50. #{orderItemId,jdbcType=VARCHAR},
    51. if>
    52. <if test="productId != null" >
    53. #{productId,jdbcType=VARCHAR},
    54. if>
    55. <if test="quantity != null" >
    56. #{quantity,jdbcType=VARCHAR},
    57. if>
    58. <if test="oid != null" >
    59. #{oid,jdbcType=VARCHAR},
    60. if>
    61. trim>
    62. insert>
    63. mapper>

    以下步骤与一方法相似就不一一展示了

    二、多对多关联关系

    1、理论:多对多的核心就是两个一对多

    2、逆向生成代码

    1. generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    2. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
    3. <generatorConfiguration>
    4. <properties resource="jdbc.properties"/>
    5. <classPathEntry location="E:\\AnZhuangBao\\Maven\\mvn_repository\\mysql\\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar"/>
    6. <context id="infoGuardian">
    7. <commentGenerator>
    8. <property name="suppressAllComments" value="true"/>
    9. <property name="suppressDate" value="true"/>
    10. commentGenerator>
    11. <jdbcConnection driverClass="${jdbc.driver}"
    12. connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>
    13. <javaTypeResolver>
    14. <property name="forceBigDecimals" value="false"/>
    15. javaTypeResolver>
    16. <javaModelGenerator targetPackage="com.leaf.ssm.model"
    17. targetProject="src/main/java">
    18. <property name="enableSubPackages" value="false"/>
    19. <property name="constructorBased" value="true"/>
    20. <property name="trimStrings" value="false"/>
    21. <property name="immutable" value="false"/>
    22. javaModelGenerator>
    23. <sqlMapGenerator targetPackage="com.leaf.ssm.mapper"
    24. targetProject="src/main/java">
    25. <property name="enableSubPackages" value="false"/>
    26. sqlMapGenerator>
    27. <javaClientGenerator targetPackage="com.leaf.ssm.mapper"
    28. targetProject="src/main/java" type="XMLMAPPER">
    29. <property name="enableSubPackages" value="false"/>
    30. javaClientGenerator>
    31. <table schema="" tableName="t_hibernate_book" domainObjectName="Hbook"
    32. enableCountByExample="false" enableDeleteByExample="false"
    33. enableSelectByExample="false" enableUpdateByExample="false">
    34. table>
    35. <table schema="" tableName="t_hibernate_category" domainObjectName="Category"
    36. enableCountByExample="false" enableDeleteByExample="false"
    37. enableSelectByExample="false" enableUpdateByExample="false">
    38. table>
    39. <table schema="" tableName="t_hibernate_book_category" domainObjectName="HbookCategory"
    40. enableCountByExample="false" enableDeleteByExample="false"
    41. enableSelectByExample="false" enableUpdateByExample="false">
    42. table>
    43. context>
    44. generatorConfiguration>

    生成成功

     3、创建vo类(包含关联表的信息)

    1. package com.zking.ssm.model.vo;
    2. import com.zking.ssm.model.Category;
    3. import com.zking.ssm.model.Hbook;
    4. import lombok.Data;
    5. import lombok.ToString;
    6. import java.util.List;
    7. /**
    8. * @author 白未
    9. * @site 3185579318
    10. * @company xx公司
    11. * @create 2022-08-16 11:36
    12. */
    13. @Data
    14. public class CategoryVo extends Category {
    15. private List hbooks;
    16. public List getHbooks() {
    17. return hbooks;
    18. }
    19. public void setHbooks(List hbooks) {
    20. this.hbooks = hbooks;
    21. }
    22. }
    1. package com.zking.ssm.model.vo;
    2. import com.zking.ssm.model.Category;
    3. import com.zking.ssm.model.Hbook;
    4. import lombok.Data;
    5. import java.util.List;
    6. /**
    7. * @author 白未
    8. * @site 3185579318
    9. * @company xx公司
    10. * @create 2022-08-16 11:36
    11. */
    12. @Data
    13. public class HbookVo extends Hbook {
    14. private List categories;
    15. public List getCategories() {
    16. return categories;
    17. }
    18. public void setCategories(List categories) {
    19. this.categories = categories;
    20. }
    21. }

    4、配置公共xml文件

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    2. <mapper namespace="com.zking.ssm.mapper.HbookCategoryMapper" >
    3. <resultMap id="BaseResultMap" type="com.zking.ssm.model.HbookCategory" >
    4. <constructor >
    5. <idArg column="bcid" jdbcType="INTEGER" javaType="java.lang.Integer" />
    6. <arg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
    7. <arg column="cid" jdbcType="INTEGER" javaType="java.lang.Integer" />
    8. constructor>
    9. resultMap>
    10. <sql id="Base_Column_List" >
    11. bcid, bid, cid
    12. sql>
    13. <resultMap id="HbookVoMap" type="com.zking.ssm.model.vo.HbookVo">
    14. <result property="bookId" column="book_id">result>
    15. <result property="bookName" column="book_name">result>
    16. <collection property="categories" ofType="com.zking.ssm.model.Category">
    17. <result property="categoryId" column="category_id">result>
    18. <result property="categoryName" column="category_name">result>
    19. collection>
    20. resultMap>
    21. <resultMap id="CategoryVoMap" type="com.zking.ssm.model.vo.CategoryVo">
    22. <result property="categoryId" column="category_id">result>
    23. <result property="categoryName" column="category_name">result>
    24. <collection property="hbooks" ofType="com.zking.ssm.model.Hbook">
    25. <result property="bookId" column="book_id">result>
    26. <result property="bookName" column="book_name">result>
    27. collection>
    28. resultMap>
    29. <select id="queryByBookId" resultMap="HbookVoMap" parameterType="java.lang.Integer">
    30. select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
    31. where b.book_id = bc.bid and bc.cid = c.category_id and b.book_id = #{bookId}
    32. select>
    33. <select id="queryByCid" resultMap="CategoryVoMap" parameterType="java.lang.Integer">
    34. select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
    35. where b.book_id = bc.bid and bc.cid = c.category_id and c.category_id=#{cid}
    36. select>
    37. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    38. select
    39. <include refid="Base_Column_List" />
    40. from t_hibernate_book_category
    41. where bcid = #{bcid,jdbcType=INTEGER}
    42. select>
    43. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    44. delete from t_hibernate_book_category
    45. where bcid = #{bcid,jdbcType=INTEGER}
    46. delete>
    47. <insert id="insert" parameterType="com.leaf.ssm.model.HbookCategory" >
    48. insert into t_hibernate_book_category (bcid, bid, cid
    49. )
    50. values (#{bcid,jdbcType=INTEGER}, #{bid,jdbcType=INTEGER}, #{cid,jdbcType=INTEGER}
    51. )
    52. insert>
    53. <insert id="insertSelective" parameterType="com.zking.ssm.model.HbookCategory" >
    54. insert into t_hibernate_book_category
    55. <trim prefix="(" suffix=")" suffixOverrides="," >
    56. <if test="bcid != null" >
    57. bcid,
    58. if>
    59. <if test="bid != null" >
    60. bid,
    61. if>
    62. <if test="cid != null" >
    63. cid,
    64. if>
    65. trim>
    66. <trim prefix="values (" suffix=")" suffixOverrides="," >
    67. <if test="bcid != null" >
    68. #{bcid,jdbcType=INTEGER},
    69. if>
    70. <if test="bid != null" >
    71. #{bid,jdbcType=INTEGER},
    72. if>
    73. <if test="cid != null" >
    74. #{cid,jdbcType=INTEGER},
    75. if>
    76. trim>
    77. insert>
    78. <update id="updateByPrimaryKeySelective" parameterType="com.zking.ssm.model.HbookCategory" >
    79. update t_hibernate_book_category
    80. <set >
    81. <if test="bid != null" >
    82. bid = #{bid,jdbcType=INTEGER},
    83. if>
    84. <if test="cid != null" >
    85. cid = #{cid,jdbcType=INTEGER},
    86. if>
    87. set>
    88. where bcid = #{bcid,jdbcType=INTEGER}
    89. update>
    90. <update id="updateByPrimaryKey" parameterType="com.zking.ssm.model.HbookCategory" >
    91. update t_hibernate_book_category
    92. set bid = #{bid,jdbcType=INTEGER},
    93. cid = #{cid,jdbcType=INTEGER}
    94. where bcid = #{bcid,jdbcType=INTEGER}
    95. update>
    96. mapper>

    5、测试类

    1. package com.zking.ssm.biz.impl;
    2. import com.zking.ssm.biz.HbookCategoryBiz;
    3. import com.zking.ssm.model.vo.CategoryVo;
    4. import com.zking.ssm.model.vo.HbookVo;
    5. import org.junit.Test;
    6. import org.junit.runner.RunWith;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.test.context.ContextConfiguration;
    9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    10. import static org.junit.Assert.*;
    11. /**
    12. * @author zking
    13. * @site 3185579318
    14. * @company xx公司
    15. * @create 2022-08-16 11:43
    16. */
    17. @RunWith(SpringJUnit4ClassRunner.class)
    18. @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
    19. public class HbookCategoryBizImplTest {
    20. @Autowired
    21. private HbookCategoryBiz hbookCategoryBiz;
    22. @Test
    23. public void queryByBookId() {
    24. HbookVo hbookVo = hbookCategoryBiz.queryByBookId(8);
    25. System.out.println(hbookVo);
    26. }
    27. @Test
    28. public void queryByCid() {
    29. CategoryVo categoryVo = hbookCategoryBiz.queryByCid(8);
    30. System.out.println(categoryVo);
    31. }
    32. }

    6、运行

     

     

  • 相关阅读:
    Ajax入门及jQuery库对Ajax的封装
    猿创征文|Docker实战:docker 安装mongodb
    下一代大数据分布式存储技术Apache Ozone初步研究
    重生奇迹MU之地下城简介
    音乐转录(AMT)库Omnizart论文笔记及实践
    敲了几万行源码后,我给Mybatis画了张“全地图”
    搭载紫光展锐V510平台 移远通信RG500U-EA 5G模组获全球首个GCF认证
    [附源码]Python计算机毕业设计Django兴达五金日杂批发商店管理系统
    SpringBoot2基础篇(一)—— 入门案例
    Self -Supervised Learning
  • 原文地址:https://blog.csdn.net/yzq102873/article/details/126358614