mybatis generator 代码生成有四中方式,这里我们列出三种,重点说下其中两项常用的。
无论哪种方式都需要配置resources目录下的generatorConfig.xml文件,这个很重要。因此先贴下文件内容:
具体每个属性的含义请搜素:mybatis generator.xml详解:https://blog.csdn.net/weixin_39805338/article/details/80999186
<?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>
<!-- 指定数据连接驱动jar地址 -->
<classPathEntry location="D:\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar" />
<!-- 不生成mybatis的注释,获取数据库表的注释 -->
<context id="mysql2Beans" targetRuntime="MyBatis3">
<!-- 不生成mybatis的注释,获取数据库表的注释 -->
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据源连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/hello_mybatis_generator?
useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=TRUE&
zeroDateTimeBehavior=round&nullCatalogMeansCurrent=true&useSSL=false&nullNamePatternMatchesAll=true"
userId="root" password="123456789">
<property name="remarksReporting" value="true" />
</jdbcConnection>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.github.domain" targetProject="src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="false"/>
</javaModelGenerator>
<!-- 生成mapxml文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成mapxml对应client,也就是接口dao -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.github.dao.mapper"
targetProject="src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 配置表信息 -->
<table tableName="t_user" domainObjectName="User"> </table>
</table>
</context>
</generatorConfiguration>
第一种:通过MybatisGenerator类和配置文件生成代码
package com.github.dao;
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.util.ArrayList;
import java.util.List;
public class MybatisGeneratorRun {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(MybatisGeneratorRun.class.getClassLoader().getResourceAsStream("generatorConfig.xml"));
DefaultShellCallback shellCallback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
myBatisGenerator.generate(null);
System.out.println(warnings);
}
}
第二种:使用mybatis-generator-maven-plugin插件自动生成MyBatis代码
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<configurationFile>
${basedir}/src/main/resources/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
第三种:使用命令行创建,命令创建需要单独的generator.properties属性文件,这里不做介绍。
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite