Spring boot + mybatis + Druid

Source: Internet
Author: User
Tags connection pooling

Because of the use of Spring boot + MyBatis project, frequent access to the interface card, the server project with a few days of the card is not even access to the situation, and our project and database are good, considering the possible database connection problems, so I intend to introduce a data pool, The introduction of data pool when looking for, compared to the current two most fire data pool, Ali's Druid and HIKARICP, than to choose Ali Druid, although spring boot default does not support Druid, but support HIKARICP, And HIKARICP performance is better, but Ali function, is to support domestic

Actual configuration:

1. First download the latest Spring boot project now, I use spring boot 1.5.4, and then add the dependency in Pom.xml

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>

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

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>

2. Add MyBatis configuration in application.yml, MySQL configuration

# MyBatis Configuration
MyBatis
Mapper-locations:classpath*:/mapper/*mapper.xml

#mysql配置

Spring
DataSource
Url:jdbc:mysql://localhost/test?usessl=false&servertimezone=utc
Username:root
Password
Driver-class-name:com.mysql.cj.jdbc.driver

So MyBatis and MySQL are configured.

3. Then we introduced Ali's Druid, this thing can not only configure and monitor the interface, Ali is still very conscience

Spring:
DataSource:
Type:com.alibaba.druid.pool.DruidDataSource
# The Supplemental settings for connection pooling are applied to all of the above data sources
# Initialize size, minimum, maximum
initialsize:1
Minidle:3
maxactive:20
# Configure the time to get the connection wait timeout
Maxwait:6 0000
# How often does the configuration interval be detected, detecting idle connections that need to be closed, in milliseconds
timebetweenevictionrunsmillis:60000
# Configure the minimum time for a connection to survive in a pool, in units of Milliseconds
minevictableidletimemillis:30000
validationquery:select ' x '
Testwhileidle:true
TestO Nborrow:false
Testonreturn:false
# opens Pscache and specifies the size of the Pscache on each connection
POOLPREPAREDSTATEMENTS:TRUE
MAXPOOLPREPAREDSTATEMENTPERCONNECTIONSIZE:20
# Configure monitoring Statistics intercept filters, remove post-monitoring interface SQL cannot be counted, ' wall ' for firewalls
filters: STAT,WALL,SLF4J
# Connectproperties property to open mergesql function; Slow SQL record
Connectionproperties:druid.stat.mergesql=tru e;druid.stat.slowsqlmillis=5000
# Merging monitoring data for multiple Druiddatasource
Useglobaldatasourcestat:true

Because spring boot does not support Druid by default, and we want to configure Ali's monitoring interface, we need to write a configuration file

Package com.xiabin.TestDruid.druid;

Import Com.alibaba.druid.pool.DruidDataSource;
Import Com.alibaba.druid.support.http.StatViewServlet;
Import Com.alibaba.druid.support.http.WebStatFilter;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.boot.context.properties.ConfigurationProperties;
Import Org.springframework.boot.web.servlet.FilterRegistrationBean;
Import Org.springframework.boot.web.servlet.ServletRegistrationBean;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.context.annotation.Primary;

Import Javax.sql.DataSource;
Import java.sql.SQLException;

/**
* Created by Xiabin on 2017/6/18.
*/
@Configuration
public class Druidconfiguration {

private static final Logger Logger = Loggerfactory.getlogger (Druidconfiguration.class);

private static final String Db_prefix = "Spring.datasource";

@Bean
Public Servletregistrationbean Druidservlet () {
Logger.info ("Init Druid Servlet Configuration");
Servletregistrationbean Servletregistrationbean = new Servletregistrationbean (new Statviewservlet (), "/druid/*");
IP White List
Servletregistrationbean.addinitparameter ("Allow", "192.168.2.25,127.0.0.1");
IP blacklist (when co-existing, deny takes precedence over allow)
Servletregistrationbean.addinitparameter ("Deny", "192.168.1.100");
Console Administration User
Servletregistrationbean.addinitparameter ("Loginusername", "admin");
Servletregistrationbean.addinitparameter ("Loginpassword", "9527");
Is it possible to reset the data disable the "Reset All" feature on the HTML page
Servletregistrationbean.addinitparameter ("Resetenable", "false");
return Servletregistrationbean;
}

@Bean
Public Filterregistrationbean Filterregistrationbean () {
Filterregistrationbean Filterregistrationbean = new Filterregistrationbean (new Webstatfilter ());
Filterregistrationbean.addurlpatterns ("/*");
Filterregistrationbean.addinitparameter ("Exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return Filterregistrationbean;
}

//Resolve spring.datasource.filters=stat,wall,log4j not register in
@ConfigurationProperties (prefix = db_prefix)
Class Idatasourceproperties {
private string url;
private string Username;
private string password;
Privat e String driverclassname;
Private int initialsize;
Private int minidle;
Private int maxactive;
Private int maxwait;
Private int timebetweenevictionrunsmillis;
Private int minevictableidletimemillis;
Private String Validationquery;
Private Boolean Testwhileidle;
Private Boolean Testonborrow;
Private Boolean Testonreturn;
Private Boolean poolpreparedstatements;
Private int maxpoolpreparedstatementperconnectionsize;
Private String filters;
Private String connectionproperties;

@Bean//Declare it as a Bean instance
@Primary//In the same DataSource, first use the labeled DataSource
Public DataSource DataSource () {
Druiddatasource DataSource = new Druiddatasource ();
Datasource.seturl (URL);
Datasource.setusername (username);
Datasource.setpassword (password);
Datasource.setdriverclassname (driverclassname);

//configuration
Datasource.setinitialsize (initialsize);
Datasource.setminidle (minidle);
Datasource.setmaxactive (maxactive);
Datasource.setmaxwait (maxwait);
Datasource.settimebetweenevictionrunsmillis (Timebetweenevictionrunsmillis);
Datasource.setminevictableidletimemillis (Minevictableidletimemillis);
Datasource.setvalidationquery (validationquery);
Datasource.settestwhileidle (Testwhileidle);
Datasource.settestonborrow (Testonborrow);
Datasource.settestonreturn (Testonreturn);
Datasource.setpoolpreparedstatements (poolpreparedstatements);
Datasource.setmaxpoolpreparedstatementperconnectionsize (maxpoolpreparedstatementperconnectionsize);
try {
Datasource.setfilters (filters);
} catch (SQLException e) {
System.err.println ("Druid Configuration initialization filter: "+ E";
}
Datasource.setconnectionproperties (connectionproperties);
return datasource;
}

Public String GetUrl () {
return URL;
}

public void SetUrl (String URL) {
This.url = URL;
}

Public String GetUserName () {
return username;
}

public void Setusername (String username) {
This.username = Username;
}

Public String GetPassword () {
return password;
}

public void SetPassword (String password) {
This.password = password;
}

Public String Getdriverclassname () {
return driverclassname;
}

public void Setdriverclassname (String driverclassname) {
This.driverclassname = Driverclassname;
}

public int getinitialsize () {
return initialsize;
}

public void setinitialsize (int initialsize) {
This.initialsize = InitialSize;
}

public int Getminidle () {
return minidle;
}

public void Setminidle (int minidle) {
This.minidle = Minidle;
}

public int getmaxactive () {
return maxactive;
}

public void setmaxactive (int maxactive) {
This.maxactive = maxactive;
}

public int getmaxwait () {
return maxwait;
}

public void setmaxwait (int maxwait) {
this.maxwait = maxwait;
}

public int Gettimebetweenevictionrunsmillis () {
return timebetweenevictionrunsmillis;
}

public void Settimebetweenevictionrunsmillis (int timebetweenevictionrunsmillis) {
This.timebetweenevictionrunsmillis = Timebetweenevictionrunsmillis;
}

public int Getminevictableidletimemillis () {
return minevictableidletimemillis;
}

public void Setminevictableidletimemillis (int minevictableidletimemillis) {
This.minevictableidletimemillis = Minevictableidletimemillis;
}

Public String Getvalidationquery () {
return validationquery;
}

public void Setvalidationquery (String validationquery) {
This.validationquery = Validationquery;
}

public Boolean istestwhileidle () {
return testwhileidle;
}

public void Settestwhileidle (Boolean testwhileidle) {
This.testwhileidle = Testwhileidle;
}

public Boolean Istestonborrow () {
return testonborrow;
}

public void Settestonborrow (Boolean testonborrow) {
This.testonborrow = Testonborrow;
}

public Boolean Istestonreturn () {
return testonreturn;
}

public void Settestonreturn (Boolean Testonreturn) {
This.testonreturn = Testonreturn;
}

public Boolean ispoolpreparedstatements () {
return poolpreparedstatements;
}

public void Setpoolpreparedstatements (Boolean poolpreparedstatements) {
This.poolpreparedstatements = poolpreparedstatements;
}

public int getmaxpoolpreparedstatementperconnectionsize () {
return maxpoolpreparedstatementperconnectionsize;
}

public void setmaxpoolpreparedstatementperconnectionsize (int maxpoolpreparedstatementperconnectionsize) {
This.maxpoolpreparedstatementperconnectionsize = maxpoolpreparedstatementperconnectionsize;
}

Public String getfilters () {
return filters;
}

public void SetFilters (String filters) {
This.filters = filters;
}

Public String getconnectionproperties () {
return connectionproperties;
}

public void Setconnectionproperties (String connectionproperties) {
This.connectionproperties = connectionproperties;
}
}

}

4. Then we launch the project, access the Localhost:8080/druid, enter admin, 9527 (username and password written in the config file), we can see our monitoring interface.

GitHub Source Address: Https://github.com/waterlufei/spring-boot.git, below the Testdruid project

Spring boot + mybatis + Druid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.