Mybatis Generator的使用

4次阅读

共计 2956 个字符,预计需要花费 8 分钟才能阅读完成。

在写代码过程中,常常要写一些简单的 CURD 操作,为了能够把时间用在业务逻辑上,看了 Mybatis Generator 生成工具,根据官网的文档,改成适合自己使用的生成器。
mybatis generator 的配置文件 如下:
<?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>
<!– 读取配置文件 –>
<properties resource=”generator.properties” />

<context id=”MySQLContext” targetRuntime=”MyBatis3″>
<!– 设置文件编码 –>
<property name=”javaFileEncoding” value=”UTF-8″/>

<!– 配置去掉所有生成的注释 –>
<commentGenerator>
<property name=”suppressAllComments” value=”true” />
</commentGenerator>

<!– 设置数据库连接驱动 –>
<jdbcConnection driverClass=”${jdbc.driverClass}”
connectionURL=”${jdbc.url}”
userId=”${jdbc.username}”
password=”${jdbc.password}”>
</jdbcConnection>

<!– 当字段类型是 DECIMAL 或者 NUMERIC 时,是否强制转换为 BigDecimal, 否的话会自动根据规模的大小选择合适的类型 –>
<javaTypeResolver >
<property name=”forceBigDecimals” value=”false” />
</javaTypeResolver>

<!– 生成模型的包名和位置 –>
<javaModelGenerator targetPackage=”me.xueyao.model” targetProject=”.\src\main\java”>
<property name=”enableSubPackages” value=”true” />
<property name=”trimStrings” value=”true” />
</javaModelGenerator>

<!– 生成映射文件的包名和位置 –>
<sqlMapGenerator targetPackage=”me.xueyao.mapper” targetProject=”.\src\main\resources”>
<property name=”enableSubPackages” value=”true” />
</sqlMapGenerator>

<!– 生成 DAO 的包名和位置 –>
<javaClientGenerator type=”XMLMAPPER” targetPackage=”me.xueyao.mapper”
targetProject=”.\src\main\java”>
<property name=”enableSubPackages” value=”true” />
</javaClientGenerator>

<!– 要生成的表 tableName 是数据库中的表名或视图名 domainObjectName 是实体类名,需要根据自己的需求修改 –>
<table tableName=”candidate” domainObjectName=”Candidate” enableCountByExample=”false”
enableDeleteByExample=”false” enableSelectByExample=”false” enableUpdateByExample=”false”>
<generatedKey column=”id” sqlStatement=”MySql” identity=”true” />
</table>

</context>
</generatorConfiguration>
mybatis generator 的执行文件 如下:
package me.xueyao;

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;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
* @Description: Mybatis Generator 生成器
* @Author: Simon.Xue
* @Date: 2019/1/18 13:44
*/
public class Generator {

public static void main(String[] args) throws Exception {
// 警告信息集合
List<String> warnings = new ArrayList<String>();
// 读取生成器的配置文件
InputStream resourceAsStream = Generator.class.getResourceAsStream(“/mybatis-generator.xml”);
// 创建配置解析器
ConfigurationParser configurationParser = new ConfigurationParser(warnings);
// 解析配置文件
Configuration configuration = configurationParser.parseConfiguration(resourceAsStream);
resourceAsStream.close();
//true 时,如果有相同的文件则覆盖文件
DefaultShellCallback defaultShellCallback = new DefaultShellCallback(true);
// 创建生成器对象
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, defaultShellCallback, warnings);
// 执行生成代码
myBatisGenerator.generate(null);
// 输出警告信息
for (String warning : warnings) {
System.out.println(warning);
}
}
}
源代码托管在 GitHub

正文完
 0