网上很多关于Mabatis-generator自动生成mabatis所需要的数据库表映射配置以及bean和dao等的用法,大致分为这几种:
1.eclipse插件生成、
2.用jar包生成、
3.用maven插件+Mabatis-generator生成,这里只介绍最后一种用法。
1. 配置Maven pom.xml 文件
在pom.xml增加以下插件:
<build>
</plugins>
...
<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>
...
</plugins>
</build>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
配置好Maven插件,下面需要配置插件需要mybatis-generator的配置文件
2. 配置mybatis-generator的配置文件
添加配置文件到resource下:
generatorConfig.xml的内容:
<?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
location="E:/maven/mvn_repository/com/oracle/ojdbc14/14/ojdbc14-14.jar" />
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false" />
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@192.168.1.112:1521:orcl" userId="KY"
password="KY" />
<javaModelGenerator targetPackage="com.dg.bean"
targetProject="E:/MyWorkspace/ssmdemo/src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.dg.mapping"
targetProject="E:/MyWorkspace/ssmdemo/src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.dg.dao"
targetProject="E:/MyWorkspace/ssmdemo/src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="ORDER_INFO" domainObjectName="OrderInfoBean"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
- 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
- 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
3. 生成代码
在eclipse 中,选择pom.xml文件,击右键先择Run AS——>Maven Build… ——>在Goals框中输入:mybatis-generator:generate,maven会先下载插件,然后生成代码。
看效果:
然后,按照我们的习惯将生成的文件名字改掉即可(注意xml文件里的namespace别忘改)。