1.maven依赖
<?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> <groupId>com.xwolf</groupId>
<artifactId>boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>boot</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<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>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.27</version>
</dependency> <!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.22</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.2.1</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
</plugins>
</build> </project>
2.application.yml
spring:
application:
name: boot http:
encoding:
force: true
charset: UTF-8
enabled: true
#指定项目的运行环境,可自动启用或停止某些功能
profiles:
active: dev datasource:
druid:
url: jdbc:mysql://127.0.0.1:3306/wifi2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&createDatabaseIfNotExist=true
username: eap
password: eap@eap
filters: stat,slf4j
initialSize: 10
maxActive: 100
maxWait: 60000
minIdle: 5
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
logging:
config: classpath:logback.xml
# file: d:/logs/boot.log server:
port: 8080
session:
timeout: 30 mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.xwolf.boot.entity
check-config-location: true
config-location: classpath:mybatis.xml
Druid数据源配置并添加sql监控:
package com.xwolf.boot.config; import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.google.common.collect.Lists;
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.Profile;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager; import java.util.HashMap;
import java.util.Map; /**
* <p>
* </p>
*
* @author xwolf
* @date 2017-02-25 08:14
* @since V1.0.0
*/
@Configuration
public class DataSourceConfig { @ConfigurationProperties(prefix = "datasource.druid")
@Bean(name="datasource",initMethod = "init",destroyMethod = "close")
public DruidDataSource getDataSource(){
return new DruidDataSource();
} @Bean
public PlatformTransactionManager transactionManager(){
return new DataSourceTransactionManager(getDataSource());
} // 指定环境下dev 开启监控
@Bean
@Profile("dev")
public FilterRegistrationBean druidFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new WebStatFilter());
Map<String, String> intParams = new HashMap<>();
intParams.put("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
registration.setName("DruidWebStatFilter");
registration.setUrlPatterns(Lists.newArrayList("/*"));
registration.setInitParameters(intParams);
return registration;
} //数据源监控
@Bean
@Profile("dev")
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean();
registration.setServlet(new StatViewServlet());
registration.setName("druid");
registration.setUrlMappings(Lists.newArrayList("/druid/*"));
//自定义添加初始化参数
Map<String, String> intParams = new HashMap<>();
intParams.put("loginUsername","druid");
intParams.put("loginPassword","druid");
registration.setName("DruidWebStatFilter");
registration.setInitParameters(intParams);
return registration;
}
}
启动
package com.xwolf; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("com.xwolf.boot.dao")
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
}
}
其他Controller,service,dao ,mapper 配置和普通spring项目一样,此处省略。