qq4615

导航

使用mybatis-generator工具自动生成mybatis代码

使用mybatis-generator工具自动生成mybatis代码 步骤如下:

1、引入maven  依赖,在项目pom.xml文件中添加

<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/conf/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>

2、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="D:/jdbc/mysql-connector-java-5.1.6-bin.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:@172.20.16.***:1521:CARGO" userId="***"
password="***"/> -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/world?useUnicode=true&amp;characterEncoding=UTF-8"
userId="admin"
password="123456"/>

<!-- 生成的model保存位置 -->
<javaModelGenerator targetPackage="com.hand.ssm.dto"
targetProject="D:/myproject/SpringMybatisSpringMVC-master/SpringMybatisSpringMVC-master/src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<!-- 生成的mybatis映射xml文件 -->
<sqlMapGenerator targetPackage="com.hand.ssm.dao"
targetProject="D:/myproject/SpringMybatisSpringMVC-master/SpringMybatisSpringMVC-master/src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成的mybatis dao接口 -->
<javaClientGenerator targetPackage="com.hand.ssm.dao"
targetProject="D:/myproject/SpringMybatisSpringMVC-master/SpringMybatisSpringMVC-master/src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>

<!--<table tableName="T_FEE_AGTBILL" domainObjectName="FeeAgentBill"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"/>-->

<!-- 数据库中要使用的自动生成代码的表 -->
<table tableName="city" domainObjectName="City"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!--<columnRenamingRule searchString="^D_"
replaceString=""/>-->
</table>

</context>
</generatorConfiguration>

 

3、使用maven 命令生成代码

  项目-> 右键 ->run as -> run configuration  然后选择 Maven Build

右键-new 新建一个配置,

name 中取个名称,不和已经有的重复就行。

在Base directory 是 项目的绝对路径,

生成代码的 命令  mybatis-generator:generate

然后 点击 apply 即可,代码自动生成了。

项目详细可参考 https://github.com/flyboy99/ssmtest

 

posted on 2018-05-23 14:46  圣彼得  阅读(152)  评论(0编辑  收藏  举报