您的位置:首页 > 编程语言 > Java开发

eclipse下用maven插件+Mabatis-generator生成mybatis的文件

2016-03-10 15:54 495 查看
网上很多关于Mabatis-generator自动生成mabatis所需要的数据库表映射配置以及bean和dao等的用法,大致分为这几种:eclipse插件生成、用jar包生成、用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>


配置好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>
<!-- ojdbc的jar包 -->
<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" />
<!-- model生成路径 -->
<javaModelGenerator targetPackage="com.dg.bean"
targetProject="E:/MyWorkspace/ssmdemo/src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 映射xml文件生成路径 -->
<sqlMapGenerator targetPackage="com.dg.mapping"
targetProject="E:/MyWorkspace/ssmdemo/src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 和xml文件关联的dao生成路径(生成文件名默认是***Mapper.java,习惯将其改名成 ***Dao.java) -->
<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>


3. 生成代码

在eclipse 中,选择pom.xml文件,击右键先择Run AS——>Maven Build… ——>在Goals框中输入:mybatis-generator:generate,maven会先下载插件,然后生成代码。



看效果:



然后,按照我们的习惯将生成的文件名字改掉即可(注意xml文件里的namespace别忘改)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  eclipse mybatis maven