之前一直在使用 Hibernate 和 JPA,但是为了找工作不得不强行上Mybatis。为了避免每次都到处找配置,还是老方法记录一份模板。

image.png

首先是 Mybatis generator 依赖库插件,放在 pombuild/plugins 标签下

1
2
3
4
5
6
7
8
9
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>

然后添加 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
<?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>

<properties resource="datasource.properties"/>
<!-- !!!! Driver Class Path !!!! 目前最好使用5.1.6版本的驱动,比较稳定 -->
<classPathEntry location="***********\.m2\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar"/>

<context id="context" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="true"/>
</commentGenerator>

<!-- !!!! Database Configurations !!!! -->
<jdbcConnection driverClass="${db.driverClassName}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}"/>

<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>

<!-- !!!! Model Configurations !!!! -->
<javaModelGenerator targetPackage="path.to.pojo.pkg" targetProject="./src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="constructorBased" value="true"/>
<property name="trimStrings" value="true"/>
<property name="immutable" value="false"/>
</javaModelGenerator>

<!-- !!!! Mapper XML Configurations !!!! -->
<sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>

<!-- !!!! Mapper Interface Configurations !!!! -->
<javaClientGenerator targetPackage="path.to.dao.pkg" targetProject="./src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>

<!-- !!!! Table Configurations !!!! -->
<table tableName="tableName" domainObjectName="objectName" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
<table tableName="yaTableName" domainObjectName="yaObjectName" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<columnOverride column="someColumn1" jdbcType="VARCHAR" />
<columnOverride column="someColumn2" jdbcType="VARCHAR" />
</table>
</context>
</generatorConfiguration>


技术   配置      mybaits

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!