maven插件自动生成mybatis代码时无法找到jdbc.Driver

发布于 2022-09-02 11:42:08 字数 10346 浏览 20 评论 0

1、目的:我在开发环境中想引用相对路径的数据库驱动jar包,以便在部署环境不必修改数据库驱动的位置。(用maven管理jar包,而不用在开发环境一个配置,部署环境也有另一个配置)
2、问题:不知道怎么引用相对路径的jar包?
3、这是pom.xml中mybatis-generator-maven-plugin插件的配置

 <plugin>  
         <!-- 
         用maven mybatis插件
         如果不在plugin里面添加依赖包得引用的话,会找不到相关得jar包,
         在plugin外部得jar包,他不会去找到并执行,
         所以要把plugin运行依赖得jar配置都放在里面 
          -->
        <groupId>org.mybatis.generator</groupId>  
        <artifactId>mybatis-generator-maven-plugin</artifactId>  
        <version>1.3.2</version>  
        <executions>  
            <execution>  
                <id>Generate MyBatis Artifacts</id>  
                <goals>  
                    <goal>generate</goal>  
                </goals>  
            </execution>  
        </executions>  
        <configuration>  
            <verbose>true</verbose>  
            <overwrite>true</overwrite>  
           
            <jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver> 
            <jdbcURL>jdbc:mysql://115.24.32.213:3306/test</jdbcURL> 
            <jdbcUserId>root</jdbcUserId> 
            <jdbcPassword>****</jdbcPassword>  
            
        </configuration>  
        <dependencies>  
            <dependency>  
                <groupId>mysql</groupId>  
                <artifactId>mysql-connector-java</artifactId>  
                 <!-- 引用依赖库的版本 -->
                <version>5.1.35</version>  
            </dependency>  
            <dependency>  
                <groupId>org.mybatis.generator</groupId>  
                <artifactId>mybatis-generator-core</artifactId>  
                <version>1.3.2</version>  
            </dependency>  
            <dependency>  
                <groupId>org.mybatis</groupId>  
                <artifactId>mybatis</artifactId>  
                <!-- 引用依赖库的版本 -->
                <version>${mybatis.version}</version>  
            </dependency>  
        </dependencies>  
</plugin>  
 

4、这是generatorConfig.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" >  
<generatorConfiguration>  
    <!-- 引入配置文件 -->  
    <properties resource="jdbc.properties"/>  
    
    

    <!-- 一个数据库一个context -->  
    <context id="infoGuardian">  
        <!-- 注释 -->  
        <commentGenerator >  
            <property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->  
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->  
        </commentGenerator>  
          
        <!-- jdbc连接 -->  
        <jdbcConnection driverClass="${driver}"  
            connectionURL="${url}" userId="${user}"  
            password="${password}" />  
          
        <!-- 类型转换 -->  
        <javaTypeResolver>  
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
          
        <!-- 生成实体类地址 -->    
        <javaModelGenerator targetPackage="org.luojj.model"  
            targetProject="src" >  
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->  
            <property name="enableSubPackages" value="false"/>  
            <!-- 是否针对string类型的字段在set的时候进行trim调用 -->  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
          
        <!-- 生成mapxml文件 -->  
        <sqlMapGenerator targetPackage="org.luojj.mapping"  
            targetProject="src" >  
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->  
            <property name="enableSubPackages" value="false" />  
        </sqlMapGenerator>  
          
        <!-- 生成mapxml对应client,也就是接口dao -->      
        <javaClientGenerator targetPackage="org.luojj.dao"  
            targetProject="src" type="XMLMAPPER" >  
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->  
            <property name="enableSubPackages" value="false" />  
        </javaClientGenerator>  
          
        <!-- 配置表信息 -->      
        <table  tableName="students"  
            domainObjectName="Student" enableCountByExample="false"  
            enableDeleteByExample="false" enableSelectByExample="false"  
            enableUpdateByExample="false">  
            <!--  tableName为对应的数据库表 domainObjectName是要生成的实体类名 enable*ByExample   
                是否生成 example类   -->  
              
            <!-- 忽略列,不生成bean 字段 -->  
            <ignoreColumn column="FRED" />  
            <!-- 指定列的java数据类型 -->  
            <!--
            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> 
             -->
        </table>  
  
    </context>  
</generatorConfiguration> 


5、这是错误提示

[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (Generate MyBatis Artifacts) on project test333: Execution Generate MyBatis Artifacts of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Exception getting JDBC Driver: com.mysql.jdbc.Driver -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (Generate MyBatis Artifacts) on project test333: Execution Generate MyBatis Artifacts of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Exception getting JDBC Driver
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution Generate MyBatis Artifacts of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Exception getting JDBC Driver
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:145)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    ... 21 more
Caused by: java.lang.RuntimeException: Exception getting JDBC Driver
    at org.mybatis.generator.internal.db.ConnectionFactory.getDriver(ConnectionFactory.java:85)
    at org.mybatis.generator.internal.db.ConnectionFactory.getConnection(ConnectionFactory.java:54)
    at org.mybatis.generator.config.Context.getConnection(Context.java:526)
    at org.mybatis.generator.config.Context.introspectTables(Context.java:436)
    at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:222)
    at org.mybatis.generator.maven.MyBatisGeneratorMojo.execute(MyBatisGeneratorMojo.java:203)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    ... 22 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver  
    at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:270)
    at org.mybatis.generator.internal.ObjectFactory.internalClassForName(ObjectFactory.java:150)
    at org.mybatis.generator.internal.ObjectFactory.externalClassForName(ObjectFactory.java:121)
    at org.mybatis.generator.internal.db.ConnectionFactory.getDriver(ConnectionFactory.java:82)
    ... 28 more
[ERROR] 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谷夏 2022-09-09 11:42:08

自己找到问题。jdbc.propertis的 driver=com.mysql.jdbc.Driver的后面多一个空格

情归归情 2022-09-09 11:42:08

真是救命了 原来 我也是多了空格

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文