• 关联关系映射


    目录

    一、一对多关联关系

    1.一对多的一方 

     2.多的一方

    二、多对多 

     三、总结


    一、一对多关联关系

    mysql数据库表 t_hibernate_ordert_hibernate_order_item

    表数据 

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

    这两张表对应的model于mapper

    generatorConfig.xml

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

    生成好了之后注意如图所示:

    1.一对多的一方 

    需求1:通过订单号查询本次订单的详细信息及对应的所以订单信息
    select * from t_hibernate_order o,t_hibernate_order_item oi
    where o.order_id = oi.oid and o.order_id = 9

    数据库里查询出的结果如图所示:

     

     

    OrderVo.java

    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 www.javajwj.com
    8. * @company xxx公司
    9. * @create  2022-08-16 21:40
    10. *
    11. * vo类不仅仅包含当前表的信息,还能包含相关联表的信息
    12. *
    13. * 当前订单会议一个或者多个订单项,多个订单项必须要集合进行接收
    14. */
    15. public class OrderVo 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. }

     OrderItemVo.java

    1. package com.zking.ssm.model.vo;
    2. import com.zking.ssm.model.Order;
    3. import com.zking.ssm.model.OrderItem;
    4. /**
    5. * @author 敢敢
    6. * @site www.javajwj.com
    7. * @company xxx公司
    8. * @create  2022-08-16 21:43
    9. *
    10. * 某一个订单项一定属于某个订单的
    11. */
    12. public class OrderItemVo extends OrderItem {
    13. private Order order;
    14. public Order getOrder() {
    15. return order;
    16. }
    17. public void setOrder(Order order) {
    18. this.order = order;
    19. }
    20. }

     OrderMapper.xml

    1. "1.0" encoding="UTF-8" ?>
    2. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    3. <mapper namespace="com.zking.ssm.mapper.OrderMapper" >
    4. <resultMap id="BaseResultMap" type="com.zking.ssm.model.Order" >
    5. <constructor >
    6. <idArg column="order_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
    7. <arg column="order_no" jdbcType="VARCHAR" javaType="java.lang.String" />
    8. constructor>
    9. resultMap>
    10. <resultMap id="OrderVoMap" type="com.zking.ssm.model.vo.OrderVo" >
    11. <result column="order_id" property="orderId">result>
    12. <result column="order_no" property="orderNo">result>
    13. <collection property="orderItems" ofType="com.zking.ssm.model.OrderItem">
    14. <result column="order_item_id" property="orderItemId">result>
    15. <result column="product_id" property="productId">result>
    16. <result column="quantity" property="quantity">result>
    17. <result column="oid" property="oid">result>
    18. collection>
    19. resultMap>
    20. <sql id="Base_Column_List" >
    21. order_id, order_no
    22. sql>
    23. <select id="queryOrderVoByOrderId" resultMap="OrderVoMap" parameterType="java.lang.Integer" >
    24. select * from t_hibernate_order o,t_hibernate_order_item oi
    25. where o.order_id = oi.oid 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.zking.ssm.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.zking.ssm.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.zking.ssm.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.zking.ssm.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>

    OrderMapper.java

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

    OrderBiz.java

    1. package com.zking.ssm.biz;
    2. import com.zking.ssm.model.Order;
    3. import com.zking.ssm.model.vo.OrderVo;
    4. import org.apache.ibatis.annotations.Param;
    5. /**
    6. * @author 敢敢
    7. * @site www.javajwj.com
    8. * @company xxx公司
    9. * @create  2022-08-16 22:00
    10. */
    11. public interface OrderBiz {
    12. OrderVo queryOrderVoByOrderId(Integer orderId);
    13. }

    OrderBizImpl.java

    1. package com.zking.ssm.biz;
    2. import com.zking.ssm.mapper.OrderMapper;
    3. import com.zking.ssm.model.Order;
    4. import com.zking.ssm.model.vo.OrderVo;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. /**
    8. * @author 敢敢
    9. * @site www.javajwj.com
    10. * @company xxx公司
    11. * @create  2022-08-16 22:01
    12. */
    13. @Service
    14. public class OrderBizImpl implements OrderBiz {
    15. @Autowired
    16. private OrderMapper orderMapper;
    17. @Override
    18. public OrderVo queryOrderVoByOrderId(Integer orderId) {
    19. return orderMapper.queryOrderVoByOrderId(orderId);
    20. }
    21. }

    测试类:OrderBizImplTest.java

    1. package com.zking.ssm.biz;
    2. import com.zking.ssm.model.vo.OrderVo;
    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. /**
    10. * @author 敢敢
    11. * @site www.javajwj.com
    12. * @company xxx公司
    13. * @create  2022-08-16 22:02
    14. */
    15. @RunWith(SpringJUnit4ClassRunner.class)
    16. @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
    17. public class OrderBizImplTest {
    18. @Autowired
    19. private OrderBiz orderBiz;
    20. @Test
    21. public void queryOrderVoByOrderId() {
    22. OrderVo orderVo = orderBiz.queryOrderVoByOrderId(9);
    23. System.out.println(orderVo);
    24. orderVo.getOrderItems().forEach(System.out::println);
    25. }
    26. }

    运行结果如图所示:

     

    我们在pom.xml引入了这个 

     

    我们把实体类的Order.java所以的方法给删除,我们在这里面加一个@Data

    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. @Data
    8. //一个是有参构造器一个是无参构造器
    9. //@NoArgsConstructor
    10. //@AllArgsConstructor
    11. public class Order {
    12. private Integer orderId;
    13. private String orderNo;
    14. }

    现在在去运行一下如图所示:把之前的地址换成了数据

      

     2.多的一方

    需求2:通过订单项ID查询出订单项详细信息及所属订单
    select * from t_hibernate_order o,t_hibernate_order_item oi
    where o.order_id = oi.oid and oi.order_item_id=43
    数据库查询出的结果如图所示:

     

    OrderItemMapper.xml

    1. "1.0" encoding="UTF-8" ?>
    2. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    3. <mapper namespace="com.zking.ssm.mapper.OrderItemMapper" >
    4. <resultMap id="BaseResultMap" type="com.zking.ssm.model.OrderItem" >
    5. <constructor >
    6. <idArg column="order_item_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
    7. <arg column="product_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
    8. <arg column="quantity" jdbcType="INTEGER" javaType="java.lang.Integer" />
    9. <arg column="oid" jdbcType="INTEGER" javaType="java.lang.Integer" />
    10. constructor>
    11. resultMap>
    12. <resultMap id="OrderItemVoMap" type="com.zking.ssm.model.vo.OrderItemVo" >
    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. <association property="order" javaType="com.zking.ssm.model.Order">
    18. <result column="order_id" property="orderId">result>
    19. <result column="order_no" property="orderNo">result>
    20. association>
    21. resultMap>
    22. <sql id="Base_Column_List" >
    23. order_item_id, product_id, quantity, oid
    24. sql>
    25. <select id="queryOrderItemVoByOrderItemId" resultMap="OrderItemVoMap" parameterType="java.lang.Integer" >
    26. select * from t_hibernate_order o,t_hibernate_order_item oi
    27. where o.order_id = oi.oid 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.zking.ssm.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.zking.ssm.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.zking.ssm.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.zking.ssm.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>

     OrderItemMapper.java

    1. package com.zking.ssm.mapper;
    2. import com.zking.ssm.model.OrderItem;
    3. import com.zking.ssm.model.vo.OrderItemVo;
    4. import org.apache.ibatis.annotations.Param;
    5. public interface OrderItemMapper {
    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. }

     OrderItemBiz.java

    1. package com.zking.ssm.biz;
    2. import com.zking.ssm.model.vo.OrderItemVo;
    3. import org.apache.ibatis.annotations.Param;
    4. /**
    5. * @author 敢敢
    6. * @site www.javajwj.com
    7. * @company xxx公司
    8. * @create  2022-08-16 22:36
    9. */
    10. public interface OrderItemBiz {
    11. OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId);
    12. }

    OrderItemBizImpl.java

    1. package com.zking.ssm.biz.impl;
    2. import com.zking.ssm.biz.OrderItemBiz;
    3. import com.zking.ssm.mapper.OrderItemMapper;
    4. import com.zking.ssm.model.OrderItem;
    5. import com.zking.ssm.model.vo.OrderItemVo;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. /**
    9. * @author 敢敢
    10. * @site www.javajwj.com
    11. * @company xxx公司
    12. * @create  2022-08-16 22:38
    13. */
    14. @Service
    15. public class OrderItemBizImpl implements OrderItemBiz {
    16. @Autowired
    17. private OrderItemMapper orderItemMapper;
    18. @Override
    19. public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
    20. return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
    21. }
    22. }

    在我们的OrderItem.java里面加一个@ToString就好了,这样就不会是地址了

    1. package com.zking.ssm.model;
    2. import lombok.ToString;
    3. @ToString
    4. public class OrderItem {
    5. private Integer orderItemId;
    6. private Integer productId;
    7. private Integer quantity;
    8. private Integer oid;
    9. public OrderItem(Integer orderItemId, Integer productId, Integer quantity, Integer oid) {
    10. this.orderItemId = orderItemId;
    11. this.productId = productId;
    12. this.quantity = quantity;
    13. this.oid = oid;
    14. }
    15. public OrderItem() {
    16. super();
    17. }
    18. public Integer getOrderItemId() {
    19. return orderItemId;
    20. }
    21. public void setOrderItemId(Integer orderItemId) {
    22. this.orderItemId = orderItemId;
    23. }
    24. public Integer getProductId() {
    25. return productId;
    26. }
    27. public void setProductId(Integer productId) {
    28. this.productId = productId;
    29. }
    30. public Integer getQuantity() {
    31. return quantity;
    32. }
    33. public void setQuantity(Integer quantity) {
    34. this.quantity = quantity;
    35. }
    36. public Integer getOid() {
    37. return oid;
    38. }
    39. public void setOid(Integer oid) {
    40. this.oid = oid;
    41. }
    42. }

     测试类OrderItemBizTest.java

    1. package com.zking.ssm.biz;
    2. import com.zking.ssm.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. /**
    10. * @author 敢敢
    11. * @site www.javajwj.com
    12. * @company xxx公司
    13. * @create  2022-08-16 22:45
    14. */
    15. @RunWith(SpringJUnit4ClassRunner.class)
    16. @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
    17. public class OrderItemBizImplTest {
    18. @Autowired
    19. private OrderItemBiz orderItemBiz;
    20. @Test
    21. public void queryOrderItemVoByOrderItemId() {
    22. OrderItemVo orderItemVo = orderItemBiz.queryOrderItemVoByOrderItemId(43);
    23. System.out.println(orderItemVo);
    24. System.out.println(orderItemVo.getOrder());
    25. }
    26. }

     运行结果如图所示:

    二、多对多 

     多对多就是两个一对多

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

     generatorConfig.xml

    1. <table schema="" tableName="t_hibernate_book" domainObjectName="Hbook"
    2. enableCountByExample="false" enableDeleteByExample="false"
    3. enableSelectByExample="false" enableUpdateByExample="false">
    4. table>
    5. <table schema="" tableName="t_hibernate_category" domainObjectName="Category"
    6. enableCountByExample="false" enableDeleteByExample="false"
    7. enableSelectByExample="false" enableUpdateByExample="false">
    8. table>
    9. <table schema="" tableName="t_hibernate_book_category" domainObjectName="HbookCategory"
    10. enableCountByExample="false" enableDeleteByExample="false"
    11. enableSelectByExample="false" enableUpdateByExample="false">
    12. table>

    需求1:根据书籍ID,查询书籍信息及所属所有类别信息

    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 = 8

     需求2:根据类别ID,查询类别信息及所包含书籍信息

    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=8

     

    HbookVo.java

    1. package com.zking.ssm.model.vo;
    2. import com.zking.ssm.model.Category;
    3. import com.zking.ssm.model.Hbook;
    4. import java.util.List;
    5. /**
    6. * @author 敢敢
    7. * @site www.javajwj.com
    8. * @company xxx公司
    9. * @create  2022-08-16 23:18
    10. */
    11. public class HbookVo extends Hbook {
    12. private List categories;
    13. public List getCategories() {
    14. return categories;
    15. }
    16. public void setCategories(List categories) {
    17. this.categories = categories;
    18. }
    19. }

     CategoryVo.java

    1. package com.zking.ssm.model.vo;
    2. import com.zking.ssm.model.Category;
    3. import com.zking.ssm.model.Hbook;
    4. import java.util.List;
    5. /**
    6. * @author 敢敢
    7. * @site www.javajwj.com
    8. * @company xxx公司
    9. * @create  2022-08-16 23:19
    10. */
    11. public class CategoryVo extends Category {
    12. private List hbooks;
    13. public List getHbooks() {
    14. return hbooks;
    15. }
    16. public void setHbooks(List hbooks) {
    17. this.hbooks = hbooks;
    18. }
    19. }

     加到中间表HbookCategoryMapper.xml,要不然你两边表都要加

    1. <resultMap id="HbookVoMap" type="com.zking.ssm.model.vo.HbookVo">
    2. <result property="bookId" column="book_id">result>
    3. <result property="bookName" column="book_name">result>
    4. <collection property="categories" ofType="com.javaxl.ssm.model.Category">
    5. <result property="categoryId" column="category_id">result>
    6. <result property="categoryName" column="category_name">result>
    7. collection>
    8. resultMap>
    9. <resultMap id="CategoryVoMap" type="com.zking.ssm.model.vo.CategoryVo">
    10. <result property="categoryId" column="category_id">result>
    11. <result property="categoryName" column="category_name">result>
    12. <collection property="hbooks" ofType="com.javaxl.ssm.model.Hbook">
    13. <result property="bookId" column="book_id">result>
    14. <result property="bookName" column="book_name">result>
    15. collection>
    16. resultMap>
    17. <select id="queryByBookId" resultMap="HbookVoMap" parameterType="java.lang.Integer">
    18. select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
    19. where b.book_id = bc.bid and bc.cid = c.category_id and b.book_id = #{bookId}
    20. select>
    21. <select id="queryByCid" resultMap="CategoryVoMap" parameterType="java.lang.Integer">
    22. select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
    23. where b.book_id = bc.bid and bc.cid = c.category_id and c.category_id=#{cid}
    24. select>
    HbookCategoryMapper.java

     

    1. package com.zking.ssm.mapper;
    2. import com.zking.ssm.model.HbookCategory;
    3. import com.zking.ssm.model.vo.CategoryVo;
    4. import com.zking.ssm.model.vo.HbookVo;
    5. import org.apache.ibatis.annotations.Param;
    6. public interface HbookCategoryMapper {
    7. int deleteByPrimaryKey(Integer bcid);
    8. int insert(HbookCategory record);
    9. int insertSelective(HbookCategory record);
    10. HbookCategory selectByPrimaryKey(Integer bcid);
    11. int updateByPrimaryKeySelective(HbookCategory record);
    12. int updateByPrimaryKey(HbookCategory record);
    13. HbookVo queryByBookId(@Param("bookId") Integer bookId);
    14. CategoryVo queryByCid(@Param("cid") Integer cid);
    15. }
    HbookCategoryBiz.java
    1. package com.zking.ssm.biz;
    2. import com.zking.ssm.model.vo.CategoryVo;
    3. import com.zking.ssm.model.vo.HbookVo;
    4. import org.apache.ibatis.annotations.Param;
    5. /**
    6. * @author 敢敢
    7. * @site www.javajwj.com
    8. * @company xxx公司
    9. * @create  2022-08-16 23:27
    10. */
    11. public interface HbookCategoryBiz {
    12. HbookVo queryByBookId(Integer bookId);
    13. CategoryVo queryByCid(Integer cid);
    14. }
    HbookCategoryBizImpl.java
    1. package com.zking.ssm.biz.impl;
    2. import com.zking.ssm.biz.HbookCategoryBiz;
    3. import com.zking.ssm.mapper.HbookCategoryMapper;
    4. import com.zking.ssm.model.vo.CategoryVo;
    5. import com.zking.ssm.model.vo.HbookVo;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. /**
    9. * @author 敢敢
    10. * @site www.javajwj.com
    11. * @company xxx公司
    12. * @create  2022-08-16 23:28
    13. */
    14. @Service
    15. public class HbookCategoryBizImpl implements HbookCategoryBiz {
    16. @Autowired
    17. private HbookCategoryMapper hbookCategoryMapper;
    18. @Override
    19. public HbookVo queryByBookId(Integer bookId) {
    20. return hbookCategoryMapper.queryByBookId(bookId);
    21. }
    22. @Override
    23. public CategoryVo queryByCid(Integer cid) {
    24. return hbookCategoryMapper.queryByCid(cid);
    25. }
    26. }
    HbookVo.java  加一个ToString不然生成的就是地址
    1. package com.zking.ssm.model.vo;
    2. import com.zking.ssm.model.Category;
    3. import com.zking.ssm.model.Hbook;
    4. import lombok.ToString;
    5. import java.util.List;
    6. /**
    7. * @author 敢敢
    8. * @site www.javajwj.com
    9. * @company xxx公司
    10. * @create  2022-08-16 23:18
    11. */
    12. @ToString
    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. }
    Category.java 也生成一个@ToString
    1. package com.zking.ssm.model;
    2. import lombok.ToString;
    3. @ToString
    4. public class Category {
    5. private Integer categoryId;
    6. private String categoryName;
    7. public Category(Integer categoryId, String categoryName) {
    8. this.categoryId = categoryId;
    9. this.categoryName = categoryName;
    10. }
    11. public Category() {
    12. super();
    13. }
    14. public Integer getCategoryId() {
    15. return categoryId;
    16. }
    17. public void setCategoryId(Integer categoryId) {
    18. this.categoryId = categoryId;
    19. }
    20. public String getCategoryName() {
    21. return categoryName;
    22. }
    23. public void setCategoryName(String categoryName) {
    24. this.categoryName = categoryName;
    25. }
    26. }

    生成一个测试类   HbookCategoryBizImplTest.java

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

     第一个方法运行结果如图所示:

     第二个方法运行结果如图所示:

     

     三、总结

     一对多

    一方:Collection:属性ofType

    多方:associate:属性JavaType

    多对多:就是两个一对多

    vo类:view Object

    entity/model描述数据库表

  • 相关阅读:
    MySQL之创建高性能的索引(十二)
    如何定制.NET6.0的日志记录
    Qt day2
    webpack配置css-loader让scss文件支持模块化引入
    react源码分析:深度理解React.Context
    supOS数据采集及接入-IoT网关接入操作
    Geogebra 教程之 04 Geogebra 小程序
    H5自适应图片-picture标签实现
    【BM2 链表内指定区间反转】
    spring高级篇(二)
  • 原文地址:https://blog.csdn.net/weixin_67465673/article/details/126374291