druid在spring boot中的使用
来源:互联网 发布:在线室内设计软件 编辑:程序博客网 时间:2024/06/14 13:57
前面已经写过一篇druid的初识http://blog.csdn.net/u013218587/article/details/70212422,现在再来整理一下druid在spring boot中的使用,spring本身不支持druid数据源,需要自己定义,下面是定义过程,准备工作去掉之前的datasource(c3p0,dbcp,dbcp2等),如果你想配置多数据源,除外,
关于如何配置多数据源,自行百度。
为了更清晰我把druid相关放在一个包下面,有三个类
package com.pillar.controller.druid;import com.alibaba.druid.pool.DruidDataSource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;import java.sql.SQLException;@Configuration@MapperScan(basePackages = ClusterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "pillarSqlSessionFactory")public class ClusterDataSourceConfig { static final String PACKAGE = "com.pillar.core"; private static final String MAPPER_LOCATION = "classpath:sqlmap/**/*.xml"; private static final String MYBATIS_CONFIG = "classpath:mybatis-config.xml"; @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String user; @Value("${spring.datasource.password}") private String password; @Value("${spring.datasource.driverClassName}") private String driverClass; @Value("${spring.datasource.initialSize}") private int initialSize; @Value("${spring.datasource.minIdle}") private int minIdle; @Value("${spring.datasource.maxActive}") private int maxActive; @Value("${spring.datasource.filters}") private String filters; @Bean(name = "pillarDataSource") @Primary public DataSource pillarDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); dataSource.setMaxActive(maxActive); dataSource.setMinIdle(minIdle); dataSource.setInitialSize(initialSize); try { dataSource.setFilters(filters); } catch (SQLException e) { e.printStackTrace(); } dataSource.setConnectionProperties("config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKbkwDlvul62rcbK0gMZQrOXhXzH5G+3g8rFJMCA1IsXtYZOdJSG1f56smZPmfFulIFxxqh20BLa1VBU9k+7fkECAwEAAQ=="); return dataSource; } @Bean(name = "pillarTransactionManager") @Primary public DataSourceTransactionManager pillarTransactionManager() { return new DataSourceTransactionManager(pillarDataSource()); } @Bean(name = "pillarSqlSessionFactory") @Primary public SqlSessionFactory pillarSqlSessionFactory(@Qualifier("pillarDataSource") DataSource pillarDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(pillarDataSource); sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(ClusterDataSourceConfig.MYBATIS_CONFIG)); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(ClusterDataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); }}
package com.pillar.controller.druid;import com.alibaba.druid.support.http.WebStatFilter;import javax.servlet.annotation.WebFilter;import javax.servlet.annotation.WebInitParam;@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*", initParams={ @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")//忽略资源 } ) public class DruidStatFilter extends WebStatFilter { }
分别是定义druid,过滤静态文件(让druid/index.html可以访问),设置监控页面权限package com.pillar.controller.druid;import com.alibaba.druid.support.http.StatViewServlet;import javax.servlet.annotation.WebInitParam;import javax.servlet.annotation.WebServlet;@WebServlet(urlPatterns="/druid/*", initParams={ @WebInitParam(name="allow",value=""),// IP白名单(没有配置或者为空,则允许所有访问) @WebInitParam(name="deny",value="192.168.1.73"),// IP黑名单 (存在共同时,deny优先于allow) @WebInitParam(name="loginUsername",value="admin"),// 用户名 @WebInitParam(name="loginPassword",value="123456"),// 密码 @WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能 }) public class DruidStatViewServlet extends StatViewServlet { private static final long serialVersionUID = -2688872071445249539L; }
最后在启动入口加入
@ServletComponentScan
/** * Created by bin on 2017/3/27. */@SpringBootApplication@EnableScheduling@ServletComponentScan//druid页面访问必须加这个public class SpringBootTopApplication { public static void main(String[] args) { SpringApplication.run(SpringBootTopApplication.class,args); }}
启动项目。浏览器输入
http://localhost:9090/druid/login.html,即可进入登录页面,输入上面设置的用户名密码即可
线上测试http://www.chengxuyuantoutiao.com/ 打开网页进入swagger-api下面的"程序猿"里面操作api,"系统监控"里面查看监控信息
1 0
- druid在spring boot中的使用
- 7. Spring Boot 使用Druid
- 使用spring boot搭建druid
- Spring Boot 使用 Druid 和监控配置
- Spring Boot使用Druid和监控配置
- spring boot(6) 使用数据库连接池druid
- Spring Boot 使用 Druid 和监控配置
- spring boot + JdbcTemplate 使用druid数据源
- Spring Boot 使用 Druid 和监控配置
- Spring Boot 使用 Druid 和监控配置
- Spring Boot 使用 Druid 和监控配置
- Spring Boot 使用 Druid 和监控配置
- Spring Boot [使用 Druid 数据库连接池]
- spring-boot集成mybatis使用Druid监控
- Spring Boot 使用 Druid 和监控配置
- spring boot使用Druid和监控配置
- Spring Boot [使用 Druid 数据库连接池]
- Spring Boot使用Druid和监控配置
- 2017 武大校赛 I: A simple math problem(矩阵快速幂)
- Linux粘滞位的设置
- 鼠标点击物体互换位置
- LeetCode 258 Add Digits(技巧 || 递归)
- IMweb训练营作业—-Todo List(vue)
- druid在spring boot中的使用
- Python时间,日期,时间戳之间转换
- linux—find指令常见用法示例
- 使用Windows自带的截图工具截取整个网页
- 【IMWeb训练营作业】todolist
- 51nod 1042
- os.path.splitext(“文件路径”)
- poj 3040Allowance_可以付多少周工资 (贪心)
- Hibernate关联映射--一对多