欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

# 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依赖
# com.alibaba.druid使用踩坑解决
添加如下依赖:

<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
# com.alibaba.druid使用踩坑解决

相关标签: 后端开发