Generator代码生成有四种方法:
1,使用Java编写代码运行
2,用命令提示符运行
3,使用Maven Plugin运行
4,使用Eclipse插件运行
1,Java编写代码实现:
Maven添加依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
生成类编写
package cn.javabb.code.generator;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class Generator{
public static void main(String[] args) throws Exception{
List<String> warnings = new ArrayList<String>();
//是否覆盖原有文件
boolean overwrite = true;
InputStream is = Generator.class.getResourceAsStream("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(is);
is.close();
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
//创建MBG
MyBatisGenerator mybatisGenerator = new MyBatisGenerator(config,callback,warnings);
mybatisGenerator.generate(null);
for(String warning:warnings){
System.out.println(warning);
}
}
}
配置文件:(和生成类同一路径)
<?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>
<!-- 一个数据库一个context -->
<context id="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!-- 注释 -->
<commentGenerator >
<!-- <property name="addRemarkComments" value="true"/> -->
<property name="suppressAllComments" value="true"/> <!--是否取消注释,这个注释太难看了 -->
<property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
</commentGenerator>
<!-- jdbc连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/db_bms"
userId="root"
password="root" />
<!-- 类型转换 -->
<!-- <javaTypeResolver>
是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.)
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> -->
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="cn.javabb.sys.entity"
targetProject="D:/ftl" >
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
<!-- 是否针对string类型的字段在set的时候进行trim调用 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成mapxml文件 -->
<sqlMapGenerator targetPackage="cn.javabb.sys.mapper.xml"
targetProject="D:/ftl" >
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成mapxml对应client,也就是接口dao -->
<javaClientGenerator targetPackage="cn.javabb.sys.mapper"
targetProject="D:/ftl" type="XMLMAPPER" >
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 配置表信息 -->
<table schema="db_bms" tableName="%"
enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类 -->
<!-- 去掉表名前缀Sys -->
<domainObjectRenamingRule searchString="^Sys" replaceString="" />
<!-- 忽略列,不生成bean 字段 -->
<ignoreColumn column="FRED" />
<!-- 指定列的java数据类型 -->
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
</context>
</generatorConfiguration>
启动生成类就可以实现功能了。生成的代码包括Mapper Mapper.xml Model
上面的就是自己写的正在用的代码生成配置。