• mybatis(关联关系映射)


    目录

    一、一对多关联关系

     二、多对多关联关系


    一、一对多关联关系

     什么叫一对多?

    比如:一个订单对应多个订单项

    1、先用逆向生成工具生成t_hibernate_order(订单)t_hibernate_order_item(订单项)

    generatorConfig.xml 

    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:\\maven\\maven_ localRepository\\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.cxy.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.cxy.mapper"
    24. targetProject="src/main/java">
    25. <property name="enableSubPackages" value="false"/>
    26. sqlMapGenerator>
    27. <javaClientGenerator targetPackage="com.cxy.mapper"
    28. targetProject="src/main/java" type="XMLMAPPER">
    29. <property name="enableSubPackages" value="false"/>
    30. javaClientGenerator>
    31. <table schema="" tableName="t_mvc_book" domainObjectName="Book"
    32. enableCountByExample="false" enableDeleteByExample="false"
    33. enableSelectByExample="false" enableUpdateByExample="false">
    34. table>
    35. <table schema="" tableName="t_hibernate_order" domainObjectName="Order"
    36. enableCountByExample="false" enableDeleteByExample="false"
    37. enableSelectByExample="false" enableUpdateByExample="false">
    38. table>
    39. <table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"
    40. enableCountByExample="false" enableDeleteByExample="false"
    41. enableSelectByExample="false" enableUpdateByExample="false">
    42. table>
    43. context>
    44. generatorConfiguration>

    OrderMapper.xml 

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    2. <mapper namespace="com.cxy.mapper.OrderMapper" >
    3. <resultMap id="BaseResultMap" type="com.cxy.model.Order" >
    4. <constructor >
    5. <idArg column="order_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
    6. <arg column="order_no" jdbcType="VARCHAR" javaType="java.lang.String" />
    7. constructor>
    8. resultMap>
    9. <resultMap id="OrderVoMap" type="com.cxy.model.vo.OrderVo" >
    10. <result property="orderId" column="order_id">result>
    11. <result property="orderNo" column="order_no">result>
    12. <collection property="orderItems" ofType="com.cxy.model.OrderItem">
    13. <result property="orderItemId" column="order_item_id">result>
    14. <result property="oid" column="oid">result>
    15. <result property="productId" column="product_id">result>
    16. <result property="quantity" column="quantity">result>
    17. collection>
    18. resultMap>
    19. <sql id="Base_Column_List" >
    20. order_id, order_no
    21. sql>
    22. <select id="queryOrderVoByOrderId" resultMap="OrderVoMap" parameterType="java.lang.Integer">
    23. select * from t_hibernate_order o,t_hibernate_order_item oi
    24. where o.order_id = oi.oid
    25. and o.order_id = #{orderId}
    26. select>
    27. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    28. select
    29. <include refid="Base_Column_List" />
    30. from t_hibernate_order
    31. where order_id = #{orderId,jdbcType=INTEGER}
    32. select>
    33. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    34. delete from t_hibernate_order
    35. where order_id = #{orderId,jdbcType=INTEGER}
    36. delete>
    37. <insert id="insert" parameterType="com.cxy.model.Order" >
    38. insert into t_hibernate_order (order_id, order_no)
    39. values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
    40. insert>
    41. <insert id="insertSelective" parameterType="com.cxy.model.Order" >
    42. insert into t_hibernate_order
    43. <trim prefix="(" suffix=")" suffixOverrides="," >
    44. <if test="orderId != null" >
    45. order_id,
    46. if>
    47. <if test="orderNo != null" >
    48. order_no,
    49. if>
    50. trim>
    51. <trim prefix="values (" suffix=")" suffixOverrides="," >
    52. <if test="orderId != null" >
    53. #{orderId,jdbcType=INTEGER},
    54. if>
    55. <if test="orderNo != null" >
    56. #{orderNo,jdbcType=VARCHAR},
    57. if>
    58. trim>
    59. insert>
    60. <update id="updateByPrimaryKeySelective" parameterType="com.cxy.model.Order" >
    61. update t_hibernate_order
    62. <set >
    63. <if test="orderNo != null" >
    64. order_no = #{orderNo,jdbcType=VARCHAR},
    65. if>
    66. set>
    67. where order_id = #{orderId,jdbcType=INTEGER}
    68. update>
    69. <update id="updateByPrimaryKey" parameterType="com.cxy.model.Order" >
    70. update t_hibernate_order
    71. set order_no = #{orderNo,jdbcType=VARCHAR}
    72. where order_id = #{orderId,jdbcType=INTEGER}
    73. update>
    74. mapper>

    orderBiz.java

    1. package com.cxy.biz;
    2. import com.cxy.model.Order;
    3. import com.cxy.model.vo.OrderVo;
    4. import org.apache.ibatis.annotations.Param;
    5. public interface OrderBiz {
    6. int deleteByPrimaryKey(Integer orderId);
    7. int insert(Order record);
    8. int insertSelective(Order record);
    9. Order selectByPrimaryKey(Integer orderId);
    10. int updateByPrimaryKeySelective(Order record);
    11. int updateByPrimaryKey(Order record);
    12. OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);
    13. }

    orderBizImpl.java

    1. package com.cxy.biz.impl;
    2. import com.cxy.biz.OrderBiz;
    3. import com.cxy.mapper.OrderMapper;
    4. import com.cxy.model.Order;
    5. import com.cxy.model.vo.OrderVo;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. @Service
    9. public class OrderBizImpl implements OrderBiz {
    10. @Autowired
    11. private OrderMapper orderMapper ;
    12. @Override
    13. public int deleteByPrimaryKey(Integer orderId) {
    14. return orderMapper.deleteByPrimaryKey(orderId);
    15. }
    16. @Override
    17. public int insert(Order record) {
    18. return orderMapper.insert(record);
    19. }
    20. @Override
    21. public int insertSelective(Order record) {
    22. return orderMapper.insertSelective(record);
    23. }
    24. @Override
    25. public Order selectByPrimaryKey(Integer orderId) {
    26. return orderMapper.selectByPrimaryKey(orderId);
    27. }
    28. @Override
    29. public int updateByPrimaryKeySelective(Order record) {
    30. return orderMapper.updateByPrimaryKeySelective(record);
    31. }
    32. @Override
    33. public int updateByPrimaryKey(Order record) {
    34. return orderMapper.updateByPrimaryKey(record);
    35. }
    36. @Override
    37. public OrderVo queryOrderVoByOrderId(Integer orderId) {
    38. return orderMapper.queryOrderVoByOrderId(orderId);
    39. }
    40. }

    OrderitemMapper.xml

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    2. <mapper namespace="com.cxy.mapper.OrderItemMapper" >
    3. <resultMap id="BaseResultMap" type="com.cxy.model.OrderItem" >
    4. <constructor >
    5. <idArg column="order_item_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
    6. <arg column="product_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
    7. <arg column="quantity" jdbcType="INTEGER" javaType="java.lang.Integer" />
    8. <arg column="oid" jdbcType="INTEGER" javaType="java.lang.Integer" />
    9. constructor>
    10. resultMap>
    11. <resultMap id="OrderVoMap" type="com.cxy.model.vo.OrderItemVo" >
    12. <result property="orderItemId" column="order_item_id">result>
    13. <result property="oid" column="oid">result>
    14. <result property="productId" column="product_id">result>
    15. <result property="quantity" column="quantity">result>
    16. <association property="order" javaType="com.cxy.model.Order">
    17. <result property="orderId" column="order_id">result>
    18. <result property="orderNo" column="order_no">result>
    19. association>
    20. resultMap>
    21. <sql id="Base_Column_List" >
    22. order_item_id, product_id, quantity, oid
    23. sql>
    24. <select id="queryOrderItemVoByOrderItemId" resultMap="OrderVoMap" parameterType="java.lang.Integer">
    25. select * from t_hibernate_order o,t_hibernate_order_item oi
    26. where o.order_id = oi.oid
    27. and oi.order_item_id = #{orderItemId}
    28. select>
    29. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    30. select
    31. <include refid="Base_Column_List" />
    32. from t_hibernate_order_item
    33. where order_item_id = #{orderItemId,jdbcType=INTEGER}
    34. select>
    35. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    36. delete from t_hibernate_order_item
    37. where order_item_id = #{orderItemId,jdbcType=INTEGER}
    38. delete>
    39. <insert id="insert" parameterType="com.cxy.model.OrderItem" >
    40. insert into t_hibernate_order_item (order_item_id, product_id, quantity,
    41. oid)
    42. values (#{orderItemId,jdbcType=INTEGER}, #{productId,jdbcType=INTEGER}, #{quantity,jdbcType=INTEGER},
    43. #{oid,jdbcType=INTEGER})
    44. insert>
    45. <insert id="insertSelective" parameterType="com.cxy.model.OrderItem" >
    46. insert into t_hibernate_order_item
    47. <trim prefix="(" suffix=")" suffixOverrides="," >
    48. <if test="orderItemId != null" >
    49. order_item_id,
    50. if>
    51. <if test="productId != null" >
    52. product_id,
    53. if>
    54. <if test="quantity != null" >
    55. quantity,
    56. if>
    57. <if test="oid != null" >
    58. oid,
    59. if>
    60. trim>
    61. <trim prefix="values (" suffix=")" suffixOverrides="," >
    62. <if test="orderItemId != null" >
    63. #{orderItemId,jdbcType=INTEGER},
    64. if>
    65. <if test="productId != null" >
    66. #{productId,jdbcType=INTEGER},
    67. if>
    68. <if test="quantity != null" >
    69. #{quantity,jdbcType=INTEGER},
    70. if>
    71. <if test="oid != null" >
    72. #{oid,jdbcType=INTEGER},
    73. if>
    74. trim>
    75. insert>
    76. <update id="updateByPrimaryKeySelective" parameterType="com.cxy.model.OrderItem" >
    77. update t_hibernate_order_item
    78. <set >
    79. <if test="productId != null" >
    80. product_id = #{productId,jdbcType=INTEGER},
    81. if>
    82. <if test="quantity != null" >
    83. quantity = #{quantity,jdbcType=INTEGER},
    84. if>
    85. <if test="oid != null" >
    86. oid = #{oid,jdbcType=INTEGER},
    87. if>
    88. set>
    89. where order_item_id = #{orderItemId,jdbcType=INTEGER}
    90. update>
    91. <update id="updateByPrimaryKey" parameterType="com.cxy.model.OrderItem" >
    92. update t_hibernate_order_item
    93. set product_id = #{productId,jdbcType=INTEGER},
    94. quantity = #{quantity,jdbcType=INTEGER},
    95. oid = #{oid,jdbcType=INTEGER}
    96. where order_item_id = #{orderItemId,jdbcType=INTEGER}
    97. update>
    98. mapper>

    orderitemBiz.java

    1. package com.cxy.biz;
    2. import com.cxy.model.OrderItem;
    3. import com.cxy.model.vo.OrderItemVo;
    4. import org.apache.ibatis.annotations.Param;
    5. public interface OrderItemBia {
    6. int deleteByPrimaryKey(Integer orderItemId);
    7. int insert(OrderItem record);
    8. int insertSelective(OrderItem record);
    9. OrderItem selectByPrimaryKey(Integer orderItemId);
    10. int updateByPrimaryKeySelective(OrderItem record);
    11. int updateByPrimaryKey(OrderItem record);
    12. OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
    13. }

    orderitemBizImpl.java

    1. package com.cxy.biz.impl;
    2. import com.cxy.biz.OrderItemBia;
    3. import com.cxy.mapper.OrderItemMapper;
    4. import com.cxy.model.OrderItem;
    5. import com.cxy.model.vo.OrderItemVo;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. @Service
    9. public class OrderItemBizImpl implements OrderItemBia {
    10. @Autowired
    11. private OrderItemMapper orderItemMapper;
    12. @Override
    13. public int deleteByPrimaryKey(Integer orderItemId) {
    14. return orderItemMapper.deleteByPrimaryKey(orderItemId);
    15. }
    16. @Override
    17. public int insert(OrderItem record) {
    18. return orderItemMapper.insert(record);
    19. }
    20. @Override
    21. public int insertSelective(OrderItem record) {
    22. return orderItemMapper.insertSelective(record);
    23. }
    24. @Override
    25. public OrderItem selectByPrimaryKey(Integer orderItemId) {
    26. return orderItemMapper.selectByPrimaryKey(orderItemId);
    27. }
    28. @Override
    29. public int updateByPrimaryKeySelective(OrderItem record) {
    30. return orderItemMapper.updateByPrimaryKeySelective(record);
    31. }
    32. @Override
    33. public int updateByPrimaryKey(OrderItem record) {
    34. return orderItemMapper.updateByPrimaryKey(record);
    35. }
    36. @Override
    37. public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
    38. return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
    39. }
    40. }
    1. package com.cxy.biz.impl;
    2. import com.cxy.model.vo.OrderItemVo;
    3. import org.junit.Test;
    4. import org.junit.runner.RunWith;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.test.context.ContextConfiguration;
    7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    8. import static org.junit.Assert.*;
    9. @RunWith(SpringJUnit4ClassRunner.class)
    10. @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
    11. public class OrderItemBizImplTest {
    12. @Autowired
    13. private OrderItemBizImpl orderItemBizimpl;
    14. @Test
    15. public void queryOrderItemVoByOrderItemId() {
    16. OrderItemVo orderitemVo = orderItemBizimpl.queryOrderItemVoByOrderItemId(43);
    17. System.out.println(orderitemVo);
    18. }
    19. }

     二、多对多关联关系

    什么是多对多?

    比如一本书会包含多种类型(言情,玄幻等),而类型里包含多本书籍

    首先先用逆向生成工具生成t_hibernate_bookt_hibernate_book_categoryt_hibernate_category,这两张表对应的model与mapper

    HbookMapper.xml

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    2. <mapper namespace="com.cxy.mapper.HbookMapper" >
    3. <resultMap id="BaseResultMap" type="com.cxy.model.Hbook" >
    4. <constructor >
    5. <idArg column="book_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
    6. <arg column="book_name" jdbcType="VARCHAR" javaType="java.lang.String" />
    7. <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
    8. constructor>
    9. resultMap>
    10. <sql id="Base_Column_List" >
    11. book_id, book_name, price
    12. sql>
    13. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    14. select
    15. <include refid="Base_Column_List" />
    16. from t_hibernate_book
    17. where book_id = #{bookId,jdbcType=INTEGER}
    18. select>
    19. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    20. delete from t_hibernate_book
    21. where book_id = #{bookId,jdbcType=INTEGER}
    22. delete>
    23. <insert id="insert" parameterType="com.cxy.model.Hbook" >
    24. insert into t_hibernate_book (book_id, book_name, price
    25. )
    26. values (#{bookId,jdbcType=INTEGER}, #{bookName,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
    27. )
    28. insert>
    29. <insert id="insertSelective" parameterType="com.cxy.model.Hbook" >
    30. insert into t_hibernate_book
    31. <trim prefix="(" suffix=")" suffixOverrides="," >
    32. <if test="bookId != null" >
    33. book_id,
    34. if>
    35. <if test="bookName != null" >
    36. book_name,
    37. if>
    38. <if test="price != null" >
    39. price,
    40. if>
    41. trim>
    42. <trim prefix="values (" suffix=")" suffixOverrides="," >
    43. <if test="bookId != null" >
    44. #{bookId,jdbcType=INTEGER},
    45. if>
    46. <if test="bookName != null" >
    47. #{bookName,jdbcType=VARCHAR},
    48. if>
    49. <if test="price != null" >
    50. #{price,jdbcType=REAL},
    51. if>
    52. trim>
    53. insert>
    54. <update id="updateByPrimaryKeySelective" parameterType="com.cxy.model.Hbook" >
    55. update t_hibernate_book
    56. <set >
    57. <if test="bookName != null" >
    58. book_name = #{bookName,jdbcType=VARCHAR},
    59. if>
    60. <if test="price != null" >
    61. price = #{price,jdbcType=REAL},
    62. if>
    63. set>
    64. where book_id = #{bookId,jdbcType=INTEGER}
    65. update>
    66. <update id="updateByPrimaryKey" parameterType="com.cxy.model.Hbook" >
    67. update t_hibernate_book
    68. set book_name = #{bookName,jdbcType=VARCHAR},
    69. price = #{price,jdbcType=REAL}
    70. where book_id = #{bookId,jdbcType=INTEGER}
    71. update>
    72. mapper>

    HbookCategoryBiz.java 

    1. package com.cxy.biz;
    2. import com.cxy.model.HbookCategory;
    3. import com.cxy.model.vo.CategoryVo;
    4. import com.cxy.model.vo.HbookVo;
    5. public interface HbookCategoryBiz {
    6. int deleteByPrimaryKey(Integer bcid);
    7. int insert(HbookCategory record);
    8. int insertSelective(HbookCategory record);
    9. HbookCategory selectByPrimaryKey(Integer bcid);
    10. int updateByPrimaryKeySelective(HbookCategory record);
    11. int updateByPrimaryKey(HbookCategory record);
    12. HbookVo queryByBookId( Integer bookId);
    13. CategoryVo queryByCid( Integer cid);
    14. }

    HbookCategoryBizImpl .java 

    1. package com.cxy.biz.impl;
    2. import com.cxy.biz.HbookCategoryBiz;
    3. import com.cxy.mapper.HbookCategoryMapper;
    4. import com.cxy.model.HbookCategory;
    5. import com.cxy.model.vo.CategoryVo;
    6. import com.cxy.model.vo.HbookVo;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.stereotype.Service;
    9. @Service
    10. public class HbookCategoryBizImpl implements HbookCategoryBiz {
    11. @Autowired
    12. private HbookCategoryMapper hbookCategoryMapper;
    13. @Override
    14. public HbookVo queryByBookId(Integer bookId) {
    15. return hbookCategoryMapper.queryByBookId(bookId);
    16. }
    17. @Override
    18. public CategoryVo queryByCid(Integer cid) {
    19. return hbookCategoryMapper.queryByCid(cid);
    20. }
    21. }
    1. package com.cxy.biz.impl;
    2. import com.cxy.biz.HbookCategoryBiz;
    3. import com.cxy.model.vo.CategoryVo;
    4. import com.cxy.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. @RunWith(SpringJUnit4ClassRunner.class)
    12. @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
    13. public class HbookCategoryBizImplTest {
    14. @Autowired
    15. private HbookCategoryBiz hbookCategoryBiz;
    16. @Test
    17. public void queryByBookId() {
    18. HbookVo hbookVo = hbookCategoryBiz.queryByBookId(8);
    19. System.out.println(hbookVo);
    20. hbookVo.getCategories().forEach(System.out::println);
    21. }
    22. @Test
    23. public void queryByCid() {
    24. CategoryVo categoryVo = hbookCategoryBiz.queryByCid(8);
    25. System.out.println(categoryVo);
    26. categoryVo.getHbooks().forEach(System.out::println);
    27. }
    28. }

  • 相关阅读:
    硕士毕设方向求建议!
    金仓数据库 KingbaseGIS 使用手册(6.17. 边界框函数)
    927. 三等分 模拟
    新手怎样快速上手接口测试?掌握这几个知识点直接起飞!
    python中的上下文管理器
    SSM学习47:SpringMvc五种参数传递
    一个简单的iOS天气应用程序源码
    云效流水线docker部署 :node.js镜像部署VUE项目
    SEO的优化教程(百度SEO的介绍和优化)
    postgresql源码学习(34)—— 事务日志⑩ - 全页写机制
  • 原文地址:https://blog.csdn.net/m0_62528678/article/details/126353430