PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2018-12-30 11:19:09  修改时间:2024-12-25 01:30:21  分类:07.Java基础  编辑

问题描述

用 IDEA 搭建 JFinal Demo 项目,使用 druid 访问数据库,相应的 maven 依赖配置,JFinal 插件都配置了,但IDEA启动调试的时候报错:

[com.alibaba.druid.pool.DruidDataSource] - {dataSource-1} init error

 1、pom.xml 依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>jianbao.blog</groupId>
    <artifactId>jianbao-blog</artifactId>
    <version>1.0.0</version>

    <name>jianbao-blog</name>
    <url></url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <jfinal.version>3.5</jfinal.version>
    </properties>

    <!-- 使用阿里 maven 库 -->
    <repositories>
        <repository>
            <id>ali-maven</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
        <!-- JFinal Web 框架 -->
        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
        <dependency>
            <groupId>com.jfinal</groupId>
            <artifactId>jfinal</artifactId>
            <version>${jfinal.version}</version>
        </dependency>

        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
        <!-- 数据库连接池 druid(依赖下面的 MySQL JDBC 驱动)-->
        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>

        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
        <!-- MySQL JDBC 驱动 -->
        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>jianbao-blog</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>

                <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
                <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
                <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->

            </plugins>
        </pluginManagement>
    </build>
</project>

2、config 配置

package blog.jianbao.config;

import blog.jianbao.model._MappingKit;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;

public class AppConfig extends JFinalConfig {
    @Override
    public void configConstant(Constants me) {
        // 加载少量必要配置,随后可用 Prokit.get(...)获取值
        PropKit.use("config.properties");

        me.setDevMode(PropKit.getBoolean("devMode", false));
    }

    @Override
    public void configRoute(Routes me) {
        me.add(new RoutesConfig());
    }

    /**
     * 设置 模板引擎
     */
    @Override
    public void configEngine(Engine me) {
        // devMode 配置为 true,将支持模板实时热加载
        me.setDevMode(true);
    }

    /**
     * 配置 插件
     */
    @Override
    public void configPlugin(Plugins me) {
        // 配置 druid 数据库连接池插件
        DruidPlugin druidPlugin = createDruidPlugin();

        // 配置 druid 监控
        druidPlugin.addFilter(new StatFilter()); // 监控  /druid/index.html
        WallFilter wall = new WallFilter();      // 防SQL注入
        wall.setDbType("mysql");
        druidPlugin.addFilter(wall);
        druidPlugin.setMaxActive(1200);
        druidPlugin.setTestOnBorrow(true);
        druidPlugin.setTestOnReturn(true);
        me.add(druidPlugin);
        
        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setShowSql(true);
        _MappingKit.mapping(arp);

        me.add(arp);
    }

    public static DruidPlugin createDruidPlugin() {
        //db_jdbcUrl=jdbc:mysql://localhost:3306/jianbao-blog?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
        //db_user = root
        //db_password = 123456
        return new DruidPlugin(PropKit.get("db_jdbcUrl"), PropKit.get("db_user"), PropKit.get("db_password").trim());
    }

    /**
     * 配置 全局拦截器
     */
    @Override
    public void configInterceptor(Interceptors me) {
    }

    /**
     * 配置 处理器
     */
    @Override
    public void configHandler(Handlers me) {
    }
}

问题解决

主要是  IDEA 调试时,没有配置好 maven依赖 打包,如下图所示: