使用Intellij IDEA在maven專案中整合mybatis-generator外掛,自動生成程式碼
阿新 • • 發佈:2018-11-09
1.在dependencies下新增:
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
2.在build下新增:
<plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--配置檔案的位置--> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins>
3.在maven專案的resources資料夾下建立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> <!-- 當前電腦的資料庫驅動程式jar包的全路徑 --> <classPathEntry location="D:\Program Files\maven\maven-repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar"/> <context id="context" targetRuntime="MyBatis3"> <!--是否在程式碼中顯示註釋--> <commentGenerator> <property name="suppressDate" value="true" /> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--資料庫連結地址賬號密碼--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/shiro" userId="root" password="admin"> </jdbcConnection> <!--...--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成pojo類存放位置 targetPackage表明要生成的檔案要存放的資料夾 targetProject表明具體路徑 比如我這裡連起來就是:D:\spring\shiroTest\src\main\java下的com.byh.shiro.pojo資料夾儲存生成的pojo檔案 下面生成xml和mapper同理 --> <javaModelGenerator targetPackage="com.byh.shiro.pojo" targetProject="D:\spring\shiroTest\src\main\java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成xml對映檔案存放位置--> <sqlMapGenerator targetPackage="mapper" targetProject="D:\spring\shiroTest\src\main\resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成mapper類存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.byh.shiro.mapper" targetProject="D:\spring\shiroTest\src\main\java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--生成對應表及類名--> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"> <property name="my.isgen.usekeys" value="true"/> <property name="useActualColumnNames" value="true"/> <generatedKey column="id" sqlStatement="JDBC"/> </table> <table tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"> <property name="my.isgen.usekeys" value="true"/> <property name="useActualColumnNames" value="true"/> <generatedKey column="id" sqlStatement="JDBC"/> </table> <table tableName="permission" domainObjectName="Permission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"> <property name="my.isgen.usekeys" value="true"/> <property name="useActualColumnNames" value="true"/> <generatedKey column="id" sqlStatement="JDBC"/> </table> <table tableName="user_role" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"> <property name="my.isgen.usekeys" value="true"/> <property name="useActualColumnNames" value="true"/> <generatedKey column="id" sqlStatement="JDBC"/> </table> <table tableName="role_permission" domainObjectName="RolePermission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"> <property name="my.isgen.usekeys" value="true"/> <property name="useActualColumnNames" value="true"/> <generatedKey column="id" sqlStatement="JDBC"/> </table> </context> </generatorConfiguration>
4.點選idea右側的MavenProjects,如果右側沒有則點選選單欄View->Tool Windows->Maven projects就可以顯示出來。
生成的太長,擷取的一部分,看到BUILD SUCCESS表明成功。
檢視對應的資料夾是否有生成的檔案,因為這裡用的是idea所以是應該build完後馬上就有檔案了。
生成完畢!!!