天天看點

mybatis 詳解(十)------ 逆向工程

通過前面的學習,在實際開發中,我們基本上能對mybatis應用自如了,但是我們發現了一個問題,所有操作都是圍繞着po類,xxxMapper.xml檔案,xxxMapper接口等檔案來進行的。如果實際開發中資料庫的表特别多,那麼我們需要手動去寫每一張表的po類,xxxMapper.xml,xxxMapper.java檔案,這顯然需要花費巨大的精力,而且可能由于表字段太多,寫錯了而不知道也是可能的。

  是以我們在實際開發中,一般使用逆向工程方式來自動生成所需的檔案。

  本篇部落格源碼下載下傳連結:http://pan.baidu.com/s/1nvvA68L 密碼:jc1p

①、建立一個工程,并導入相應的jar包(詳情見上面源碼)

  

mybatis 詳解(十)------ 逆向工程

  注意:使用逆向工程時,最好建立一個工程,如果你在原來的工程中使用,那也可以,但是有一定的風險,因為mybatis是根據配置檔案中配置的路徑來生成的檔案的,如果你工程中有相同名字的檔案,那麼就會被新生成的檔案所覆寫。是以實際開發中,我們一般建立一個工程,将生成的檔案複制到自己的所需的工程中。

②、建立配置檔案 generatorConfig.xml 檔案

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

<?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/mybatisrelation"

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.ys.po"

targetProject=

".\src"

>

<!-- enableSubPackages:是否讓schema作為包的字尾 -->

<property name=

"enableSubPackages"

value=

"false"

/>

<!-- 從資料庫傳回的值被清理前後的空格 -->

<property name=

"trimStrings"

value=

"true"

/>

</javaModelGenerator>

<!-- targetProject:mapper映射檔案生成的位置,重要!! -->

<sqlMapGenerator targetPackage=

"com.ys.mapper"

targetProject=

".\src"

>

<property name=

"enableSubPackages"

value=

"false"

/>

</sqlMapGenerator>

<!-- targetPackage:mapper接口生成的位置,重要!! -->

<javaClientGenerator type=

"XMLMAPPER"

targetPackage=

"com.ys.mapper"

targetProject=

".\src"

>

<property name=

"enableSubPackages"

value=

"false"

/>

</javaClientGenerator>

<!-- 指定資料庫表,要生成哪些表,就寫哪些表,要和資料庫中對應,不能寫錯! -->

<table tableName=

"items"

></table>

<table tableName=

"orders"

></table>

<table tableName=

"orderdetail"

></table>

<table tableName=

"user"

></table>       

</context>

</generatorConfiguration>

  注意:

  1、連接配接資料庫的配置,包括資料名稱,資料庫使用者名密碼等配置

  2、指定要生成代碼的包名,包括實體類po的包名,mapper的包名等

  3、指定資料庫中哪些表需要生成檔案

③、運作主程式生成代碼

package

com.ys.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

GeneratorTest {

public

void

testGenerator()

throws

Exception{

List<String> warnings =

new

ArrayList<String>();

boolean

overwrite =

true

;

//指向逆向工程配置檔案

File configFile =

new

File(GeneratorTest.

class

.getResource(

"/generatorConfig.xml"

).getFile());

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

{

GeneratorTest generator =

new

GeneratorTest();

generator.testGenerator();

}

catch

(Exception e) {

e.printStackTrace();

}

}

}

  直接運作上面的程式,控制台會列印如下代碼,說明生成代碼成功

mybatis 詳解(十)------ 逆向工程

  然後重新整理generatorConfig.xml 檔案中指定的包,會發現生成了如下檔案

mybatis 詳解(十)------ 逆向工程

繼續閱讀