运行:muven build : mybatis-generator:generate
新建一个maven项目:pom.xml:
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>src/main/java/mbg.xml</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
配置文件:
<?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="d:\mysql-connector-java-5.1.26-bin.jar" />
<!-- 设置生成代码的规则
targetRuntime:开发环境使用Mybatis的版本
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 分页插件 -->
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/food"
userId="root"
password="123456">
</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>
<!-- 生成的接口所在位置
type="ANNOTATEDMAPPER" :接口映射sql注解
type="XMLMAPPER" :接口映射sql使用xml
-->
<javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="cn.et.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 告诉mbg 需要生成代码的表 -->
<table tableName="student" >
</table>
<!-- 需要生成代码的表可以有多个 再加一个
<table tableName="表名" >
</table>
-->
</context>
</generatorConfiguration>