1. 程式人生 > >Mybatis 自動生成程式碼外掛&IDEA外掛

Mybatis 自動生成程式碼外掛&IDEA外掛

Mybatis 自動生成程式碼的外掛使用方法

1、下載Mybatis的jar包外掛
mybatis-generator-core-1.3.2.jar

2、下載資料庫驅動
本例子中採用mysql資料庫
mysql-connector-java-5.1.18.jar

3、配置檔案 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="mysql-connector-java-5.1.18.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property
name="suppressAllComments" value="true"/>
</commentGenerator> <!--資料庫連結地址賬號密碼--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://ip:port/db?characterEncoding=UTF-8" userId="xxx" password="xxx"> </jdbcConnection> <javaTypeResolver
>
<property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Model類存放位置--> <javaModelGenerator targetPackage="com.jd.notice.domain.common" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成對映檔案存放位置--> <sqlMapGenerator targetPackage="notice.mapping" targetProject="src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成Dao類存放位置--> <!--<javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator>--> <!--生成對應表及類名--> <table tableName="notice_message_record" domainObjectName="NoticeMessageRecord" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="notice_message_template" domainObjectName="NoticeMessageTemplate" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="notice_business_apply" domainObjectName="NoticeBusinessApply" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>

4、執行java命令生成程式碼

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

這裡寫圖片描述

ps:intellij外掛iBATIS/MyBatis
mini-plugin 可以讓我們從dao層直接直接點選到對應的xml,很方便快捷。