MyBatis(10)逆向工程


什么是逆向工程?
在學習的過程中會發現,需要我們寫大量的sql語句
此時mybaatis官方為我們提供逆向工程可以針對單表自動生成的mybatis執行所需要的代碼
 
 
使用方法:
   MyBatis Generator (MBG) can be run in the following ways:
 
推薦使用紅色的方法
 
jar包

 

官方文檔在解壓后的doc文件中點擊index.html

 

簡單的講解一下:

MyBatis GeneratorXML Configuration File Reference

 

 
建立新的工程:
導入需要加入的jar

 

 
在config文件下的
gengeratorConfig.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="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>

 

test目錄下
GeneratorSqlmap.java
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
兩個包里面的文件
 
此時的文件我們得到了,在使用的時候我們不要進行對原有的代碼的修改。
 
 
 
 
 返回上一個博文將進行簡單的小案例測試:
有不懂的可以參考: MyBatis整合Spring
 

復制圖中的文件到本工程中

 
 
 
 新建測試類
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);
           }
}

 

 
均可以測試成功!!
 
對於其他的方法,在使用的時候,可以參考自動生成的源碼進行測試,都是可以測試成功的!
 
 
 逆向工程的使用極大的方便我們進行開發,作為程序員不需要過多的關注這個,我們進行了解基本的使用和用法即可。
其余的增刪改除,可以進行測試,在不會使用的情況下,可以參考源碼,源碼是之前的前幾章節的內容.
 
 
 
 
 
 
 
 
 
 


免責聲明!

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



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