idea的maven项目使用mybatis generator逆向生成报错:generate failed: Exception getting JDBC Driver

问题描述:

1.报错:generate failed: Exception getting JDBC Driver
Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.4.0:generate (default-cli) on project TeachingEvaluationSystem: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.4.0:generate failed: Exception getting JDBC Driver

在这里插入图片描述

2.Git提示:jdbc.properties 存在unused property

在这里插入图片描述

3.初步猜测:
  • 原本以为是版本过高(因为用的全是最新版本,基本上是今年10、11月更新的),发现报错是找不到JDBC驱动器;
  • mysql-connector-java 8.0太高级报错?
  • 但是Maven项目里面已经有mysql-connector-java-5.1.42.jar,后面猜测是识别不了驱动器?

文件说明

1.jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/evaluation_system?useUnicode=true&autoReconnect=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
jdbc.username=root
jdbc.password=123456

2.mybatis-generator.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- refer to: http://mybatis.org/generator/quickstart.html -->
<generatorConfiguration>
    <!--引入数据库配置文件以解耦-->
    <properties resource="jdbc.properties"/>

    <context id="Mysql" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
            <!-- 是否去除所有自动生成的文件的时间戳,默认为false -->
            <!-- <property name="suppressDate" value="false"/> -->
        </commentGenerator>

        <!-- 一、配置数据库连接 -->
        <jdbcConnection driverClass="${jdbc.driverClassName}"
                        connectionURL="${jdbc.url}"
                        userId="${jdbc.username}"
                        password="${jdbc.password}">
        </jdbcConnection>
         <!--<jdbcConnection driverClass="com.mysql.jdbc.Driver"
                         connectionURL="jdbc:mysql://localhost:3306/evaluation_system"
                         userId="root"
                         password="123456">
         </jdbcConnection>-->
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
             NUMERIC 类型解析为java.math.BigDecimal -->
<!--        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>-->
        
        <!-- 二、配置生成的model和mapper等名称和路径 -->
        <!--(1)Generate Entity Classes(java bean)-->
        <!-- targetPackage:包名称(自定义)  targetProject:项目路径(自定义)   -->
        <javaModelGenerator targetPackage="tqes.model"
                            targetProject="src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true" />
            <!-- 从数据库返回的值被清理前后的空格  -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- (2)Generate mapper corresponding XML file, optionally generate -->
        <!--指定sql映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- (3)Generate mapper -->
        <!-- 指定dao接口生成的位置,mapper接口 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="tqes.dao"
                             targetProject="src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

		<!--三、配置生成表策略-->
        <!-- table指定每个表的生成策略 -->
        <table tableName="%"/>
        <!--<table tableName="t_imagepath" domainObjectName="ImagePath"></table>-->
        <!--<table tableName="t_wx_msg_out" domainObjectName="WxMsgOut"></table>-->
        <!--<table tableName="t_user" domainObjectName="User"></table>-->
        <!--<table tableName="t_version" domainObjectName="Version"></table>-->
        <!--<table tableName="" domainObjectName=""></table>-->
    </context>
</generatorConfiguration>

3.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>cn.edu.scau.cmi.liye</groupId>
  <artifactId>TeachingEvaluationSystem</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>TeachingEvaluationSystem Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <!--依赖-->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--mybatis-->
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.3</version>
    </dependency>

    <!--mybatis整合spring适配包-->
    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator -->
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator</artifactId>
      <version>1.4.0</version>
      <type>pom</type>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.3</version>
    </dependency>

    <!--mybatis逆向工程-->
    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.4.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.42</version>
    </dependency>
  </dependencies>


  <!--插件-->
  <build>
    <finalName>TeachingEvaluationSystem</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.1.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.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</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>

    <!--mybatis逆向工程插件-->
    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-maven-plugin -->
    <plugins>
      <plugin>
        <!--http://mybatis.org/generator/running/runningWithMaven.html-->
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.4.0</version>
        <configuration>
          <configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
          <overwrite>true</overwrite>
          <verbose>true</verbose>
        </configuration>
        <executions>
          <execution>
            <id>generate-mappers</id>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

mybatis相关依赖记录

artifactidlast versionlast date
mybatis3.5.32019.10
mybatis-generator1.3.72019.11
mybatis-spring2.0.32019.10
mybatis-generator-core1.4.02019.11
mysql-connector-java8.0.182019.09
  • 依赖查询官网:https://mvnrepository.com/
  • 关于mysql-connertor-java,就如前面初步猜测所说,以为太高级了,就换成了之前使用的版本5.1.42
    在这里插入图片描述
    发现之前用的都是蓝色框的,后来发现自己选了最上面那个带J的,想问一些有什么区别?

解决

1.在mybatis-generator.xml文件中设置路径,确保能找到jdbc驱动器

<classPathEntry location="C:\Users\admin\.m2\repository\mysql\mysql-connector-java\5.1.42\mysql-connector-java-5.1.42.jar"/>

2.或者在jdbc.properties文件中指定,再在xml中引用

jdbc.driverLocation=C:/Users/admin/.m2/repository/mysql/mysql-connector-java/5.1.42/mysql-connector-java-5.1.42.jar
<!--mybatis-generator.xml-->
<classPathEntry location="${jdbc.driverLocation}"/>
成功
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------< cn.edu.scau.cmi.liye:TeachingEvaluationSystem >------------
[INFO] Building TeachingEvaluationSystem Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.4.0:generate (default-cli) @ TeachingEvaluationSystem ---
[INFO] Connecting to the Database
[INFO] Introspecting table %
[INFO] Generating Example class for table teacher
[INFO] Generating Record class for table teacher
[INFO] Generating Mapper Interface for table teacher
[INFO] Generating SQL Map for table teacher
……
[INFO] Saving file User.java
[INFO] Saving file UserMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.714 s
[INFO] Finished at: 2019-12-16T08:42:35+08:00
[INFO] ------------------------------------------------------------------------

其他说明

  • 使用mysql-connector-java-8.0.18.jar报错
[INFO] Connecting to the Database
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值