package com.util; 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.File; import java.util.ArrayList; import java.util.List; /** * 描述: * <p> * author caoxiaoyang * date 2021-04-27 * version */ public class GenUtils { public static void main(String[] args) { try { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; String genCfg = "generatorConfig.xml"; File configFile = new File("D:\generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = null; config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = null; myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } catch (Exception e) { e.printStackTrace(); } } }
<?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 id="mysqlTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库连接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/date?characterEncoding=UTF-8" userId="root" password="123456"/> <!--指定生成的类型为java类型,避免数据库中number等类型字段 --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--自动生成的实体的存放包路径--> <javaModelGenerator targetPackage="com.pojo" targetProject="d:/"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--自动生成的*Mapper.xml文件存放路径 --> <sqlMapGenerator targetPackage="mappers" targetProject="d:/"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--自动生成的*Mapper.java存放路径 --> <!-- <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" targetProject="src/main/java">--> <!-- <property name="enableSubPackages" value="true"/>--> <!-- </javaClientGenerator>--> <table tableName="table_name" domainObjectName="tableName" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>