什麼是逆向工程java
mybatis須要程序員本身編寫sql代碼 官方提供了逆向工程 能夠針對單表自動生成mybatis執行所須要的代碼(mapper.java,mapper.xml,pojo...)mysql
在企業的實際開發中經常使用的逆向工程方式:程序員
由數據庫的表生成java代碼(由於大多數狀況下在設計階段數據庫已經設計好了)sql
逆向工程實現:數據庫
引入所須要的包:apache
mybatis包:mybatis-3.2.7.jar api
mybatis逆向工程包:mybatis-generator-core-1.3.2.jarmybatis
mysql驅動包:mysql-connector-java-5.1.6-bin.jarapp
日誌包:commons-logging-1.1.1.jardom
日誌包:log4j-1.2.17.jar
日誌包:log4j-api-2.0-rc1.jar
下載mybatis-generator-core-1.3.2.jar 逆向工程所須要的包
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> <context id="testTables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 鏈接數據庫 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password=""> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetPackage:生成po類的包名 targetProject:生成po類的位置 --> <javaModelGenerator targetPackage="com.mybatis.po" targetProject="./src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetPackage:mapper接口文件生成的包名 targetProject:mapper接口文件生成的項目位置--> <sqlMapGenerator targetPackage="com.mybatis.dao" targetProject="./src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- targetPackage:mapper映射文件生成的包名 targetProject:mapper映射文件生成的項目位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.dao" targetProject="./src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 配置須要生成的表 --> <table tableName="user" domainObjectName="user"></table> <table tableName="orders" domainObjectName="orders"></table> <table tableName="orderdetail" domainObjectName="orderdetail"></table> <table tableName="items" domainObjectName="items"></table> </context> </generatorConfiguration>
執行逆向工程的類
GeneratorSqlMap.java
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 GeneratorSqlMap { public void generator() 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); } public static void main(String[] args) { GeneratorSqlMap generatorSqlMap = new GeneratorSqlMap(); try { generatorSqlMap.generator(); } catch (Exception e) { e.printStackTrace(); } } }
只須要指定配置文件的路徑就能夠了
日誌文件log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n