最近使用MyBatis Generator逆向生成持久層的代碼,打卡。html
MyBatis Generator的詳細介紹http://mybatis.github.io/generator/index.htmljava
MyBatis Generatormysql
With Maven http://mybatis.github.io/generator/running/runningWithMaven.htmlgit
MyBatis Generator(如下簡稱爲MBG),能夠逆向生成持久層的基本代碼,並且mybatis的實現方案比較好,能夠自由組合完成比較複雜的查詢,固然更復雜的就須要手動寫了,下面整理下基本使用.github
1.搭建逆向工程:sql
在pom文件中,添加MBG插件,IDE會自動幫咱們下載插件數據庫
(若是沒反應,能夠點開右側Maven Project選項卡刷新如下)mybatis
(插件1.3.0有點小bug,不能去掉生成的註釋)app
<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>
2.在src/main/resource目錄下建立generatorConfig.xml文件:dom
官方配置說明:
http://mybatis.github.io/generator/configreference/xmlconfig.html
<?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:\response\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar"></classPathEntry> <context id="common" targetRuntime="MyBatis3"> <commentGenerator> <!--去除註釋 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--數據庫鏈接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/helpCenter" userId="root" password="root"/> <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer true,把JDBC DECIMAL 和 NUMERIC 類型解析爲java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成實體類 指定包名 以及生成的地址 (能夠自定義地址,可是路徑不存在不會自動建立 使用Maven生成在target目錄下,會自動建立) --> <javaModelGenerator targetPackage="com.zhaogang.model" targetProject="E:/pangmaobao/gitCode/interest.pmb.backend.ui/pmb-backend/backend-biz/src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成SQLMAP文件 --> <sqlMapGenerator targetPackage="com.zhaogang.mapper" targetProject="E:/pangmaobao/gitCode/interest.pmb.backend.ui/pmb-backend/backend-biz/src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成Dao文件 能夠配置 type="XMLMAPPER"生成xml的dao實現 context id="DB2Tables" 修改targetRuntime="MyBatis3" --> <javaClientGenerator targetPackage="com.zhaogang.mapper" targetProject="E:/pangmaobao/gitCode/interest.pmb.backend.ui/pmb-backend/backend-biz/src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 配置不生成Exmaple文件 --> <!--對應數據庫表 mysql能夠加入主鍵自增 字段命名 忽略某字段等--> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <!--<table tableName="role" domainObjectName="Role"--> <!--enableCountByExample="false" enableUpdateByExample="false"--> <!--enableDeleteByExample="false" enableSelectByExample="false"--> <!--selectByExampleQueryId="false">--> <!--</table>--> <!--<table tableName="resource" domainObjectName="Resource"--> <!--enableCountByExample="false" enableUpdateByExample="false"--> <!--enableDeleteByExample="false" enableSelectByExample="false"--> <!--selectByExampleQueryId="false">--> <!--</table>--> <!--<table tableName="user_role" domainObjectName="UserRole"--> <!--enableCountByExample="false" enableUpdateByExample="false"--> <!--enableDeleteByExample="false" enableSelectByExample="false"--> <!--selectByExampleQueryId="false">--> <!--</table>--> <!--<table tableName="role_resource" domainObjectName="RoleResource"--> <!--enableCountByExample="false" enableUpdateByExample="false"--> <!--enableDeleteByExample="false" enableSelectByExample="false"--> <!--selectByExampleQueryId="false">--> <!--</table>--> </context> </generatorConfiguration>
3.點擊maven project --項目 --plugins --mybatis generator -- run maven build
4.成功了!~
引用(轉自):