本文介绍下如何在idea中利用Maven工具逆向生成mybatis代码
1. 在maven中配置pom.xml文件
在pom.xml的中加入如下插件:
1 2 3 4 5 6 7 8 9 10
| <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>
|
配置好maven插件后,进行下一步。
2. 添加逆向工程的配置文件
在resources目录下新建一个generatorConfig.xml文件,然后将如下配置文件拷贝到上面建的文件中。
maven的项目配置文件存放位置如下:

generatorConfig.xml文件:
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| <?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="F:\.m2\repository\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar" />
<context id="default" targetRuntime="MyBatis3">
<commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/cms" userId="root" password="sky"> </jdbcConnection>
<javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver>
<javaModelGenerator targetPackage="com.spring.cms.model.vo" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/> <property name="constructorBased" value="true"/> <property name="trimStrings" value="true"/> <property name="immutable" value="false"/> </javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator>
<javaClientGenerator targetPackage="com.spring.cms.dao" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator>
<table tableName="cc_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table>
<table tableName="cc_user_role" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table>
<table tableName="cc_role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table>
<table tableName="cc_role_resource" domainObjectName="RoleResource" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table>
<table tableName="cc_resource" domainObjectName="Resource" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
|
jdbc.properties配置文件的代码如下:
1 2 3 4 5 6 7 8 9 10
| driverClassName=com.mysql.jdbc.Driver validationQuery=SELECT 1 jdbc_url=jdbc:mysql://localhost:3306/contentmanagersystem_db?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull jdbc_username=root jdbc_password=sky jdbc.mysql-master.initialSize=5 jdbc.mysql-master.maxActive=5 jdbc.mysql-master.maxIdle=5 jdbc.mysql-master.minIdle=1 jdbc.mysql-master.maxWait=6000
|
3. 在IDEA中利用插件生成代码
选择IDEA右上角下拉框中的Edit Configurations,然后进入到配置页面,选择做上角的 + 号,如图:

选择maven,然后在Commond line栏填上如图命令

点击应用。最后点击启动按钮,即可自动生成代码。

生成成功!