工作知識點,mybaties逆向產生pojo和mapper

來源:互聯網
上載者:User

標籤:知識點   mode   new   over   解析   尾碼   pac   led   util   

節省時間寫javabean和mapper等xml檔案。這個需要手寫java方法和對應的xml,之後運行方法,獲得需要的結果。

建立一個web項目,在web項目的lib包中添加逆向工程需要的jar包

建立generatorConfig.xml檔案

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfigurationPUBLIC "-//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>            <!-- 是否去除自動產生的注釋 true:是 : false:否 -->            <property name="suppressAllComments" value="true" />        </commentGenerator>        <!--資料庫連接的資訊:驅動類、串連地址、使用者名稱、密碼 -->        <!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis"             userId="root" password="1234"> </jdbcConnection> -->        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"            connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:FASPGPORCL" userId="cs_qinghai"            password="1">        </jdbcConnection>        <!-- 預設false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer,為 true時把JDBC DECIMAL             和 NUMERIC 類型解析為java.math.BigDecimal -->        <javaTypeResolver>            <property name="forceBigDecimals" value="false" />        </javaTypeResolver>        <!-- targetProject:產生PO類的位置 -->        <javaModelGenerator targetPackage="cn.edu.hpu.ssm.po"            targetProject=".\src">            <!-- enableSubPackages:是否讓schema作為包的尾碼 -->            <property name="enableSubPackages" value="false" />            <!-- 從資料庫返回的值被清理前後的空格 -->            <property name="trimStrings" value="true" />        </javaModelGenerator>        <!-- targetProject:mapper對應檔產生的位置 -->        <sqlMapGenerator targetPackage="cn.edu.hpu.ssm.mapper"            targetProject=".\src">            <!-- enableSubPackages:是否讓schema作為包的尾碼 -->            <property name="enableSubPackages" value="false" />        </sqlMapGenerator>        <!-- targetPackage:mapper介面產生的位置 -->        <javaClientGenerator type="XMLMAPPER"            targetPackage="cn.edu.hpu.ssm.mapper" targetProject=".\src">            <!-- enableSubPackages:是否讓schema作為包的尾碼 -->            <property name="enableSubPackages" value="false" />        </javaClientGenerator>        <!-- 指定資料庫表 -->        <table tableName="gp_contract_pay"></table>        <table tableName="gp_contract"></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> -->        <!-- 有些表的欄位需要指定java類型 <table schema="" tableName=""> <columnOverride column=""             javaType="" /> </table> -->    </context></generatorConfiguration>

編寫java方法,來讀取generatorConfig.xml檔案,直接將xml檔案放到根目錄下

寫java類

package test;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 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();        }    }}

運行java中的main方法,等待一段時間,然後重新整理項目,就會看到產生的檔案

把需要的檔案粘到自己的項目中即可。

 

工作知識點,mybaties逆向產生pojo和mapper

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.