Mybatis逆向工程


Mybatis逆向工程

1、什么是Mybatis逆向工程

  • mybatis逆向工程是一個可以快速根據數據庫表幫我們生成pojo實體類和mapper接口和mapper映射文件的一個插件,需要下載該項目。

  • 注意:只支持單表操作(單表的增刪改查等sql可以幫助我們生成),關聯查詢需要自己寫。

  • 建議查閱tk-mybatis 更加好用的工具。

2、引入依賴pom.xml

<dependencies>
    <!--mybatis逆向工程-->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.7</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.21</version>
    </dependency>
</dependencies>

3、配置文件的配置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>
    <!--指定了驅動jar包的位置,這個是針對下載Jar包的方式,因為用了maven所以這個就用不上了-->
    <!-- <classPathEntry location="D:/mvn_repository_new/mysql/mysql-connector-java/5.1.45/mysql-connector-java-5.1.45.jar"/>-->

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--數據庫鏈接URL,用戶名、密碼 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/online?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;allowPublicKeyRetrieval=true"
                        userId="root"
                        password="mc123456">
        </jdbcConnection>
        <!--指定生成entity實體類的具體位置-->
        <javaModelGenerator targetPackage="com.mc.entity" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--指定生成mybatis映射xml文件的包名和位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--指定生成mapper接口的具體位置-->
        <javaClientGenerator targetPackage="com.mc.mapper" targetProject="./src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--要生成entity/mapper的表名及自定義的DO名-->
        <!--<table tableName="users" domainObjectName="User"/>-->

        <!--<table tableName="product_brand" domainObjectName="ProductBrand" />-->

        <!--mybatis generator代碼生成器在默認的情況下會生成對於表實體類的一個Examle類, 可以更改生成器的配置可避免生成Examle類,
        enableCountByExample,enableUpdateByExample,enableDeleteByExample,enableSelectByExample等配置為false后, 就不會生成生成Examle類了 -->

        <table tableName="t_bis_wallet" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
       
    <!--或者直接寫表名-->
	<!--<table schema="" tableName="product_type"></table>-->

    </context>
</generatorConfiguration>

4、配置Generator的插件pom.xml

<build>
    <plugins>
        <!-- mybatis逆向工程插件 -->
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.7</version>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.21</version>
                </dependency>
            </dependencies>
            <configuration>
                <!--配置文件的路徑-->
                <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
                <overwrite>true</overwrite>
            </configuration>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <includes>
                <include>**/*.*</include>
            </includes>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>

5、進行單元測試

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring-context*.xml")
public class TestGenerator {

    @Autowired
    TBusinessMapper tBusinessMapper;

    @Test
    public void testInsertBusiness(){
        TBusiness tBusiness = new TBusiness();
        tBusiness.setBsId(2L);
        tBusiness.settBsId(2L);
        tBusiness.setBsName("海爾冰箱");
        tBusiness.setTbId(2L);
        tBusinessMapper.insert(tBusiness);
    }
}

6、編寫啟動程序

package com.mybatisTools;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.ProgressCallback;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorSqlmap {
    public GeneratorSqlmap() {
    }

    public void generator() throws Exception {
        List<String> warnings = new ArrayList();
        boolean overwrite = true;
        File configFile = new File("src/main/resources/generator/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((ProgressCallback)null);
    }

    public static void main(String[] args) throws Exception {
        try {
            GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
            generatorSqlmap.generator();
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }
}

也可以雙擊圖中位置啟動


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM