SpringBoot+Mybatis+Spring缓存抽象+Druid数据源的整合
SpringBoot对这几种的整合非常简单,无非就是pom.xml文件添加几个依赖罢了,稍微有点复杂的就是druid数据源的配置。有了Druid之后可以非常方便的对数据源进行监控和分析。
1、项目环境的搭建
第一步
用IDEA快速搭建出项目,勾选这几个组件(当然,不勾选也行,之后再pom.xml引入依赖即可),其它配置根据自己喜好或者一路next也可。
第二步
对MyBatis和Druid数据源进行配置
application.yml
<?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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<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>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.18</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第三步
配置druid的信息,使上面我们配置的数据源各种属性能加载上去。新建druidconfig类,配置信息
DruidConfig.class
package com.example.demo2.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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 javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DruidConfig {
//加载druid的配置
@ConfigurationProperties(prefix ="spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
//配置druid监控
//1、配置一个管理后台的servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String,String> initParams = new HashMap<>();
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
servletRegistrationBean.setInitParameters(initParams);
return servletRegistrationBean;
}
//2、配置一个web监控的filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String,String> initParams = new HashMap<>();
initParams.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
第四步
配置mybatis的全部设置和局部的映射
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>
</configuration>
UserMapper.xml(这就是正常的sql代码)
<?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="com.example.demo2.mapper.UserMapper">
<select id="getUserById" resultType="com.example.demo2.bean.User">
select * from user where uid = #{uid}
</select>
</mapper>
这里的mapper扫描,我试过不加也可以正常使用
环境已经配置好了,测试下进入druid后台监控看看
地址栏输入
输入刚才设置的账号和密码
进入后台监控界面
2、Spring缓存抽象的使用
环境已经搭建好了,剩下的正常写controller service mapper的代码即可。
这里再配置下spring缓存,缓存的配置也非常简单,只需几个注解即可
好了,大功告成,开始测试吧
3、测试
为了方面看,把debug日志打开
controller代码
访问了两次根据用户的id查询(查询同一个id),这里可以很明显的看出,使用了缓存的优势
可以看下监控,所有的数据访问都已经记录了下来
4、结束
SpringBoot+Mybatis+Spring缓存抽象+Druid数据源的整合已经结束了,这个写的非常简单,可能只是刚刚入门。但是学无止境嘛~ 一步一个脚印~ 最后来个妹子养养眼,睡觉咯~
2019-7-10 23:28:58