Use mybatis to automatically generate entity classes and mapper mapping files and interface files in reverse

Problem Description:

I'm doing web stuff recently. I want to generate entity classes from the database, then operate the database, and then do some work of adding, deleting, modifying and checking.

It is found that you can directly use mybatis to directly generate javabeans, and you can directly generate mapper mapping files. The specific operations are as follows:

Create a new maven project:

(1) Add the jar package that needs to be depended on in the pom file (the following three dependencies)

<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.38</version>

</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core -->
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-core</artifactId>
<version>3.0</version>
</dependency>

(2) Create a generatorConfig.xml file in the root directory.

<?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="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- Whether to remove automatically generated comments true: yes: false: no -->
<property name="suppressAllComments" value="true " />
</commentGenerator>
<!--Database connection information: driver class, connection address, username, password mysql database connection -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc: mysql://zhb-pc:3306/rrv" userId="report"
password="report">
</jdbcConnection>
<!--oracle database connection-->
<!-- <jdbcConnection driverClass="oracle.jdbc. OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
userId="yycg"
password="yycg">
</jdbcConnection> -->

<!-- Default false, resolve JDBC DECIMAL and NUMERIC types to Integer, when true,
resolve JDBC DECIMAL and NUMERIC types to java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false " />
</javaTypeResolver>

<!-- targetProject: The location where the PO class is generated-->
<javaModelGenerator targetPackage="com.report.pojo"
targetProject="src/main/java">
<!-- enableSubPackages: Whether to use schema as the suffix of the package-- >
<property name="enableSubPackages" value="false" />
<!-- spaces before and after values ​​returned from the database are sanitized -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject: The location where the mapper mapping file is generated-->
<sqlMapGenerator targetPackage="com.itheima.mapper"
targetProject="src/main/resources">
<!-- enableSubPackages: Whether to use schema as the suffix of the package- ->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage: the location where the mapper interface is generated -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.report.dao"
targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<!-- <table tableName="items"></table> -->
<table tableName="rule_conf"></table>
<table tableName="rule_run_result"></table>
<!-- <table tableName="user"></table> -->
<!-- <table schema="" tableName="sys_user"></table>
<table schema="" tableName="sys_role"></table>
<table schema="" tableName="sys_permission"></table>
<table schema="" tableName="sys_user_role"></table>
<table schema="" tableName="sys_role_permission"></table> -->

<!-- Some table fields need to specify java type
<table schema="" tableName="">
<columnOverride column="" javaType="" />
</table> -->
</context>
</generatorConfiguration>

(3) Write a main class and then run it to generate it.

import java.io.File;
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.internal.DefaultShellCallback;
public class Generate {
public void generator() throws Exception{
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);
}
public static void main(String[] args) throws Exception {
try {
Generate generatorSqlmap = new Generate();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Summary: The package defined in the xml file does not need to be created manually, and needs to be generated directly during execution.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324694925&siteId=291194637