Step 1: POM.xml

<dependencies> <dependency> <groupId>com.macro.mall</groupId> <artifactId>mall-common</artifactId> < version > 1.0 - the SNAPSHOT < / version > < / dependency > <! MyBatis --> <dependency> <groupId>org.mybatis. Generator </groupId> <artifactId> The < version > 1.3.3 < / version > < / dependency > <! -- MyBatis--> <dependency> <groupId>org. MyBatis </artifactId> <version>3.4.6</version> </dependency> <! --> <dependency> <groupId> Mysql </groupId> <artifactId>mysql-connector-java</artifactId> The < version > 8.0.15 < / version > < / dependency > < / dependencies >Copy the code

Step 2: Generatorconfig.xml

<? The 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" defaultModelType="flat"> <property  name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <property name="javaFileEncoding" value="UTF-8"/> <! - generated serialization method for model - > < plugin type = ". Org. Mybatis generator. Plugins. SerializablePlugin "/ > <! - create a toString method for the generated Java model - > < plugin type = ". Org. Mybatis generator. Plugins. ToStringPlugin "/ > < commentGenerator type="com.macro.mall.CommentGenerator"> <! -- Whether to remove automatically generated comments true: False: no --> < Property name="suppressAllComments" value="true"/> < Property name="suppressDate" value="true"/> < Property name="addRemarkComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}"> <! <property name="nullCatalogMeansCurrent" value="true" /> </jdbcConnection> <javaModelGenerator targetPackage="com.macro.mall.model" targetProject="mall-mbg\src\main\java"/> <sqlMapGenerator targetPackage="com.macro.mall.mapper" targetProject="mall-mbg\src\main\resources"/> <javaClientGenerator type="XMLMAPPER" targetPackage="com.macro.mall.mapper" targetProject="mall-mbg\src\main\java"/> <! <table tableName="%"> <generatedKey ="id" sqlStatement="MySql" identity="true"/> </table> </context> </generatorConfiguration>Copy the code

Step 3: Generator.properties

jdbc.driverClass=com.mysql.jdbc.Driver jdbc.connectionURL=jdbc:mysql://localhost:3306/mall? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai jdbc.userId=root jdbc.password=123456Copy the code

Step 4: Generator

package com.macro.mall; 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; Public class Generator {public static void main(String[] args) {public static void main(String[] args); Throws Exception {//MBG warning information List<String> warnings = new ArrayList<String>(); //MBG warning information List<String> warnings = new ArrayList<String>(); Boolean overwrite = true; / / read our MBG configuration file InputStream is = the Generator. The class. The getResourceAsStream ("/generatorConfig. XML "); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); // Create MBG MyBatisGenerator MyBatisGenerator = new MyBatisGenerator(config, callback, warnings); / / execution code generation myBatisGenerator. Generate (null). For (String warning: warnings) {system.out.println (warning); }}}Copy the code