MBG(mybatisgenerator)自动生成代码工具
建一个maven项目,在pom.xml中配置mbg的配置文件所在位置,在自动生成代码时有时会报
- 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]
- ttp://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
这是缺少一个resource文件夹,解决方案,找到new选项中的source folder选项,自己创建一个src\main\resources文件夹重新运行即可,
运行命令mybatis-generator:generate
自动生成后报错,只是没有jar包,不必担心,导入jar包即可
mbg的配置文件(一部分)
- <?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>
- <!--
- 生成代码的步骤
- 1:连接数据库(驱动包 四要素)
- -->
- <!-- 连接数据库的驱动包
- 路径中有中文会报错
- -->
- <classPathEntry location="E:\mysql-connector-java-5.1.26-bin.jar" />
- <!--
- context 代码生成的规则标签
- targetRuntime mybatis的版本 (详细信息在http://www.mybatis.org/generator/configreference/context.html)
- -->
- <context id="DB2Tables" targetRuntime="MyBatis3">
- <!-- 生成分页代码 -->
- <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
- <!-- 连接数据库的四要素 -->
- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
- connectionURL="jdbc:mysql://localhost:3306/test"
- userId="root"
- password="root">
- </jdbcConnection>
- <javaTypeResolver >
- <property name="forceBigDecimals" value="false" />
- </javaTypeResolver>
- <!-- 实体类 bean 带有get和set方法的bean -->
- <javaModelGenerator targetPackage="cn.et.entity" targetProject="src/main/java">
- <property name="enableSubPackages" value="true" />
- <property name="trimStrings" value="true" />
- </javaModelGenerator>
- <!-- sql语句相关的xml配置文件或者注解的生成包路径 -->
- <sqlMapGenerator targetPackage="cn.et.resource" targetProject="src/main/java">
- <property name="enableSubPackages" value="true" />
- </sqlMapGenerator>
- <!-- 生成的注解所在位置
- <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="cn.et.dao" targetProject="src/main/java">
- -->
- <!-- 生成的接口所在位置 -->
- <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="cn.et.dao" targetProject="src/main/java">
- <property name="enableSubPackages" value="true" />
- </javaClientGenerator>
- <!-- 告诉mbg需要生成代码的表名 -->
- <table tableName="food">
- </table>
- </context>
- </generatorConfiguration>