MyBatis逆向工程

來源:互聯網
上載者:User

標籤:代碼   技術分享   dtd   desc   org   返回   res   for   使用者   

  MyBatis的一個主要的特點是需要自己編寫Sql,但是如果表很多的話,難免會很麻煩,所以mybatis官方提供了一個逆向工程,可以針對單表自動產生mybatis執行所需要的代碼(包括mapper.xml, mapper.java, po), 一般在開發中,常用的逆向工程方式是通過資料庫的表生存代碼。

      建立一個java工程,這個工程專門用來使用逆向工程產生代碼的,這樣做的目的是為了防止產生相同的檔案覆蓋了原來的檔案,所以單獨建立工程,然後組建檔案,再通過拷貝到自己的工程中,這也不麻煩,而且很安全。

      工程的目錄如下:

逆向工程的設定檔的名稱可以隨便起,只要放在工程的根目錄下,在主程式中進行指定檔案名稱匯入,設定檔的內容如下:

<?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="testTables" targetRuntime="MyBatis3">        <commentGenerator>            <!-- 是否去除自動產生的注釋 true:是 : false:否 -->            <property name="suppressAllComments" value="true" />        </commentGenerator>        <!--資料庫連接的資訊:驅動類、串連地址、使用者名稱、密碼 -->        <jdbcConnection driverClass="com.mysql.jdbc.Driver"            connectionURL="jdbc:mysql://localhost:3306/ego" userId="root"            password="root">        </jdbcConnection>        <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"            connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"             userId="yycg"            password="yycg">        </jdbcConnection> -->        <!-- 預設false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer,為 true時把JDBC DECIMAL 和             NUMERIC 類型解析為java.math.BigDecimal -->        <javaTypeResolver>            <property name="forceBigDecimals" value="false" />        </javaTypeResolver>        <!-- targetProject:產生PO類的位置 -->        <javaModelGenerator targetPackage="com.fd.mybatis.po"            targetProject=".\src">            <!-- enableSubPackages:是否讓schema作為包的尾碼 -->            <property name="enableSubPackages" value="false" />            <!-- 從資料庫返回的值被清理前後的空格 -->            <property name="trimStrings" value="true" />        </javaModelGenerator>        <!-- targetProject:mapper對應檔產生的位置 -->        <sqlMapGenerator targetPackage="com.fd.mybatis.mapper"             targetProject=".\src">            <!-- enableSubPackages:是否讓schema作為包的尾碼 -->            <property name="enableSubPackages" value="false" />        </sqlMapGenerator>        <!-- targetPackage:mapper介面產生的位置 -->        <javaClientGenerator type="XMLMAPPER"            targetPackage="com.fd.mybatis.mapper"             targetProject=".\src">            <!-- enableSubPackages:是否讓schema作為包的尾碼 -->            <property name="enableSubPackages" value="false" />        </javaClientGenerator>        <!-- 指定資料庫表 -->        <table schema="" tableName="tb_content"></table>        <table schema="" tableName="tb_content_category"></table>        <table schema="" tableName="tb_item"></table>        <table schema="" tableName="tb_item_cat"></table>        <table schema="" tableName="tb_item_desc"></table>        <table schema="" tableName="tb_item_param"></table>        <table schema="" tableName="tb_item_param_item"></table>        <table schema="" tableName="tb_order"></table>        <table schema="" tableName="tb_order_item"></table>        <table schema="" tableName="tb_order_shipping"></table>        <table schema="" tableName="tb_user"></table>        <!-- 有些表的欄位需要指定java類型         <table schema="" tableName="">            <columnOverride column="" javaType="" />        </table> -->    </context></generatorConfiguration>

  設定檔的主要內容為:

1. 串連資料庫

2. 指定要產生代碼的位置

3. 指定資料庫中想要產生哪些表

 

主要代碼如下:

package com.fd.mybatis.generatorsqlmap;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;//指定 逆向工程設定檔//InputStream input =("generatorConfig.xml");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) {try {GeneratorSqlMap generatorSqlMap = new GeneratorSqlMap();generatorSqlMap.generator();} catch (Exception e) {e.printStackTrace();}}}

  

 源碼連結 https://pan.baidu.com/s/1DzJJ5wqEx8AGmEAjYPeyjQ

MyBatis逆向工程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.