SpringBoot之集成MyBatis

  1. 引入工程依赖包
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.16</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
  1. 编写DAO接口
package com.example.mybatis.repository;

import com.example.mybatis.domain.SmsCoupon;
import org.apache.ibatis.annotations.Param;

/**
 * @author shanks on 2019-11-09
 */

public interface SmsCouponDAO {


    SmsCoupon queryById(@Param("id") Integer id);
}
  1. 编写SQL配置文件(本人不太习惯注解,习惯将SQL写在配置文件中)
<?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.example.mybatis.repository.SmsCouponDAO">

    <resultMap type="com.example.mybatis.domain.SmsCoupon" id="SmsCouponMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="type" column="type" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="platform" column="platform" jdbcType="INTEGER"/>
        <result property="count" column="count" jdbcType="INTEGER"/>
        <result property="amount" column="amount" jdbcType="NUMERIC"/>
        <result property="perLimit" column="per_limit" jdbcType="INTEGER"/>
        <result property="minPoint" column="min_point" jdbcType="NUMERIC"/>
        <result property="startTime" column="start_time" jdbcType="TIMESTAMP"/>
        <result property="endTime" column="end_time" jdbcType="TIMESTAMP"/>
        <result property="useType" column="use_type" jdbcType="INTEGER"/>
        <result property="note" column="note" jdbcType="VARCHAR"/>
        <result property="publishCount" column="publish_count" jdbcType="INTEGER"/>
        <result property="useCount" column="use_count" jdbcType="INTEGER"/>
        <result property="receiveCount" column="receive_count" jdbcType="INTEGER"/>
        <result property="enableTime" column="enable_time" jdbcType="TIMESTAMP"/>
        <result property="code" column="code" jdbcType="VARCHAR"/>
        <result property="memberLevel" column="member_level" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="SmsCouponMap">
        select
          id, type, name, platform, count, amount, per_limit, min_point, start_time, end_time, use_type, note, publish_count, use_count, receive_count, enable_time, code, member_level
        from mall.sms_coupon
        where id = #{id}
    </select>

</mapper>
  1. 配置myBatis配置类,也可以放在启动类上
package com.example.mybatis.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author shanks on 2019-11-09
 */
@Configuration
@MapperScan("com.example.mybatis.repository")
public class MyBatisConfig {
}

  1. 配置application.yml文件
spring:
  datasource:
    #数据源基本配置
    url: jdbc:mysql://localhost:3306/mall?allowMultiQueries=true&characterEncoding=utf-8&useSSL=false
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
    druid:
      #连接池配置, 初始化大小,最小,最大
      initial-size: 5
      max-active: 10
      #配置获取连接等待超时的时间
      max-wait: 60000
      #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      time-between-eviction-runs-millis: 60000
      #配置一个连接在池中最小生存的时间,单位是毫秒
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false

mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml
  1. 编写controller,调用MyBatis
package com.example.mybatis.web;

import com.example.mybatis.domain.SmsCoupon;
import com.example.mybatis.service.SmsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author shanks on 2019-11-09
 */
@RestController
@RequestMapping("/voucher/web/")
public class SmsCouponController {

    @Autowired
    SmsCouponService smsCouponService;

    @RequestMapping("/querySmsCouponDetail")
    public SmsCoupon querySmsCouponDetail(@RequestParam("id") int id){
        SmsCoupon smsCoupon = smsCouponService.querySmsCouponDetail(id);
        return smsCoupon;
    }
}
package com.example.mybatis.service;

import com.example.mybatis.domain.SmsCoupon;

/**
 * @author shanks on 2019-11-09
 */
public interface SmsCouponService {
    SmsCoupon querySmsCouponDetail(int id);
}

package com.example.mybatis.service.impl;

import com.example.mybatis.domain.SmsCoupon;
import com.example.mybatis.repository.SmsCouponDAO;
import com.example.mybatis.service.SmsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shanks on 2019-11-09
 */
@Service
public class SmsCouponServiceImpl implements SmsCouponService {

    @Autowired
    private SmsCouponDAO smsCouponDAO;

    @Override
    public SmsCoupon querySmsCouponDetail(int id) {
        SmsCoupon smsCoupon = smsCouponDAO.queryById(id);
        return smsCoupon;
    }
}
package com.example.mybatis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoMybatisApplication.class, args);
    }

}

源代码:https://gitee.com/shanksV/springboot-mybatis.git

posted @ 2019-11-09 23:47  拓仲  阅读(311)  评论(0编辑  收藏  举报