1.什麼是逆向工程
MyBatis的一個主要的特色就是須要程序員本身編寫sql,那麼若是表太多的話,不免會很麻煩,因此mybatis官方提供了一個逆向工程,能夠針對單表自動生成mybatis執行所須要的代碼(包括mapper.xml、mapper.java、po..)。通常在開發中,經常使用的逆向工程方式是經過數據庫的表生成代碼。
2.逆向工程的使用java
2.1建立一個新的工程mysql
2.2導入jar包git
mybatis-generator-core-1.3.5.jar地址:程序員
https://github.com/mybatis/generator/releasesgithub
2.3定義generator.xml文件sql
<?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="E:/Jar/mysql-connector-java-5.1.46.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自動生成的註釋 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- Mysql數據庫鏈接的信息:驅動類、鏈接地址、用戶名、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="123456"> </jdbcConnection> <!-- 默認爲false,把JDBC DECIMAL 和NUMERIC類型解析爲Integer,爲true時 把JDBC DECIMAL 和NUMERIC類型解析爲java.math.BigDecimal --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成實體類的位置 --> <javaModelGenerator targetPackage="com.zhiyou.zyl.bean" targetProject="./src"> <!-- enableSubPackages:是否讓schema做爲包的後綴 --> <property name="enableSubPackages" value="true" /> <!-- 從數據庫返回的值被清理先後的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="com.zhiyou.zyl.mapper" targetProject="./src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- targetProject:mapper接口生成的的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyou.zyl.dao" targetProject="./src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 指定數據表 --> <table schema="mybatis" tableName="users" domainObjectName="Users" > <property name="useActualColumnNames" value="true"/> <generatedKey column="ID" sqlStatement="DB2" identity="true" /> <columnOverride column="DATE_FIELD" property="startDate" /> <ignoreColumn column="FRED" /> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> </table> </context> </generatorConfiguration>
2.4建立執行xml文件的類數據庫
import java.io.File; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; public class Generator { public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置文件 File configFile = new File("generoter.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); } public static void main(String[] args) throws Exception { try { Generator generatorSqlmap = new Generator(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }
2.5執行結果api