MyBatis-generator常见错误(整合)
错误一:(property*,plugin*,commentGenerator?,(connectionFactory|jdbcConnection)
[INFO] Building ssm01 Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- mybatis-generator-maven-plugin:1.3.5:generate (default-cli) @ ssm01 ---
[ERROR] XML Parser Error on line 71: 元素类型为 "context" 的内容必须匹配 "(property*,plugin*,commentGenerator?,(connectionFactory|jdbcConnection),javaTypeResolver?,javaModelGenerator,sqlMapGenerator?,javaClientGenerator?,table+)"。
[INFO] ------------------------------------------------------------------------
这个问题我都被折腾了好久。因为我把这段代码的位置,移动了好几个地方,就是差一个地方没有移动到,于是就被折腾了好久。
解决:代码顺序问题!!
以下是正确的 myBatisGeneratorConfig.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>
<!-- 本地数据库驱动程序jar包的全路径 使用时改称自己的本地路径-->
<classPathEntry location="D:/software/Mavenrepo/mysql/mysql-connector-java/5.1.44/mysql-connector-java-5.1.44.jar"/>
<context id="context" targetRuntime="MyBatis3">
<!--定义生成的java类的编码格式-->
<property name="javaFileEncoding" value="UTF-8"/>
<!--suppressAllComments 设置为true 则不再生成注释-->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库的相关配置 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm_crud"
userId="root"
password="123"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 实体类生成的位置 -->
<javaModelGenerator targetPackage="com.ltq.model" targetProject="src/main/java">
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- Mapper.xml 文件的位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
</sqlMapGenerator>
<!-- Mapper 接口文件的位置 -->
<javaClientGenerator targetPackage="com.ltq.mapper" targetProject="src/main/java" type="XMLMAPPER">
</javaClientGenerator>
<!-- table指定每个生成表的生成策略 表名 和 model实体类名-->
<table tableName="tbl_emp" domainObjectName="Employee" enableSelectByExample="true"
enableDeleteByExample="true" enableCountByExample="true"
enableUpdateByExample="true" selectByExampleQueryId="true">
<property name="ignoreQualifiersAtRuntime" value="false"/>
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="tbl_dept" domainObjectName="Department" enableSelectByExample="true"
enableDeleteByExample="true" enableCountByExample="true"
enableUpdateByExample="true" selectByExampleQueryId="true">
<property name="ignoreQualifiersAtRuntime" value="false"/>
<property name="useActualColumnNames" value="false"/>
</table>
</context>
</generatorConfiguration>
错误二:Exception getting JDBC Driver: com.mysql.jdbc.Driver
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate (default-cli) on project ssm01: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate failed: Exception getting JDBC Driver: com.mysql.jdbc.Driver -> [Help 1]
问题接一连二,用maven mybatis插件,如果不在plugin里面添加依赖包得引用的话,会找不到相关得jar包
解决:在pom.xml中添加依赖
<!-- mybatis-generator -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${org.mybatis.generator.version}</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<!--
用maven mybatis插件
如果不在plugin里面添加依赖包得引用的话,会找不到相关得jar包,
在plugin外部得jar包,他不会去找到并执行,
所以要把plugin运行依赖得jar配置都放在里面
-->
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${com.mybatis.mybatis.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</plugin>