Making: github.com/imyiren/spr…

  • Note: less explanation, just read the notes,

pom.xml

<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4. The RELEASE</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>io.ilss</groupId>
    <artifactId>spring-boot-mybatis-druid</artifactId>
    <version>1.0</version>
    <name>spring-boot-mybatis-druid</name>
    <description>spring-boot-mybatis-druid</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Copy the code

application.yml

spring:
  application:
    name: spring-boot-mybatis-druid
  datasource:
    druid:
      url: jdbc:mysql://localhost:3306/test? useSSL=true&characterEncoding=UTF-8
      username: root
      password: feng1104
      # configure the initial size (default 0), minimum, maximum (default 8)
      initial-size: 1
      min-idle: 1
      max-active: 20
      Set the connection wait timeout
      max-wait: 60000
      PreparedStatement (PSCache) PSCache greatly improves the performance of databases that support cursors. The default is false
      pool-prepared-statements: true
      To enable PSCache, the configuration must be greater than 0. When greater than 0, poolPreparedStatements automatically triggers a change to true.
      max-open-prepared-statements: 20
      Configure how often to detect idle connections that need to be closed, in milliseconds
      time-between-eviction-runs-millis: 60000
      Set the minimum and maximum lifetime of a connection in the pool in milliseconds
      min-evictable-idle-time-millis: 300000
      max-evictable-idle-time-millis: 900000

      Select * from 'x'; select * from 'x';
      TestOnBorrow, testOnReturn, and testWhileIdle will not work if validationQuery is null.
      validation-query: SELECT 1
      # Execute validationQuery when applying for a connection to check whether the connection is valid
      test-on-borrow: true
      # Execute validationQuery when returning a connection to check whether the connection is valid. Default is false
      test-on-return: false
      # application connection testing, if free time is more than timeBetweenEvictionRunsMillis, performing validationQuery test connection is valid.
      test-while-idle: true
mybatis:
  mapper-locations: classpath:mapper/*.xml

Copy the code

Database class: User

package io.ilss.dataobject;

import lombok.Data;
import org.apache.ibatis.type.Alias;

/ * * *@author : feng
 * @description: User
 * @date: the 2019-05-13 11:44 *@version: : 1.0 * /
@Data
public class User {
    private Integer id;

    private String username;

    private String password;
}
Copy the code

DAO layer: Userdao.java and Usermapper.xml

  • UserDAO.java
package io.ilss.dao;

import io.ilss.dataobject.User;
import org.springframework.stereotype.Repository;

import java.util.List;

/ * * *@author : feng
 * @description: UserDAO
 * @date: the 2019-05-13 11:46 *@version: : 1.0 * /
@Repository
public interface UserDAO {

    List<User> listAll(a);
}

Copy the code
  • UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ? >

      
<mapper namespace="io.ilss.dao.UserDAO">
    <select id="listAll" resultType="io.ilss.dataobject.User">
    select * from user
  </select>
</mapper>
Copy the code

The test interface

package io.ilss.controller;

import com.alibaba.druid.stat.DruidStatManagerFacade;
import io.ilss.dao.UserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/ * * *@author : feng
 * @description: DruidStatController
 * @date: the 2019-05-13 11:42 *@version: : 1.0 * /
@RestController
public class TestController {

    @Autowired
    UserDAO userDAO;
	
    @GetMapping("/druid/stat")
    public Object druidStat(a){
        return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
    }

    @GetMapping("/list/user")
    public Object listUser(a) {
        returnuserDAO.listAll(); }}Copy the code

Notice:

  • There are integration examples on the web that have @bean and then create Datasource, test, using druid-spring-boot-starter dependencies that don’t need to be created
  • Druid is optional for datasource configuration. It’s just one more layer. Druid supports both. (Official)
  • accesslocalhost:8080/druid/statYou can enter the Druid Monitor as follows:

About me

  • Majoring in Computer Science and technology, general university, Hangzhou.
  • Graduated in 20 years, mainly engaged in back-end development of Java technology stack.
  • GitHub: github.com/imyiren
  • Blog : imyi.ren