一、核心文件generator.xml
指定數據庫jar包位置、數據庫連接信息、生成包的位置、表名等關鍵信息。該文件放在任意位置。
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
|
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration> <!-- 數據庫的JDBC驅動的jar包地址 --> <classPathEntry location= "F:\xy\jars\mysql-connector-java-5.0.7-bin.jar" /> <context id= "DB2Tables" targetRuntime= "MyBatis3" > <!-- 是否去除自動生成的注釋 --> <commentGenerator> <property name= "suppressAllComments" value= "true" /> </commentGenerator> <!-- 數據庫連接的信息 --> <jdbcConnection driverClass= "com.mysql.jdbc.Driver" connectionURL= "jdbc:mysql://localhost:3306/db_MybatisTest" userId= "root" password= "mysqltest" > </jdbcConnection> <!-- false :JDBC DECIMAL、NUMERIC類型解析為Integer,默認方式 --> <!-- true : JDBC DECIMAL、NUMERIC類型解析為java.math.BigDecimal --> <javaTypeResolver> <property name= "forceBigDecimals" value= "false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage= "com.xy.model" targetProject= "F:\xy\mybatis-generator\src" > <!-- 是否讓schema作為包的后綴 --> <property name= "enableSubPackages" value= "true" /> <!-- 從數據庫返回的值被清理前后的空格 --> <property name= "trimStrings" value= "true" /> </javaModelGenerator> <!-- 生成映射文件的包名和位置 --> <sqlMapGenerator targetPackage= "com.xy.mapping" targetProject= "F:\xy\mybatis-generator\src" > <property name= "enableSubPackages" value= "false" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type= "XMLMAPPER" targetPackage= "com.xy.dao" targetProject= "F:\xy\mybatis-generator\src" > <property name= "enableSubPackages" value= "true" /> </javaClientGenerator> <!-- tableName:數據庫表 --> <!-- domainObjectName:對應于數據庫表的javaBean類名 --> <table tableName= "t_student" domainObjectName= "Student" enableCountByExample= "false" enableUpdateByExample= "false" enableDeleteByExample= "false" enableSelectByExample= "false" selectByExampleQueryId= "false" > <!-- 忽略該字段(可省略) --> <ignoreColumn column= "name" /> </table> </context> </generatorConfiguration> |
二、table標簽解析
①屬性
schema即為數據庫名,tableName為對應的數據庫表,domainObjectName是要生成的實體類。
若要生成例子可將enableCountByExample等設為true, 就會生成一個對應domainObjectName的Example類,false則不生成,默認策略是true。
類似的還有enableUpdateByExample、enableDeleteByExample、enableSelectByExample、selectByExampleQueryId屬性。
②子標簽
若要對某些數據庫字段進行操作,可以在table標簽中加入如下標簽
1、忽略某個字段
1
|
<ignoreColumn column= "name" /> |
2、無論數據庫字段是何類型,生成的類屬性都是varchar
1
|
<columnOverride column= "LONG_VARCHAR_FIELD" jdbcType= "VARCHAR" /> |
三、生成
mybatis-generator-core-1.3.2.jar是核心jar包,可在網上自行下載。命令窗口執行語句,執行成功后就會在generator.xml文件中指定的位置找到代碼了。
1
|
java -jar F:\xy\jars\mybatis-generator-core- 1.3 . 2 .jar -configfile F:\xy\generator.xml -overwrite |
四、總結
使用Mybatis Generator需要
①兩個jar包——mybatis-generator-core-1.3.2.jar和數據庫jar包
②一個配置文件generator.xml
③執行語句
五、注意事項
①generator.xml格式:必須是以UTF-8無BOM格式編碼,用notepad++轉換。
②注意數據庫包的可用性,無效的數據庫包轉換會報錯。
以上所述是小編給大家介紹的Mybatis generator的使用全面解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/woshixuye/article/details/29220123