MyBatis(10)逆向工程
阿新 • • 發佈:2018-12-17
什麼是逆向工程?
在學習的過程中會發現,需要我們寫大量的sql語句
此時mybaatis官方為我們提供逆向工程可以針對單表自動生成的mybatis執行所需要的程式碼
使用方法:
MyBatis Generator (MBG) can be run in the following ways:
- From the command prompt with an XML configuration
- As an Ant task with an XML configuration
-
From another Java program with an XML configuration
- From another Java program with a Java based configuration
官方文件在解壓後的doc檔案中點選index.html
簡單的講解一下:
MyBatis GeneratorXML Configuration File Reference
建立新的工程: 匯入需要加入的jar 在config檔案下的 gengeratorConfig.xml 基本上都是固定的格式,不需要懂,只許看看懂紅色部分的註釋,以及相關標籤的意義。<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLICtest目錄下 GeneratorSqlmap.java"-//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:3307/shopping" userId="root" password="1234"> </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.MrChengs.po" targetProject=".\src"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> <!-- 從資料庫返回的值被清理前後的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper對映檔案生成的位置 --> <sqlMapGenerator targetPackage="com.MrChengs.mapper" targetProject=".\src"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper介面生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.MrChengs.mapper" targetProject=".\src"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定資料庫表 --> <table tableName="items"></table> <table tableName="orders"></table> <table tableName="orderdetail"></table> <table tableName="user"></table> </context> </generatorConfiguration>
public class GeneratorSqlmap { public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置檔案 File configFile = new File("config/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(); } } }都是固定的格式,固體的可以參考官方的文件。 然後執行在 簡單說明一下 如下的註釋 首先是表 其次是表中的欄位
2018-10-25 16:04:22,837 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "items" 2018-10-25 16:04:22,848 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "shopping..items" 2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "name", data type 12, in table "shopping..items" 2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "price", data type 7, in table "shopping..items" 2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "detail", data type -1, in table "shopping..items" 2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "pic", data type 12, in table "shopping..items" 2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "createtime", data type 93, in table "shopping..items" 2018-10-25 16:04:22,855 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "orders" 2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "shopping..orders" 2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "user_id", data type 4, in table "shopping..orders" 2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "number", data type 12, in table "shopping..orders" 2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "createtime", data type 93, in table "shopping..orders" 2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "note", data type 12, in table "shopping..orders" 2018-10-25 16:04:22,864 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "orderdetail" 2018-10-25 16:04:22,868 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "shopping..orderdetail" 2018-10-25 16:04:22,868 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "orders_id", data type 4, in table "shopping..orderdetail" 2018-10-25 16:04:22,868 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "items_id", data type 4, in table "shopping..orderdetail" 2018-10-25 16:04:22,868 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "items_num", data type 4, in table "shopping..orderdetail" 2018-10-25 16:04:22,869 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "user" 2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "shopping..user" 2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "username", data type 12, in table "shopping..user" 2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "birthday", data type 91, in table "shopping..user" 2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "sex", data type 1, in table "shopping..user" 2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "address", data type 12, in table "shopping..user"重新整理觀察 com.MrChengs.po com.MrCehngs.mapper 兩個包裡面的檔案 此時的檔案我們得到了,在使用的時候我們不要進行對原有的程式碼的修改。 返回上一個博文將進行簡單的小案例測試:
複製圖中的檔案到本工程中
新建測試類public class test { private ApplicationContext applicationContext; public ApplicationContext getApplication(){ applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); return applicationContext; } //根據主鍵id進行查詢 @Test public void test() { ItemsMapper items = (ItemsMapper) getApplication().getBean("itemsMapper"); Items it = items.selectByPrimaryKey(1); System.out.println(it.toString()); } //自定義查詢條件 @Test public void test1() { ItemsMapper itemsMapperems = (ItemsMapper) getApplication().getBean("itemsMapper"); ItemsExample itemsExample = new ItemsExample(); //通過criteria構造條件查詢 ItemsExample.Criteria criteria = itemsExample.createCriteria(); criteria.andNameEqualTo("筆記本"); //可能返回多個記錄 List<Items> items = itemsMapperems.selectByExample(itemsExample); System.out.println(items); } }均可以測試成功!! 對於其他的方法,在使用的時候,可以參考自動生成的原始碼進行測試,都是可以測試成功的! 逆向工程的使用極大的方便我們進行開發,作為程式設計師不需要過多的關注這個,我們進行了解基本的使用和用法即可。 其餘的增刪改除,可以進行測試,在不會使用的情況下,可以參考原始碼,原始碼是之前的前幾章節的內容.