MBG(mybatisgenerator)自动生成代码工具

建一个maven项目,在pom.xml中配置mbg的配置文件所在位置,在自动生成代码时有时会报

[html] view plain copy
  1. Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate (default-cli) on project cn.et: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5:generate failed: Cannot resolve classpath entry: E:\servletWorkspace\myeclipsWorkspace\MGB\src\main\resources -> [Help 1]  
  2. ttp://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException  

这是缺少一个resource文件夹,解决方案,找到new选项中的source folder选项,自己创建一个src\main\resources文件夹重新运行即可,

运行命令mybatis-generator:generate

MBG(mybatisgenerator)自动生成代码工具

自动生成后报错,只是没有jar包,不必担心,导入jar包即可

mbg的配置文件(一部分)

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE generatorConfiguration  
  3.   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
  4.   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
  5.   
  6. <generatorConfiguration>  
  7. <!--   
  8.     生成代码的步骤  
  9.     1:连接数据库(驱动包 四要素)  
  10.  -->  
  11.     <!-- 连接数据库的驱动包   
  12.     路径中有中文会报错  
  13.     -->  
  14.   <classPathEntry location="E:\mysql-connector-java-5.1.26-bin.jar" />  
  15.     <!--  
  16.     context 代码生成的规则标签  
  17.      targetRuntime mybatis的版本 (详细信息在http://www.mybatis.org/generator/configreference/context.html)  
  18.      -->  
  19.   <context id="DB2Tables" targetRuntime="MyBatis3">  
  20.   <!-- 生成分页代码 -->  
  21.   <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>  
  22.     <!-- 连接数据库的四要素 -->  
  23.     <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
  24.         connectionURL="jdbc:mysql://localhost:3306/test"  
  25.         userId="root"  
  26.         password="root">  
  27.     </jdbcConnection>  
  28.   
  29.     <javaTypeResolver >  
  30.       <property name="forceBigDecimals" value="false" />  
  31.     </javaTypeResolver>  
  32.     <!-- 实体类 bean 带有get和set方法的bean -->  
  33.     <javaModelGenerator targetPackage="cn.et.entity" targetProject="src/main/java">  
  34.       <property name="enableSubPackages" value="true" />  
  35.       <property name="trimStrings" value="true" />  
  36.     </javaModelGenerator>  
  37.     <!-- sql语句相关的xml配置文件或者注解的生成包路径 -->  
  38.     <sqlMapGenerator targetPackage="cn.et.resource"  targetProject="src/main/java">  
  39.       <property name="enableSubPackages" value="true" />  
  40.     </sqlMapGenerator>  
  41.     <!-- 生成的注解所在位置   
  42.     <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="cn.et.dao"  targetProject="src/main/java">  
  43.     -->  
  44.     <!-- 生成的接口所在位置 -->  
  45.     <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="cn.et.dao"  targetProject="src/main/java">  
  46.       <property name="enableSubPackages" value="true" />  
  47.     </javaClientGenerator>  
  48.     <!-- 告诉mbg需要生成代码的表名 -->  
  49.     <table tableName="food">  
  50.     </table>  
  51.   
  52.   </context>  
  53. </generatorConfiguration>