1. 程式人生 > >sprint-boot 整合mybatis+註解方式+配置檔案方式

sprint-boot 整合mybatis+註解方式+配置檔案方式

註解方式

 application.yml

spring:
  datasource:
#   資料來源基本配置
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
# serverTimezone=UTC解決時區問題
    url: jdbc:mysql://127.0.0.1:3306/money?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    type: com.alibaba.druid.pool.DruidDataSource
#   資料來源其他配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
#   配置監控統計攔截的filters,去掉後監控介面sql無法統計,'wall'用於防火牆
    filters: stat,wall
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#    schema:執行對應路徑的sql檔案
#        - classpath:sql/1.sql
#        - classpath:sql/2.sql

 

package feizhou.mapper;

import feizhou.bean.Department;
import org.apache.ibatis.annotations.*;
@Mapper//指定這是一個操作資料庫的mapper,或者這裡不配mapper,需要主入口配置mapper的掃描包
//@MapperScan(value = "feizhou.mapper"),好處就是不需要每個mapper介面都要配置@Mapper
public interface DepartmentMapper {

    @Select("select * from department where DepartmentID=#{id}")
    public Department getDeptById(String id);
}
@RestController
public class DeptController {

    @Autowired
    DepartmentMapper departmentMapper;

    @GetMapping("/dept/{id}")
    public Department getDepartment(@PathVariable("id") String id){

        return departmentMapper.getDeptById(id);
    }

 

配置檔案方式

spring:
  datasource:
#   資料來源基本配置
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/money?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    type: com.alibaba.druid.pool.DruidDataSource
#   資料來源其他配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
#   配置監控統計攔截的filters,去掉後監控介面sql無法統計,'wall'用於防火牆
    filters: stat,wall
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#    schema:執行對應路徑的sql檔案
#        - classpath:sql/1.sql
#        - classpath:sql/2.sql
#和註解方式的區別就是多了這個配置
mybatis:
  # 指定全域性配置檔案位置
  config-location: classpath:mybatis/mybatis-config.xml
  # 指定sql對映檔案位置
  mapper-locations: classpath:mybatis/mapper/*.xml

UserMapper.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="feizhou.mapper.UserMapper">
    <select id="getUserById" resultType="feizhou.bean.User">
         SELECT * FROM userinfo WHERE UserID=#{id}
    </select>

</mapper>

mybatis-config.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>


    <settings>
        <!--設定駝峰-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

UserMapper

import feizhou.bean.User;
import org.apache.ibatis.annotations.Mapper;

//@Mapper或者@MapperScan將介面掃描裝配到容器中
@Mapper
public interface UserMapper {
    public User  getUserById(Integer id);
}

 

@RestController
public class DeptController {

//    @Autowired
//    DepartmentMapper departmentMapper;
//
//
//
//
//
//    @GetMapping("/dept/{id}")
//    public Department getDepartment(@PathVariable("id") String id){
//
//        return departmentMapper.getDeptById(id);
//    }

    @Autowired
    UserMapper userMapper;

    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Integer id){
        return userMapper.getUserById(id);
    }


}

 

測試結果