如何實(shí)現(xiàn)Springboot版mybatis逆向生成

這篇文章主要介紹如何實(shí)現(xiàn)Springboot版mybatis逆向生成,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、聶榮ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的聶榮網(wǎng)站制作公司

代碼生成器

逆向生成MySQL對(duì)應(yīng)的pojo和mybatis通過(guò)單獨(dú)的一個(gè)工程不影響現(xiàn)有的業(yè)務(wù)邏輯。

  • mybatis-generatorConfig工程

如何實(shí)現(xiàn)Springboot版mybatis逆向生成

如何實(shí)現(xiàn)Springboot版mybatis逆向生成

如何實(shí)現(xiàn)Springboot版mybatis逆向生成

通過(guò)源碼里面的sql生成對(duì)應(yīng)的表結(jié)構(gòu)和表數(shù)據(jù)。

如何實(shí)現(xiàn)Springboot版mybatis逆向生成

mybatis-generatorConfig的pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>


   <groupId>com.idig8</groupId>
    <version>0.0.1-SNAPSHOT</version>

  <artifactId>mybatis-generatorConfig</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <!-- 引入log4j日志依賴(lài) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>

        <!-- 阿里開(kāi)源數(shù)據(jù)源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.41</version>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!--mapper-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.2.4</version>
        </dependency>
        <!--pagehelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- mybatis 逆向生成工具  -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

添加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>
    <context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.idig8.utils.MyMapper"/>
        </plugin>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://47.94.162.28:3306/test"
                        userId="XXXXX"
                        password="XXXXX">
        </jdbcConnection>

        <!-- 對(duì)應(yīng)生成的pojo所在包 -->
        <javaModelGenerator targetPackage="com.idig8.pojo" targetProject="src/main/java"/>

        <!-- 對(duì)應(yīng)生成的mapper所在目錄 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>

        <!-- 配置mapper對(duì)應(yīng)的java映射 -->
        <javaClientGenerator targetPackage="com.idig8.mapper" targetProject="src/main/java" 
        type="XMLMAPPER"/>


        <table tableName="comments"></table>

    </context>
</generatorConfiguration>

GeneratorDisplay.java類(lèi)

package com.idig8.mybatis.utils;

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 GeneratorDisplay {

    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 {
            GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

添加MyMapper.java文件

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2016 abel533@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.idig8.utils;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
    //TODO
    //FIXME 特別注意,該接口不能被掃描到,否則會(huì)出錯(cuò)
}
  • 運(yùn)行GeneratorDisplay的main方法

如何實(shí)現(xiàn)Springboot版mybatis逆向生成

  1. 復(fù)制com.idig8.mapper目錄到wx-springboot-mapper項(xiàng)目下
    如何實(shí)現(xiàn)Springboot版mybatis逆向生成
    如何實(shí)現(xiàn)Springboot版mybatis逆向生成

  2. 復(fù)制com.idig8.pojo 目錄到wx-springboot-pojo項(xiàng)目下
    如何實(shí)現(xiàn)Springboot版mybatis逆向生成
    如何實(shí)現(xiàn)Springboot版mybatis逆向生成

  3. 復(fù)制com.idig8.utils 目錄到wx-springboot-common項(xiàng)目下
    如何實(shí)現(xiàn)Springboot版mybatis逆向生成
    如何實(shí)現(xiàn)Springboot版mybatis逆向生成

  4. 復(fù)制resources mapper到wx-springboot-api的resources里面
    如何實(shí)現(xiàn)Springboot版mybatis逆向生成
    如何實(shí)現(xiàn)Springboot版mybatis逆向生成

PS:mybatis-generatorConfig 可以看成一個(gè)單獨(dú)的項(xiàng)目,主要的目的就是為了升成對(duì)應(yīng)的mapper.xml和對(duì)應(yīng)的pojo實(shí)體類(lèi),mapper對(duì)應(yīng)的實(shí)體。

以上是“如何實(shí)現(xiàn)Springboot版mybatis逆向生成”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁(yè)名稱(chēng):如何實(shí)現(xiàn)Springboot版mybatis逆向生成
文章出自:http://bm7419.com/article44/igicee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、搜索引擎優(yōu)化、微信公眾號(hào)、電子商務(wù)、外貿(mào)建站、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化