最近使用Mybatis Generator的过程中,出现了以下异常,导致生成不了实体、配置文件等,无奈去网上找,也花了不少时间但就是没有找到解决办法,,,但是前两周还是好的,我也还用过的!
异常
Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate (default-cli) on project itsm: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate failed: Mapper插件缺少必要的mappers属性!
问题分析解决
先说下这依赖配置和Generator的配置,依赖配置文件如下:
<!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!-- 通用mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.1.5</version> </dependency> <!-- pagehelper 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- mybatis generator --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <dependencies> <!--配置这个依赖主要是为了等下在配置mybatis-generator.xml的时候可以不用配置classPathEntry这样的一个属性,避免代码的耦合度太高--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.0</version> </dependency> </dependencies> <executions> <execution> <id>Generate MyBatis Artifacts</id> <phase>package</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <!--允许移动生成的文件--> <verbose>true</verbose> <!-- 是否覆盖--> <overwrite>true</overwrite> <!-- 自动生成的配置 --> <configurationFile>src/main/resources/mybatis-generator-cfg.xml</configurationFile> </configuration> </plugin> </plugins> </build>
Mybatis Generator对应的配置文件如下:
<?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="application-dev.properties" /> <!--<classPathEntry location="${jdbc.location}" />--> <!--<context id="mysql" targetRuntime="MyBatis3">--> <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <!--<property name="beginningDelimiter" value="`"/>--> <!--<property name="endingDelimiter" value="`"/>--> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="com.woxin.itsm.framework.mapper.BaseMapper"/> <!--caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true--> <property name="caseSensitive" value="true"/> </plugin> <!-- 注释 --> <commentGenerator> <property name="javaFileEncoding" value="UTF-8"/> <!-- 是否生成注释代时间戳 --> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="false"/> </commentGenerator> <!-- 数据库连接 --> <jdbcConnection driverClass="${spring.datasource.driver-class-name}" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}"> </jdbcConnection> <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制 --> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和--> <!--NUMERIC 类型解析为java.math.BigDecimal --> <!--<javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">--> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.)--> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成实体类地址 --> <javaModelGenerator targetPackage="com.woxin.itsm.model" targetProject="src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="true" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成mapper xml文件 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO层--> <!-- type="ANNOTATEDMAPPER",生成Java Model和基于注解的Mapper对象--> <!-- type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口--> <javaClientGenerator targetPackage="com.woxin.itsm.mapper" targetProject="src/main/java" type="XMLMAPPER"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 配置表信息 --> <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类 --> <table tableName="sys_user" domainObjectName="User" enableCountByExample="false"> <generatedKey column="id" sqlStatement="Mysql" identity="true"/> </table> <!--<table schema="itsm" tableName="sys_user"--> <!--domainObjectName="User" enableCountByExample="false"--> <!--enableDeleteByExample="false" enableSelectByExample="false"--> <!--enableUpdateByExample="false">--> <!--<!– mysql配置 –>--> <!--<generatedKey column="id" sqlStatement="Mysql" identity="true"/>--> <!--<!– oracle 配置 –>--> <!--<!–<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>–>--> <!--</table>--> <!--<table schema="blog" tableName="article"--> <!--domainObjectName="Article" enableCountByExample="false"--> <!--enableDeleteByExample="false" enableSelectByExample="false"--> <!--enableUpdateByExample="false">--> <!--</table>--> <!--<table schema="blog" tableName="document"--> <!--domainObjectName="Document" enableCountByExample="false"--> <!--enableDeleteByExample="false" enableSelectByExample="false"--> <!--enableUpdateByExample="false">--> <!--</table>--> <!--<table schema="blog" tableName="tag"--> <!--domainObjectName="Tag" enableCountByExample="false"--> <!--enableDeleteByExample="false" enableSelectByExample="false"--> <!--enableUpdateByExample="false">--> <!--</table>--> </context> </generatorConfiguration>
仔细想了下,依赖和配置文件其实是没有改过的,实在是搞不明白,找了几个小时网上也有找到解决办法,不知道是不是没有遇到过这样的情况。
唯一变过的就是项目的Maven配置,原来项目中使用的Maven是IDEA工具自身的配置,后来我把Maven的对应的配置文件和本地依赖存储的路径改了,会不会没有加载到呢?虽然基本上没可能,但是尝试了,果然还是报错。重新引入了依赖、关闭IDEA重启这样无厘头的做法都试了,还是没用,最后还是得仔细看下提示的异常,根据提示信息来理解错误,找到解决办法。
Mapper插件缺少必要的mappers属性!这里我想插件本身是没有问题的,因为它出错的概率太小了,有问题也是因为关联的架包或者配置文件造成的,所以看了下generator的配件文件,跟mappers最接近的,就是下面红色标记的地方了。原来写的是mapper,当时也是可以用的,但不知道为什么这次就用不了了,反正问题解决了,暂时没还仔细想真正原因是什么!如果有朋友想到了,请留言告知,多谢!!!