SpringBoot框架整合⼀、主要思路
1. 使⽤spring-boot-starter-jdbc集成Mybatis框架
2. 通过spring-boot-starter-cache集成EhCache
3. 使⽤WebServlet集成Druid监控
⼆、程序实现
1. POM ⽂件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>
<groupId&k</groupId>
<artifactId>SpringBootInit</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<mybatis.version>3.4.5</mybatis.version>
<mybatis.spring.version>1.3.1</mybatis.spring.version>
<druid.version>1.1.6</druid.version>
<ehcache.version>3.4.0</ehcache.version>
&tor.version>5.1.45</tor.version>
</properties>
<dependencies>
<!-- SpringBoot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${tor.version}</version>
</dependency>
<!-- MySQL Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- EhCache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${ehcache.version}</version>
</dependency>
</dependencies>
springboot框架是干嘛的
</project>
2. 集成Mybatis
mybatis:
mapper-locations: classpath:mappers/*.xml
type-aliases-package: k.*.dto
3. 集成EhCache
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="/ehcache.xsd"
updateCheck="false">
<defaultCache
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU"/>
<cache
name="users"
eternal="false"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
java中使⽤注解
@Override
@Cacheable(value="users",key="'user_'+#id")
public User getUserById(Long id) {
return userMapper.selectByPrimaryKey(id);
}
@Override
@CacheEvict(value="users",allEntries=true)
public int saveUser(User user) {
return userMapper.insertSelective(user);
}
4. durid 使⽤
WebServlet 配置
@WebServlet(urlPatterns = "/druid/*",
initParams={
@WebInitParam(name="allow",value=""),// IP⽩名单 (没有配置或者为空,则允许所有访问, 逗号分隔)                @WebInitParam(name="deny",value=""),// IP⿊名单 (存在共同时,deny优先于allow)
@WebInitParam(name="loginUsername",value="admin"),// ⽤户名
@WebInitParam(name="loginPassword",value="admin"),// 密码
@WebInitParam(name="resetEnable",value="false")// 禁⽤HTML页⾯上的“Reset All”功能
})
public class DruidController extends StatViewServlet {
}
Filter 配置
@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
initParams={
@WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源
})
public class DruidStatFilter extends WebStatFilter {
}
三、效果图如下:
增加数据
第⼀次执⾏
第⼆次执⾏
四、程序⽬录结构
五、其他补充
注:本⽂著作权归作者,由demo⼤师代发,拒绝转载,转载需要作者授权

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。