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

Intellij IDEA 14中maven下生成利用mybatis-generator自动生成代码

2016-09-20 12:36 711 查看
摘要: Intellij IDEA 14中maven下生成利用mybatis-generator自动生成代码(逆向数据库)

mybatis-generator 是mybatis框架下逆向生成dao层、mapper等的神器,和hibernate的逆向非常相似,但相比起来却要简单很多,不过新手总是不知所措,本文主要是针对新手配置mybatis-generator的使用。

第一步,将mybatis-generator 加入到pom文件

<!--配置其他组件-->
<build>
<plugins>

<!-- 配置Tomcat插件,聚合工程需要将其他的东西一起聚合在一起才能运行,所以tomcat放在这 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

<!--这是一个mybatis逆向生成工具,用于逆向生成实体-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
</plugin>

</plugins>
</build>
这里我将我的具体pom放到上面供大家参考,最关键的其实就是加入

<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
</plugin>
即可,但很多新手会出现直接放到
<dependencies> 标签中,可能出现问题,注意是放在build 标签中。

这个时候如果没有下载下来,需要去手动刷新一下maven。

最关键的一步就是它的配置文件,按照我下面的修改即可,使用绝对路径并不好,但为了减少错误,我下面暂时使用的是绝对路径。

<?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:/kaiFa/repository/mysql/mysql-connector-java/5.1.37/mysql-connector-java-5.1.37.jar"/>
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>

<!--数据库相关设置-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/sprout"
userId="root"
password="root"/>

<!--实体放置目录-->
<javaModelGenerator targetPackage="com.sprout.pojo"
targetProject="G:/kaiFa/IDEATest/sprout/src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<!--mybatis映射xml文件放置位置-->
<sqlMapGenerator targetPackage="com.sprout.mapper.impl.mybatis.xml"
targetProject="G:/kaiFa/IDEATest/sprout/src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<!--mapper接口-->
<javaClientGenerator targetPackage="com.sprout.mapper"
targetProject="G:/kaiFa/IDEATest/sprout/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="user" domainObjectName="user"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!--<columnRenamingRule searchString="^D_"
replaceString=""/>-->
</table>
<table tableName="book" domainObjectName="book"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!--<columnRenamingRule searchString="^D_"
replaceString=""/>-->
</table>
<table tableName="authority" domainObjectName="authority"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!--<columnRenamingRule searchString="^D_"
replaceString=""/>-->
</table>
<table tableName="booktype" domainObjectName="booktype"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!--<columnRenamingRule searchString="^D_"
replaceString=""/>-->
</table>
<table tableName="collect" domainObjectName="collect"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!--<columnRenamingRule searchString="^D_"
replaceString=""/>-->
</table>
<table tableName="down" domainObjectName="down"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!--<columnRenamingRule searchString="^D_"
replaceString=""/>-->
</table>
<table tableName="role" domainObjectName="role"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!--<columnRenamingRule searchString="^D_"
replaceString=""/>-->
</table>
<table tableName="upload" domainObjectName="upload"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!--<columnRenamingRule searchString="^D_"
replaceString=""/>-->
</table>

</context>
</generatorConfiguration>


最后一步:运行

可以使用mvn mybatis-generator:generate 这个命令运行,但如果在idea下面的话,直接如下即可运行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis-generator idea