然而并没有什么东西┑( ̄Д  ̄)┍

0%

[MyBatis]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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?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>
<!-- classPathEntry:数据库的JDBC驱动的jar包地址-->
<classPathEntry location="mysql-connector-java-5.1.31-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--
org.mybatis.generator.plugins.EqualsHashCodePlugin
很明显,这个插件用来给Java模型生成equals和hashcode方法;注意下,如果Domain类有rootClass,需要重新处理下生成的代码(因为MBG是不会考虑rootClass相关内容的,甚至都不会加载rootClass,可以参考配置文件中rootClass相关说明);
-->
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
<!--
org.mybatis.generator.plugins.RenameExampleClassPlugin
这个插件可以使用正则表达式的方式来重命名生成的XXXExample类,通过配置 searchString和replaceString属性来完成(这个实现原理请参考MBG配置文件中的columnRenamingRule元素),一个例子,比如要xxxExample改成xxxCriteria,只需要配置:
<property name="searchString" value="Example$" />
<property name="replaceString" value="Criteria" />
-->
<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"/>
<!--
org.mybatis.generator.plugins.SerializablePlugin
这个插件主要用来为生成的Java模型类添加序列化接口,并生成serialVersionUID字段;有两个配置项:

addGWTInterface:这个是针对GWT的,我们忽略;
suppressJavaInterface:这个参数是必须要填的,我们设置为false就可以了;
-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!--
org.mybatis.generator.plugins.CaseInsensitiveLikePlugin
这个插件用来在XXXExample类中生成大小写敏感的LIKE方法(插件本身用处不大,但是我们可以通过这个插件学习给XXXExample类添加额外的方法)
-->
<plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin">
<property name="searchString" value="Example$"/>
<property name="replaceString" value="Critera"/>
</plugin>
<!--
org.mybatis.generator.plugins.MapperConfigPlugin
比较有用的一个插件,可以用来帮助生成一个默认的MapperConfig.xml文件骨架,在这个骨架文件中完成了本次生成的mapper.xml文件的配置;该插件支持的配置属性有:

fileName:配置文件名称,默认为MapperConfig.xml;
targetPackage:配置文件所在的包,同MBG配置文件中的所有targetPackage配置;
targetProject:配置文件所在目录,同MBG配置文件中的所有targetProject配置;
-->
<plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">
<property name="targetPackage" value="config"/>
<property name="targetProject" value="Project/resource"/>
</plugin>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<commentGenerator>
<property name="suppressDate" value="true"/>

<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="123456"> </jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- targetProject:自动生成代码的位置 -->
<javaModelGenerator targetPackage="ProjectName.Dao.Model" targetProject="Project/src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<sqlMapGenerator targetPackage="ProjectName.Dao.XML" targetProject="Project/src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<javaClientGenerator type="XMLMAPPER" targetPackage="ProjectName.Dao.Mapper" targetProject="Project/src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
<!--
table其他属性:
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类,
如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true,
这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时,
就不会生成对应的Example类了.

如果table里边不配置property,默认字段都生成为类属性。
<ignoreColumn column="FRED" />//忽略字段
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//无论字段是什么类型,生成的类属性都是varchar。
-->
<table tableName="user" domainObjectName="user">
<property name="useActualColumnNames" value="true" />
<!--
constructorBased:和<javaModelGenerator>中的属性含义一样。
ignoreQualifiersAtRuntime:生成的SQL中的表名将不会包含schema和catalog前缀。
immutable:和<javaModelGenerator>中的属性含义一样。
modelOnly:此属性用于配置是否为表只生成实体类。如果设置为true就不会有Mapper接口。如果配置了<sqlMapGenerator>,并且modelOnly为true,那么XML映射文件中只有实体对象的映射元素(<resultMap>)。如果为true还会覆盖属性中的enableXXX方法,将不会生成任何CRUD方法。
rootClass:和<javaModelGenerator>中的属性含义一样。
rootInterface:和<javaClientGenerator>中的属性含义一样。
runtimeCatalog:运行时的catalog,当生成表和运行环境的表的catalog不一样的时候可以使用该属性进行配置。
runtimeSchema:运行时的schema,当生成表和运行环境的表的schema不一样的时候可以使用该属性进行配置。
runtimeTableName:运行时的tableName,当生成表和运行环境的表的tableName不一样的时候可以使用该属性进行配置。
selectAllOrderByClause:该属性值会追加到selectAll方法后的SQL中,会直接跟order by拼接后添加到SQL末尾。
useActualColumnNames:如果设置为true,那么MBG会使用从数据库元数据获取的列名作为生成的实体对象的属性。 如果为false(默认值),MGB将会尝试将返回的名称转换为驼峰形式。 在这两种情况下,可以通过 元素显示指定,在这种情况下将会忽略这个(useActualColumnNames)属性。
useColumnIndexes:如果是true,MBG生成resultMaps的时候会使用列的索引,而不是结果中列名的顺序。
useCompoundPropertyNames:如果是true,那么MBG生成属性名的时候会将列名和列备注接起来. 这对于那些通过第四代语言自动生成列(例如:FLD22237),但是备注包含有用信息(例如:"customer id")的数据库来说很有用. 在这种情况下,MBG会生成属性名FLD2237_CustomerId。
-->
</table>


</context>
</generatorConfiguration>