XML import mybatis, mybatis- Generator-core, and MySQL driver package.
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
</dependencies>
Copy the code
2, add a mybatis- Generator configuration file in the project root directory
<! DOCTYPEgeneratorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<! -- Specify driver package -->
<classPathEntry location="C: \ Users \ mysql connector - Java - 5.1.43. Jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<! -- Set connection parameters -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/****" <!--Setting up the database-->
userId="root"
password="11111111">
</jdbcConnection>
<! -- Set entity class location -->
<javaModelGenerator targetPackage="ovls.entity" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<! -- Select SQL XML location -->
<sqlMapGenerator targetPackage="sql" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<! -- Specify Mapper interface location -->
<javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="***.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<! -- Specify which tables are used to generate the above elements.
<table tableName="%" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
</table>
</context>
</generatorConfiguration>
Copy the code
3. Load relevant configuration files and perform generation
public class RunMyBatisGenerator {
public static void main(String[] args) throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.xml");// Set the accessory name in step 2
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); }}Copy the code
Run the class, OK.