MyBatis 根據資料表反向生成 java 實體類等
1、MyBatis 根據已有的資料表生成對應的 Java 實體類的配置檔案:
mybatisReverseGeneratorConfig.xml
此檔案放到專案的根目錄下
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE generatorConfiguration
3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
5 <generatorConfiguration>
6
7 <context id="GBaseContext" targetRuntime="MyBatis3" defaultModelType="flat">
8
9 <!--前置分隔符-->
10 <property name="beginningDelimiter" value="`"/>
11 <!--後置分隔符-->
12 <property name="endingDelimiter" value="`"/>
13 <!--設定編碼-->
14 <property name="javaFileEncoding" value="UTF-8"/>
15 <!--生成mapper.xml時覆蓋原檔案-->
16 <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
17
18 <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
19 connectionURL="jdbc:mysql://127.0.0.1:3309/db_demo"
20 userId="admin"
21 password="123456">
22 </jdbcConnection>
23
24 <javaModelGenerator targetPackage="com.smbea.po.generated" targetProject="src/main/java">
25 <property name="enableSubPackages" value="true"/>
26 <property name="trimStrings" value="true"/>
27 </javaModelGenerator>
28
29 <!-- 反向生成持久化物件(*PO) -->
30 <sqlMapGenerator targetPackage="com.smbea.mapper.mysql.generated" targetProject="src/main/resources">
31 <property name="enableSubPackages" value="true"/>
32 </sqlMapGenerator>
33
34 <!-- 反向生成 Mapper 介面(*Mapper) -->
35 <javaClientGenerator type="XMLMAPPER" targetPackage="com.smbea.mapper.mysql.generated" targetProject="src/main/java">
36 <property name="enableSubPackages" value="true"/>
37 </javaClientGenerator>
38
39 <!-- <table tableName="%"></table>--> <!-- % 表示全部表 -->
41 <table tableName="t_user" domainObjectName="User"></table>
42 <table tableName="t_order" domainObjectName="Order"></table>
43 <table tableName="t_trolly" domainObjectName="Trolly"></table>
50
51 </context>
52
53 </generatorConfiguration>
2、在專案的 pom.xml 檔案加入如下依賴:
1 <dependency>
2 <groupId>org.mybatis.generator</groupId>
3 <artifactId>mybatis-generator-core</artifactId>
4 <version>1.4.0</version>
5 </dependency>
3、反向生成入口類:
1 package com.smbea.tool;
2
3 import org.mybatis.generator.api.MyBatisGenerator;
4 import org.mybatis.generator.config.Configuration;
5 import org.mybatis.generator.config.xml.ConfigurationParser;
6 import org.mybatis.generator.internal.DefaultShellCallback;
7
8 import java.io.File;
9 import java.util.ArrayList;
10 import java.util.List;
11
12 /**
13 * MyBatis 反向工程生成器
14 */
15 public class MyBatisReverseGenerator {
16
17 public static void main(String[] args) throws Exception {
18 List<String> warnings = new ArrayList<>();
19 boolean overwrite = true;
20 File configFile = new File("mybatisReverseGeneratorConfig.xml");
21 ConfigurationParser cp = new ConfigurationParser(warnings);
22 Configuration config = cp.parseConfiguration(configFile);
23 DefaultShellCallback callback = new DefaultShellCallback(overwrite);
24 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
25 myBatisGenerator.generate(null);
26 }
27
28 }