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

利用mybatis-generator自动生成代码

2016-09-20 12:39 369 查看
mybatis-generator 使用maven插件生成代码的步骤,经过测试:

开发工具:myeclipse2014

jdk1.6

一、在pom.xml中添加plugin

<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/generator/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>

二、generatorConfig.xml配置文件

<generatorConfiguration>
<classPathEntry location="D:/03works/ZYWSPT/tj/ZYWS20160908/cn.qazit.app/src/main/generator/ojdbc14-10.2.0.2.jar" />
<context id="MBG" targetRuntime="MyBatis3" defaultModelType="conditional"> <!--targetRuntime 此属性用于指定生成的代码的运行目标。 -->
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
<property name="searchString" value="Example$" />
<property name="replaceString" value="Criteria" />
</plugin>
<commentGenerator>
<property name="suppressAllComments" value="false" />
<property name="suppressDate" value="true" />
</commentGenerator>

<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@192.168.0.9:1521:orcl" userId="***" password="***"/>

<javaTypeResolver>
<property name="forceBigDecimals" value="false" /> <!-- 类型解析器 -->
</javaTypeResolver>

<javaModelGenerator targetPackage="cn.qazit.app.core.charge.model" targetProject="D:/03works/ZYWSPT/tj/ZYWS20160908/cn.qazit.app/src/main/java"> <!-- 实体类 -->
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>

<sqlMapGenerator targetPackage="cn.qazit.app.core.charge.persistence" targetProject="D:/03works/ZYWSPT/tj/ZYWS20160908/cn.qazit.app/src/main/resources"> <!-- 实体类SQL映射文件 -->
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>

<javaClientGenerator type="XMLMAPPER" targetPackage="cn.qazit.app.core.charge.persistence" targetProject="D:/03works/ZYWSPT/tj/ZYWS20160908/cn.qazit.app/src/main/java">
<property name="enableSubPackages" value="true" /> <!-- 接口 -->
</javaClientGenerator>

<table tableName="ZYWSPT-ZYWHPJB" domainObjectName="ZywsptZywhpjb" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="true"></table>

配置完成后,选择项目,右键:run as->Goals mybatis-generator:generate,

如果运行报错,提示

No plugin found for prefix 'mybatis-generator' 等等,

请先使用:

run as -> maven clear

run as ->maven install

run as -> Goals mybatis-generator:generate

完成,参考了网上资料,在此谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MyBatis