提前记录问题
- generatorConfig.xml中地址如果写错,就不会生成文件,但是也不报错。如果地址不存在,会自动创建地址。
- 要在配置文件中写
classPathEntry
标签 - mac是
/
win是\
- 配置文件中,
<context id="mybatisGenerator" targetRuntime="MyBatis3Simple">
,targetRuntime是MyBatis3
(或者MyBatis3Simple
) 不是MyBatis
目录
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b4ad26264ec53b6dc09aadc4832a2020.png)
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>
<classPathEntry
location="/Users/...文件地址.../mysql-connector-java/8.0.20/mysql-connector-java-8.0.20.jar"/>
<context id="mybatisGenerator" targetRuntime="MyBatis3Simple">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/kill?useSSL=false"
userId="root"
password="root.123">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="model.entity"
targetProject="./src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper"
targetProject="./src/main/resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="model.mapper"
targetProject="./src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<table tableName="item"></table>
<table tableName="item_kill"></table>
<table tableName="item_kill_success"></table>
<table tableName="random_code"></table>
<table tableName="user"></table>
</context>
</generatorConfiguration>
pom.xml
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
生成方法
public class Generator {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/generatorConfig.xml");
ConfigurationParser parser = new ConfigurationParser(warnings);
Configuration config = parser.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 {
Generator generatorSqlmap = new Generator();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Maven中执行
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6c1462d2cd2a622e6b809d1e2478f3bf.png)