關于MyBatis:
MyBatis Generator (MBG) 是一個Mybatis的代碼生成器 MyBatis 和 iBATIS. 他可以生成Mybatis各個版本的代碼,和iBATIS 2.2.0版本以后的代碼。 他可以內省數據庫的表(或多個表)然后生成可以用來訪問(多個)表的基礎對象。 這樣和數據庫表進行交互時不需要創建對象和配置文件。 MBG的解決了對數據庫操作有最大影響的一些簡單的CRUD(插入,查詢,更新,刪除)操作。
準備工作:
下載MyBatis-Generator 點擊此處下載
下載成功以后 如下圖
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
54
55
56
57
58
59
60
61
|
<? 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 > <!-- 在此處修改數據庫的驅動包 必須提前將驅動包放到本配置文件的同級目錄下 筆者已提前放好 如使用Oracle數據庫時 <classPathEntry location="oracle.jar" /> --> < classPathEntry location = "mysql.jar" /> < context id = "DB2Tables" targetRuntime = "MyBatis3" > < commentGenerator > < property name = "suppressAllComments" value = "true" /> <!-- 是否取消注釋 --> < property name = "suppressDate" value = "true" /> <!-- 是否生成注釋代時間戳 --> </ commentGenerator > <!-- 此處修改數據庫的連接信息 --> < jdbcConnection driverClass = "com.mysql.jdbc.Driver" connectionURL = "jdbc:mysql://localhost:3306/easybuy" userId = "root" password = "pengxiongpengdi" /> < javaTypeResolver > < property name = "forceBigDecimals" value = "false" /> </ javaTypeResolver > <!-- 要生成的實體類 每個項目包的命名 都不一樣 可以通過修改 該屬性 實現 targetPackage="com.buy.entity" --> < javaModelGenerator targetPackage = "com.buy.entity" targetProject = "src" > < property name = "enableSubPackages" value = "true" /> < property name = "trimStrings" value = "true" /> </ javaModelGenerator > <!-- 要生成的接口 --> < sqlMapGenerator targetPackage = "com.buy.dao" targetProject = "src" > < property name = "enableSubPackages" value = "true" /> </ sqlMapGenerator > <!-- 要生成的映射文件 --> < javaClientGenerator type = "XMLMAPPER" targetPackage = "com.buy.dao" targetProject = "src" > < property name = "enableSubPackages" value = "true" /> </ javaClientGenerator > <!-- 配置要映射的表 數據庫中對應的表: tableName="EASYBUY_PRODUCT" 項目中實體類的名字: domainObjectName="ProductEntity" 其他屬性默認即可 --> < table tableName = "EASYBUY_PRODUCT" domainObjectName = "ProductEntity" enableCountByExample = "false" enableUpdateByExample = "false" enableDeleteByExample = "false" enableSelectByExample = "false" selectByExampleQueryId = "false" ></ table > < table tableName = "EASYBUY_PRODUCT_CATEGORY" domainObjectName = "CategoryEntity" enableCountByExample = "false" enableUpdateByExample = "false" enableDeleteByExample = "false" enableSelectByExample = "false" selectByExampleQueryId = "false" ></ table > < table tableName = "EASYBUY_USER" domainObjectName = "UserEntity" enableCountByExample = "false" enableUpdateByExample = "false" enableDeleteByExample = "false" enableSelectByExample = "false" selectByExampleQueryId = "false" ></ table > </ context > </ generatorConfiguration > |
配置好以后運行go.cmd src目錄下就會生成 對應的接口、映射文件和實體類
此時就生成完畢了可以在此基礎上添加其他功能
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://www.cnblogs.com/hnlictmso/p/6308664.html