本文博客地址:http://blog.csdn.net/soonfly/article/details/64499423
逆向工程下載
鏈接:https://pan.baidu.com/s/1YOAq8w-1gex9e-n8wFARpA 密碼:op9h
mybatis官方提供了一個逆向工程包,可以針對數據庫表自動生成mybatis執行所需要的Pojo、Mapper xml文件、Mapper Interface接口文件。
mybatis-generator有很多種用法:命令行、eclipse/IDEA、Maven插件,其使用原理完全一樣。
無論哪種方式,首先要准備兩個組件包:mybatis-generator-core-1.X.X.jar 和MySQL-connector-Java-5.X.XX.jar (點擊下載兩個組件)
命令行方式
從這個入手,因為最方便。
1、新建任意目錄(D:\A-TWM\Mybatis),把兩個組件拷入目錄。
2、新建配置文件,命名:config.xml
補充:下載好的jar包里面有幫助文檔,打開后里面有配置文件的模板。
config.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="D:\A-TWM\Mybatis\mysql-connector-java-5.1.26-bin.jar" /> <context id="sqlGenerate" targetRuntime="MyBatis3"> <!-- 是否去除自動生成的注釋 true:是 : false:否 --> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 數據庫鏈接URL、用戶名、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/tangwenmingdb?characterEncoding=utf8" userId="root" password="root"> </jdbcConnection> <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer; 為 true時把JDBC DECIMAL和NUMERIC類型解析為java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成Pojo包名和位置 --> <javaModelGenerator targetPackage="twm.mybatisdemo.pojo" targetProject="D:\A-TWM\Mybatis\src"> <!-- enableSubPackages:是否讓schema作為包的后綴 --> <property name="enableSubPackages" value="true" /> <!-- 清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成Mapper映射XML文件位置 --> <sqlMapGenerator targetPackage="twm.mybatisdemo.mapper" targetProject="D:\A-TWM\Mybatis\src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成Mapper接口文件位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="twm.mybatisdemo.mapper" targetProject="D:\A-TWM\Mybatis\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成哪些表(更改tableName和domainObjectName就可以) --> <!-- tableName:要生成的表名 domainObjectName:生成后的實例名 enableCountByExample:Count語句中加入where條件查詢,默認為true開啟 enableUpdateByExample:Update語句中加入where條件查詢,默認為true開啟 enableDeleteByExample:Delete語句中加入where條件查詢,默認為true開啟 enableSelectByExample:Select多條語句中加入where條件查詢,默認為true開啟 selectByExampleQueryId:Select單個對象語句中加入where條件查詢,默認為true開啟 --> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="category" /> <table tableName="order"/> <table tableName="product"/> <table tableName="order_detail"/> </context> </generatorConfiguration>
如果table里邊不配置property,默認將所有字段逆向生成為類屬性。
如果有些字段並不想生成為類屬性,可以用ignoreColumn標簽:
<ignoreColumn column="FRED" />//忽略字段
還可以指定逆向生成時,字段到屬性的轉換對應關系
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//無論字段是什么類型,生成的類屬性都是varchar。
3、通過cmd打開命令窗口
運行:java -jar mybatis-generator-core-1.3.2.jar -configfile config.xml -overwrite
出現MyBatis Generator finished successfully.表示運行成功,將指定生成位置(這里是src)的源碼拷入工作項目中即可。
Eclipse方式
1、新建工程、將組件和將配置文件config.xml放到對應的目錄
2、在main函數中寫代碼運行
public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指向逆向工程配置文件 File configFile = new File("generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); }
3、以application的方式運行就可以了