使用mybatis逆向工程生成sql操作語句和pojo,mapper


今天主要介紹如何使用mybatis逆向工程生成操作語句,mybatis-geneator是一款mybatis自動代碼生成工具,可以通過配置,快速生成mapper和xml文件。

 

首先需要到官網上下載相關core程序http://www.mybatis.org/generator/

還需要下載下列包

image

 

其中,mybatis和mybatis-generator為必須包,其他的可以根據數據庫類型選擇jdbc鏈接包

本案例使用sqlserver數據庫

需要在目錄下創建存儲路徑的包名

mapper和pojo,后期可以在xml文件中修改

 

image

創建一個java程序

 

image

import java.io.File;
import java.io.IOException;
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.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorSqlmap {

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

}

 

並且需要配置xml文件

<commentGenerator>
    <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
    <property name="suppressAllComments" value="false" />
</commentGenerator>
<!--數據庫連接的信息:驅動類、連接地址、用戶名、密碼 -->
<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"      //數據庫jdbc連接方式
    connectionURL="jdbc:sqlserver://127.0.0.1; DatabaseName=account" userId="sa"      //數據庫賬號密碼
    password="123456">
</jdbcConnection>

 

<!-- targetProject:生成PO類的位置 -->
        <javaModelGenerator targetPackage="com.mybatis.pojo"           //pojo生成位置
            targetProject=".\src">
            <!-- enableSubPackages:是否讓schema作為包的后綴 -->
            <property name="enableSubPackages" value="false" />
            <!-- 從數據庫返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="com.mybatis.mapper"   //mapper生成位置
            targetProject=".\src">
            <!-- enableSubPackages:是否讓schema作為包的后綴 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

          //需要生成的表格都需要在這里一一列出

    <!-- 指定數據庫表 -->
    <table schema="" tableName="other_User"></table>
    <table schema="" tableName="login_log"></table>

 

 

點擊運行程序后出現下面生成日志,並等待生成。

image

image image


免責聲明!

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



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