http://openwares.net/java/mabatis_autogenerator.html
Mybatis Generator可以自動生成模型實體對象POJO,mapper接口和對應的xml配置文件。
Mybatis Generator提到的模型model其實就是實體entity。
Mybatis Generator的核心就一個jar包mybatis-generator-core-1.3.2.jar,可以從命令行運行,也有相應的eclipse插件。
配置文件
Mybatis Generator需要一個配置文件來生成代碼,下面是配置文件的一個樣例:
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
|
<? xml
version = "1.0"
encoding = "UTF-8" ?> <! DOCTYPE
generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" < generatorConfiguration > < classPathEntry
location = "/path/to/WebRoot/WEB-INF/lib/postgresql-9.2-1003.jdbc4.jar"
/> < context
id = "BuildingTables"
targetRuntime = "MyBatis3" > < commentGenerator > < property
name = "suppressAllComments"
value = "true"
/> < property
name = "suppressDate"
value = "true"
/> </ commentGenerator > < jdbcConnection
driverClass = "org.postgresql.Driver" userId = "xxx" password = "xxx" > </ jdbcConnection > < javaTypeResolver
> < property
name = "forceBigDecimals"
value = "true"
/> </ javaTypeResolver > < javaModelGenerator
targetPackage = "org.xxx.xxx.entity"
targetProject = "project_name/src/main" > < property
name = "enableSubPackages"
value = "false"
/> < property
name = "trimStrings"
value = "true"
/> </ javaModelGenerator > < sqlMapGenerator
targetPackage = "org.xxx.xxx.dao"
targetProject = "project_name/src/main" > < property
name = "enableSubPackages"
value = "false"
/> </ sqlMapGenerator > < javaClientGenerator
type = "XMLMAPPER"
targetPackage = "org.xxx.xxx.dao"
targetProject = "project_name/src/main" > < property
name = "enableSubPackages"
value = "false"
/> </ javaClientGenerator > < table
schema = "base"
tableName = "tb_building"
domainObjectName = "Building"
> < property
name = "useActualColumnNames"
value = "true" /> </ table > < table
schema = "base"
tableName = "tb_floor"
domainObjectName = "Floor"
> < property
name = "useActualColumnNames"
value = "true" /> </ table > < table
schema = "base"
tableName = "tb_house"
domainObjectName = "House"
> < property
name = "useActualColumnNames"
value = "true" /> </ table > </ context > </ generatorConfiguration > |
配置文件的詳細語法見官方文檔。
運行Mybatis Generator
命令行
$ java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml
eclipse插件
在eclipse中配置新的安裝源http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/
然後安裝即可。
可以通過file->new->Mybatis->Mybatis Generator Configuration File 新建配置文件
在配置文件上右擊選擇Generate Mybatis/iBatis Artifacts產生mapper接口和實體POJO