# com.alibaba.druid使用踩坑解决

com.alibaba.druid使用踩坑解决


1.加入依赖

<dependency>
    
    <groupId>com.alibabagroupId>
    <artifactId>druid-spring-boot-starterartifactId>
    <version>1.1.10version>
dependency>

报错如下:少了jar包Springframework spring-jdbc依赖
# com.alibaba.druid使用踩坑解决_第1张图片
添加如下依赖:

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
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
# com.alibaba.druid使用踩坑解决_第2张图片

你可能感兴趣的:(后端开发)