MyBatis Generator:
簡稱MBG,是一個專門爲MyBatis框架使用者定製的代碼生成器,能夠快速的根據表生成對應的映射文件,接口,以及bean類。支持基本的增刪改查,以及QBC風格的條件查詢。可是錶鏈接、存儲過程等這些複雜sql的定義須要咱們手工編寫
官方文檔地址
http://www.mybatis.org/generator/
官方工程地址
https://github.com/mybatis/generator/releasesjava
使用步驟:
1)編寫MBG的配置文件(重要幾處配置)
1)jdbcConnection配置數據庫鏈接信息
2)javaModelGenerator配置javaBean的生成策略
3)sqlMapGenerator 配置sql映射文件生成策略
4)javaClientGenerator配置Mapper接口的生成策略
5)table 配置要逆向解析的數據表
tableName:表名
domainObjectName:對應的javaBean名
2)運行代碼生成器生成代碼
注意:
Context標籤
targetRuntime=「MyBatis3「能夠生成帶條件的增刪改查
targetRuntime=「MyBatis3Simple「能夠生成基本的增刪改查
若是再次生成,建議將以前生成的數據刪除,避免xml向後追加內容出現的問題。mysql
<generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3"> //數據庫鏈接信息配置 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/bookstore0629" userId="root" password="123456"> </jdbcConnection> //javaBean的生成策略 <javaModelGenerator targetPackage="com.atguigu.bean" targetProject=".\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> //映射文件的生成策略 <sqlMapGenerator targetPackage="mybatis.mapper" targetProject=".\conf"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> //dao接口java文件的生成策略 <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.dao" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> //數據表與javaBean的映射 <table tableName="books" domainObjectName="Book"></table> </context> </generatorConfiguration>