# com.alibaba.druid使用踩坑解决
程序员文章站
2022-07-05 18:43:21
...
com.alibaba.druid使用踩坑解决
1.加入依赖
<dependency>
<!--自动配置-->
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
报错如下:少了jar包Springframework spring-jdbc依赖
添加如下依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
2.在启动类添加Servlert扫描注解
@ServletComponentScan
@SpringBootApplication
public class BigdatacarApplication {
}
3.druid配置
@Bean
public ActiveRecordPlugin initActiveRecordPlugin() {
try {
Prop prop = PropKit.use("db.config");
String url = prop.get("dburl");
String username = prop.get("dbusername");
String password = prop.get("dbpassword");
DruidPlugin druidPlugin = new DruidPlugin(url, username, password);
//添加startFilter才会有数据
druidPlugin.addFilter(new StatFilter());
//加强数据库安全
WallFilter wallFilter = new WallFilter();
wallFilter.setDbType("mysql");
druidPlugin.addFilter(wallFilter);
druidPlugin.start();
if (arp == null) {
arp = new ActiveRecordPlugin(Multidb.dbmysql, druidPlugin);
arp.setShowSql(true);
//配置缓存
arp.setCache(new IcacherForJfinal());
_MappingKit.mapping(arp);
arp.setDevMode(true);
// 设置sql模板位置
engine = arp.getEngine();
engine.setBaseTemplatePath("/sql");
arp.start();
log.info("Mysql数据源初始化完成");
return arp;
}else{
log.info("Mysql数据源已经存在!");
return null;
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
return null;
}
}
4.访问项目地址http://localhost:8001/druid
上一篇: java.util.concurrent
下一篇: 深入理解angularjs过滤器