Mybatis自动生成代码

1、配置文件(放在项目下即可)

<?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>
<!-- 指定驱动包位置 mysql驱动包 版本不同-->
<classPathEntry location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\8.0.16\mysql-connector-java-8.0.16.jar"/>

<context id="DB2Tables" targetRuntime="MyBatis3">

<commentGenerator>
<property name="suppressDate" value="true" />
</commentGenerator>

<!-- 配置连接池 库名写在connectionURL后面-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/testapp?useUnicode=true&amp;characterEncoding=utf8&amp;
useSSL=false&amp;serverTimezone=GMT"
userId="root"
password="123456">
</jdbcConnection>

<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>

<!-- 指定实体类位置 -->
<javaModelGenerator targetPackage="nanh.entity.test"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>

<!-- 指定sql定义文件位置 Xml-->
<sqlMapGenerator targetPackage="sqlMapper.test"
targetProject="./src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>

<!--mapper映射器 Dao层 接口 type="XMLMAPPER Xml定义Sql type="ANNOTATEDMAPPER"接口注解-->
<javaClientGenerator type="XMLMAPPER" targetPackage="nanh.dao.test"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>

<!-- 指定数据库表 tableName指定表名 domainObjectName生成实体类类名 库名写在连接池处 -->
<table tableName="threshold" domainObjectName="threshold" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
</table>

<table tableName="deptstate" domainObjectName="deptstate" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="persions" domainObjectName="persions" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
</table>

</context>

</generatorConfiguration>

2、执行文件

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
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.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class RunMybatisGenerator {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
//此文件直接执行即可
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//这个xml文件放在项目下就可以
File configFile = new File("MybagisGenerator.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}

 

posted @ 2019-09-02 15:26  夏天丷  阅读(179)  评论(0编辑  收藏  举报