SpringBoot之集成MyBatis

  1. 引入工程依赖包

            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            mysql
            mysql-connector-java
            5.1.47
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        

        
            com.alibaba
            druid-spring-boot-starter
            1.1.16
        
        
            org.projectlombok
            lombok
            1.18.10
        
  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写在配置文件中)




    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    

  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

你可能感兴趣的