mybatis插件--(1)--mybatis generator自定义插件或者扩展报Cannot instantiate object of type XXX

1、错误说明

mybatis generator确实好用,但是离我们的生产代码还是有差别的
比如缺少,toString hashCode equals等方法,或者自定一的一些注释,作者信息等。
官方文档也说可以自定义插件,但是当我们满心欢喜的继承或者实现了mybatis-generator的类或者接口的时候,已运行,报错了,并且我们有默认的空构造。报错如下:

Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate (default-cli) on project damon: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate failed: Cannot instantiate object of type cn.edu.zua.damon.util.MyDefaultCommentGenerator -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate (default-cli) on project damon: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate failed: Cannot instantiate object of type cn.edu.zua.damon.util.MyDefaultCommentGenerator
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    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:309)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    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 default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate failed: Cannot instantiate object of type cn.edu.zua.damon.util.MyDefaultCommentGenerator
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:145)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    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:309)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)

经过耐心的搜索和寻找,大致确定了错误。
错误就是,mybatis-generator 的plugin有自己的classpath,我们在项目中直接继承的类和plugin不属于同一个classpath。这一点在官方文档有一点提到,
mybatis插件--(1)--mybatis generator自定义插件或者扩展报Cannot instantiate object of type XXX_第1张图片
但是官方文档并没有明确说明,当我们自定义的插件该放到哪里。

到这里,问题就解决了。
我们需要吧自己写的插件封装成一个jar,然后在plugin中添加依赖,或者安装到本地,或者项目仓库。

2、解决方案

方案一

在plugin的pom中添加jar依赖

<dependency>
     <groupId>xxxgroupId>
     <artifactId>xxxartifactId>
     <version>1.0version>
     <scope>systemscope>
     <systemPath>F:/xxx/lib/xxx.jarsystemPath>
dependency>

方案二

使用:https://blog.csdn.net/u_ascend/article/details/80485931 中的命令安装jar到本地仓库,然后再在plugin的pom中添加jar依赖

<dependency>
    <groupId>xxxgroupId>
    <artifactId>xxxartifactId>
    <version>1.0version>
dependency>

方案三

和方案二类似,这种方案的区别是仓库位于项目下。安装命令也是使用 https://blog.csdn.net/u_ascend/article/details/80485931 中的命令安装jar到项目仓库,然后再在plugin的pom中添加jar依赖。

<repositories>
    <repository>
        <id>in-project-repoid>
        <releases>
            <checksumPolicy>ignorechecksumPolicy>
        releases>
        <url>file://${project.basedir}/repourl>
    repository>
repositories>
...
<build>
...
...
<dependency>
    <groupId>xxxgroupId>
    <artifactId>xxxartifactId>
    <version>1.0version>
dependency>

希望对你有帮助

看这里,看这里
文章总目录:博客导航
码字不易,尊重原创,转载请注明:https://blog.csdn.net/u_ascend/article/details/80485958

你可能感兴趣的:(mybatis)