• MyBatis之关联关系


    目录

    一、一对多关联关系

    1、实体类

    ②OrderItem

    2、Biz层

    ①OrderBiz

    3、Biz的实现层Impl

    ①OrderBizImpl

    ②OrderItemBizImpl

    4、mapper层

    ①OrderMapper.java

    ②OrderMapper.xml

    ③OrderItemMapper.java


    一、一对多关联关系

    1、实体类

    首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
    这两张表对应的model与mapper 

    ①Order 

    1. package com.chenchen.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 Integer orderId;
    12. private String orderNo;
    13. }

    ②OrderItem

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

    2、Biz层

    ①OrderBiz

    1. package com.chenchen.ssm.biz;
    2. import com.chenchen.ssm.model.Book;
    3. import com.chenchen.ssm.model.BookVo;
    4. import com.chenchen.ssm.model.Order;
    5. import com.chenchen.ssm.model.vo.OrderVo;
    6. import com.chen.ssm.util.PageBean;
    7. import org.apache.ibatis.annotations.Param;
    8. import java.util.List;
    9. import java.util.Map;
    10. public interface OrderBiz {
    11. int deleteByPrimaryKey(Integer orderId);
    12. int insert(Order record);
    13. int insertSelective(Order record);
    14. Order selectByPrimaryKey(Integer orderId);
    15. OrderVo queryOrderVoByOrderId(Integer orderId);
    16. int updateByPrimaryKeySelective(Order record);
    17. int updateByPrimaryKey(Order record);
    18. }

    3、Biz的实现层Impl

    ①OrderBizImpl

    1. package com.chenchen.ssm.biz.impl;
    2. import com.chenchen.ssm.biz.OrderBiz;
    3. import com.chenchen.ssm.mapper.OrderMapper;
    4. import com.chenchen.ssm.model.Order;
    5. import com.chenchen.ssm.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 0;
    15. }
    16. @Override
    17. public int insert(Order record) {
    18. return 0;
    19. }
    20. @Override
    21. public int insertSelective(Order record) {
    22. return 0;
    23. }
    24. @Override
    25. public Order selectByPrimaryKey(Integer orderId) {
    26. return null;
    27. }
    28. @Override
    29. public OrderVo queryOrderVoByOrderId(Integer orderId) {
    30. return orderMapper.queryOrderVoByOrderId(orderId);
    31. }
    32. @Override
    33. public int updateByPrimaryKeySelective(Order record) {
    34. return 0;
    35. }
    36. @Override
    37. public int updateByPrimaryKey(Order record) {
    38. return 0;
    39. }
    40. }

    ②OrderItemBizImpl

    1. package com.chenchen.ssm.biz.impl;
    2. import com.chenchen.ssm.biz.OrderItemBiz;
    3. import com.chenchen.ssm.mapper.OrderItemMapper;
    4. import com.chenchen.ssm.model.OrderItem;
    5. import com.chenchen.ssm.model.vo.OrderItemVo;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. @Service
    9. public class OrderItemBizImpl implements OrderItemBiz {
    10. @Autowired
    11. private OrderItemMapper orderItemMapper;
    12. @Override
    13. public int deleteByPrimaryKey(Integer orderItemId) {
    14. return 0;
    15. }
    16. @Override
    17. public int insert(OrderItem record) {
    18. return 0;
    19. }
    20. @Override
    21. public int insertSelective(OrderItem record) {
    22. return 0;
    23. }
    24. @Override
    25. public OrderItem selectByPrimaryKey(Integer orderItemId) {
    26. return null;
    27. }
    28. @Override
    29. public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
    30. return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
    31. }
    32. @Override
    33. public int updateByPrimaryKeySelective(OrderItem record) {
    34. return 0;
    35. }
    36. @Override
    37. public int updateByPrimaryKey(OrderItem record) {
    38. return 0;
    39. }
    40. }

    4、mapper层

    ①OrderMapper.java

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

    ②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.chenchen.ssm.mapper.OrderMapper" >
    4. <resultMap id="BaseResultMap" type="com.chenchen.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.chenchen.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.chenchen.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 where
    25. 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.chenchen.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.chenchen.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.chenchen.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.chenchen.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>

    ③OrderItemMapper.java

    1. package com.chenchen.ssm.mapper;
    2. import com.chenchen.ssm.model.OrderItem;
    3. import com.chenchen.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. OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
    11. int updateByPrimaryKeySelective(OrderItem record);
    12. int updateByPrimaryKey(OrderItem record);
    13. }

    queryByBookId运行效果:

    queryByCid运行效果:

  • 相关阅读:
    [GXYCTF2019]禁止套娃
    mac 安装 selenium + chrome driver
    【C++】C++11右值引用
    智力考验看成语猜古诗句微信小程序源码
    Redhat安装oracle 11g
    Java设计模式 --建造者模式【Builder Pattern】
    某金融机构身份国产化LDAP创新实践——国产自主可控 LDAP目录服务建设经验分享
    windows服务器热备、负载均衡配置
    MATLAB环境下基于深度学习的JPEG图像去块(Image Deblocking)
    【21天算法学习】希尔排序
  • 原文地址:https://blog.csdn.net/m0_67094505/article/details/126363824