当前位置:   article > 正文

SpringBoot结合Mybatis使用_springboot引入mybatis依赖

springboot引入mybatis依赖

SpringBoot结合Mybatis使用

一、基础工作

  1. 在Spring Boot项目的pom.xml文件中添加依赖

    <!-- springboot:2.7.4 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- mybatis -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  2. 配置数据源:在application.properties(或application.yml)文件中配置数据源信息,如数据库连接URL、用户名和密码等。

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/yibinstore?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        username: root
        password: root
    mybatis:
      mapper-locations: classpath:mapper/*.xml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

二、具体使用案例

项目结构:

在这里插入图片描述

  1. 编写实体类

    @Setter
    @Getter
    @NoArgsConstructor
    @Accessors(chain = true)
    public class Order implements Serializable {
    
        /**
         * 订单id
         */
        private Integer oid;
    
        /**
         * 用户id
         */
        private Integer uid;
    
        /**
         * 收货人姓名
         */
        private String recvName;
    
        /**
         * 收货人电话
         */
        private String recvPhone;
    
        /**
         * 收货地址
         */
        private String recvAddress;
    
        /**
         * 总价
         */
        private Long totalPrice;
    
        /**
         * 状态:0-未支付,1-已支付
         */
        private Integer status;
    
        /**
         * 下单时间
         */
    
        private Date orderTime;
    
        /**
         * 支付时间
         */
        private Date payTime;
    
        /**
         * 创建人
         */
        private String createdUser;
    
        /**
         * 创建时间
         */
        private Date createdTime;
    
        /**
         * 最后修改执行人
         */
        private String modifiedUser;
    
        /**
         * 最后修改时间
         */
        private Date modifiedTime;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
  2. 创建Mapper接口和XML文件:创建一个Mapper接口,用于定义数据库操作方法,并创建对应的XML文件,用于编写SQL语句。

    • mapper文件

      @Mapper
      public interface OrderMapper {
      
          /**
           * 新增订单信息
           * @param order 订单数据
           */
          int addOrder(Order order);
      
          /**
           * 更改订单信息
           * @param oid 订单id
           * @param status 订单状态:
           *               0-未支付
           *               1-已支付
           * @param orderTime 支付时间
           */
          int updateOrder(@Param("oid") Integer oid,
              @Param("status") Integer status,
              @Param("orderTime") Date orderTime);
      
          /**
           * 查询订单
           * @param oid 订单id
           */
          Order selectOrderByOid(Integer oid);
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
    • 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.aben.union.mapper.OrderMapper">
      
          <resultMap id="resultMapOrder" type="com.aben.union.entity.Order">
              <!-- 主键不可以省略,一定要写 -->
              <!-- colum:数据库中的列名  property:java类中对应的列名    相同时可以省略 -->
              <id column="oid" property="oid"></id>
              <result column="recv_name" property="recvName"></result>
              <result column="recv_phone" property="recvPhone"></result>
              <result column="recv_address" property="recvAddress"></result>
              <result column="total_price" property="totalPrice"></result>
              <result column="order_time" property="orderTime"></result>
              <result column="pay_time" property="payTime"></result>
              <result column="created_user" property="createdUser"></result>
              <result column="created_time" property="createdTime"></result>
              <result column="modified_user" property="modifiedUser"></result>
              <result column="modified_time" property="modifiedTime"></result>
          </resultMap>
      
          <insert id="addOrder" parameterType="com.aben.union.entity.Order" useGeneratedKeys="true" keyProperty="oid">
              insert into t_order(uid,recv_name,recv_phone,recv_address,total_price,
                                  status,order_time,pay_time,created_user,created_time,
                                  modified_user,modified_time)
              values(#{uid},#{recvName},#{recvPhone},#{recvAddress},#{totalPrice},
                      #{status},#{orderTime},#{payTime},#{createdUser},
                      #{createdTime},#{modifiedUser},#{modifiedTime});
          </insert>
      
          <update id="updateOrder">
              update
                  t_order
              set
                  status=#{status} and order_time=#{orderTime}
              where
                   oid=#{oid}
          </update>
      
          <select id="selectOrderByOid" resultMap="resultMapOrder">
              select * from t_order where oid=#{oid}
          </select>
      
      </mapper>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
  3. 编写service层

    @Service
    public class OrderServiceImpl implements OrderService {
    
        @Inject
        private OrderMapper orderMapper;
    
        @Override
        public Boolean addOrder() {
            Order order = new Order();
            order.setUid(1001)
                .setRecvName("isabener")
                .setRecvAddress("四川省成都市")
                .setRecvPhone("18200000000")
                .setTotalPrice(99L)
                .setStatus(1)
                .setOrderTime(new Date())
                .setPayTime(new Date())
                .setCreatedUser("admin")
                .setCreatedTime(new Date())
                .setModifiedUser("admin")
                .setModifiedTime(new Date());
            return orderMapper.addOrder(order) == 1 ? Boolean.TRUE : Boolean.FALSE;
        }
    
        @Override
        public Boolean updateOrder() {
            return orderMapper.updateOrder(1, 0, new Date()) == 1 ? Boolean.TRUE : Boolean.FALSE;
        }
    
        @Override
        public Order selectOrderByOid() {
            return orderMapper.selectOrderByOid(1);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  4. web层调用

    @RestController
    @RequestMapping("/order")
    public class OrderController {
    
        @Inject
        private OrderService orderService;
    
        @GetMapping("/add")
        public Boolean addOrder() {
            return orderService.addOrder();
        }
    
        @GetMapping("/update")
        public Boolean updateOrder() {
            return orderService.updateOrder();
        }
    
        @GetMapping("/get")
        public Order getOrder() {
            return orderService.selectOrderByOid();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/498177
推荐阅读
相关标签
  

闽ICP备14008679号