A, jar package ready
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>Log4j</artifactId> <version>1.2.16</version> </dependency> </dependencies>
Second, the configuration file
<?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="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--Database connection information: driver class, the connection address, user name, password -> < JdbcConnection driverClass = "com.mysql.cj.jdbc.Driver" connectionURL = "jdbc:? MySQL: // localhost: 3306 / = the SSM serverTimezone & amp UTC; = useSSL to false " the userId =" XXX " password =" XXX " > </ JdbcConnection > ! <- <JdbcConnection driverClass =" oracle.jdbc.OracleDriver " the connectionURL =" JDBC: Oracle: Thin: @ 127.0.0.1: 1521: YycG " the userId =" YycG " password =" YycG "> </ JdbcConnection> -> <! -Default false, the JDBC DECIMAL and NUMERIC type resolution as Integer, when the true JDBC DECIMAL and NUMERIC type resolution is java.math.BigDecimal -> < javaTypeResolver > < Property name = "forceBigDecimals" value = "to false" /> < / javaTypeResolver > <-! targetProject: generating position POJO class -> < javaModelGenerator targetPackage = "org.kgc.pojo" targetProject = "./ src / main / the Java" > <-! enableSubPackages: whether to allow schema as package suffix -> < Property name = "enableSubPackages" value= "false" /> <-! spaces before and after the clean-up value is returned from the database -> < Property name = "trimStrings" value = "to true" /> </ javaModelGenerator > <-! targetProject: Mapper mapping file generated location -> < sqlMapGenerator targetPackage = "org.kgc.mapper" targetProject = "./ src / main / the Java" > <-! enableSubPackages: whether to allow schema package as a suffix -> < Property name = " enableSubPackages " value =" to false " /> </sqlMapGenerator> <!--targetPackage: mapper interface generation location -> < javaClientGenerator of the type = "XMLMAPPER" targetPackage = "org.kgc.mapper" targetProject = "./ src / main / the Java" > <-! enableSubPackages: whether to allow schema as the package suffix -> < Property name = "enableSubPackages" value = "to false" /> </ javaClientGenerator > <-! specified database table -> < table tableName = "edoc_entry" > </ table > <table tableName="edoc_category"></table> </context> </generatorConfiguration>
Third, run the program
import java.io.File; 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 GeneratorSqlmap { public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置文件 File configFile = new File("./src/main/resources/generatorConfig.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); } public static void main(String[] args) throws Exception { try { GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }