介绍

MyBatis Generator(MBG)是MyBatis的代码生成器。它将为MyBatis的所有版本生成代码。它将内省一个数据库表(或多个表),并将生成可用于访问表的工件。这减轻了设置对象和配置文件以与数据库表进行交互的麻烦。 MBG试图对简单CRUD(创建,检索,更新,删除)的大部分数据库操作产生重大影响。您仍将需要手工编写SQL和对象代码以进行联接查询或存储过程。

换句话讲,就是不用再重复对数据库字段创建各种对象接口类就能实现CRUD了,芜湖起飞~

所需驱动

commons-logging-1.2.jar
log4j-1.2.17.jar
mybatis-3.5.6.jar
mybatis-generator-core-1.4.0.jar
mysql-connector-java-8.0.21.jar

过程

  1. config的source目录下:

    1. 配置mybatis-config.xml、db.properties
    2. 配置log4j.properties
    ### direct log messages to stdout ###
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.err
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    ### direct messages to file mylog.log ###
    log4j.appender.file=org.apache.log4j.FileAppender
    log4j.appender.file.File=c:\mylog.log
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    ### set log levels - for more verbose logging change 'info' to 'debug' ###
    
    #等级为debug
    log4j.rootLogger=debug, stdout
    
  2. 根目录下配置generatorConfig.xml【重要】

    <?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="MySQL2Tables" targetRuntime="MyBatis3">
        <!-- jdbc四剑客 -->
    		<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
    			connectionURL="jdbc:mysql://localhost:3306/test" userId="root"
    			password="mysql">
              <!-- MySQL8.0 版本需加nullCatalogMeansCurrent参数并设置为真,否则会越库查表 -->
    			<property name="nullCatalogMeansCurrent" value="true"/>
    		</jdbcConnection>
    
    		<javaTypeResolver>
    			<property name="forceBigDecimals" value="false" />
    		</javaTypeResolver>
    
        <!-- 此处大坑!在win表示路径用"\",Unix下为"/"。 -->
    		<javaModelGenerator targetPackage="test.model"
    			targetProject="./src">
    			<property name="enableSubPackages" value="true" />
    			<property name="trimStrings" value="true" />
    		</javaModelGenerator>
    
    		<sqlMapGenerator targetPackage="test.xml"
    			targetProject="./src">
    			<property name="enableSubPackages" value="true" />
    		</sqlMapGenerator>
    
    		<javaClientGenerator type="XMLMAPPER"
    			targetPackage="test.dao" targetProject="./src">
    			<property name="enableSubPackages" value="true" />
    		</javaClientGenerator>
    
        <!-- 此处贴于Generator关于MySQL的配置信息 -->
    		<table tableName="user">
    			<columnOverride column="UNSIGNED_BIGINT_FIELD" javaType="java.lang.Object" jdbcType="LONG" />
    		</table>
    
    	</context>
    </generatorConfiguration>
    
  3. 创建MBG.java

    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 MBG {
    
    	public static void main(String[] args) throws XMLParserException, IOException, InvalidConfigurationException, SQLException, InterruptedException {
    		List<String> warnings = new ArrayList<String>();
    		boolean overwrite = true;
    		File configFile = new File("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);
    	}
    
    }
    
  4. 起飞~

踩过的坑

  1. Generator需要log4j和logging日志输出。
  2. generatorConfig.xml的jdbc配置中,最好增加 ,避免越库查表(MySQL 8.0)。
  3. targetProject="./src"中,在win表示路径用"\",Unix下为"/"。

参考资料

MyBatis Generator