9. Reverse Engineering
9.1 What is a reverse engineering
mybatis programmers need to write your own sql statement, mybatis official reverse engineering, can automatically generate the code required to perform mybatis (mapper.java, mapper.xml, po) for Form
The actual development companies, the commonly used reverse engineering methods: java code generated by the table in the database.
9.2 Download Reverse Engineering
mybatis-generator-core-1.3.2.jar
Reverse engineering required jar package:
Under log4j.properties:--- classpath
log4j.rootLogger=DEBUG, Console #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n log4j.logger.java.sql.ResultSet=INFO log4j.logger.org.apache=INFO log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
9.3 Use (will be used)
9.3.1 Run Reverse Engineering
Java program recommended way, does not depend on development tools.
9.3.2 profile generated code
generatorConfig.xml: ---- the classpath
<? xml Version = "1.0" encoding = "UTF-8"?> <! DOCTYPE generatorConfiguration the PUBLIC "- // mybatis.org//DTD the Configuration MyBatis Generator 1.0 // EN" "http://mybatis.org/dtd /mybatis-generator-config_1_0.dtd "> <generatorConfiguration> <context ID =" testTables "targetRuntime =" MyBatis 3 "> <commentGenerator> <- whether to remove automatically generated annotation! to true : is: to false : NO -> < name = Property "suppressAllComments" value = "to true" /> </ commentGenerator> <- database connections:! driver class, connection address, user name, password -> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL= "jdbc: MySQL: // localhost: 3306 / the mybatis" userId = "root" password = "root"> </ JdbcConnection> <- <JdbcConnection driverClass = "oracle.jdbc.OracleDriver"! connectionURL = "jdbc: the Oracle : Thin: @ 127.0.0.1: 1521: YycG " userId =" YycG " password =" YycG "> </ JdbcConnection> -> <-! default false, the JDBC DECIMAL and NUMERIC type resolution as Integer, is true the JDBC DECIMAL and NUMERIC type resolution is java.math.BigDecimal -> <javaTypeResolver> <Property name = "forceBigDecimals"value="false" /> </javaTypeResolver> <- targetProject:! Position to PO type -> <javaModelGenerator targetPackage = "com.xjs.ssm.po" targetProject =. "\ The src"> <- enableSubPackages:! Whether to allow schema package as a suffix - > <Property name = "enableSubPackages" value = "to false" /> <-! spaces before and after the cleaning values are returned from the database -> <Property name = "trimStrings" value = "to true" /> </ javaModelGenerator> <- targetProject:! mapper mapping file generated location -> <sqlMapGenerator targetPackage = "com.xjs.ssm.mapper" targetProject =. "\ src"> <- enableSubPackages!:Whether to allow schema package as a suffix -> <Property name = "enableSubPackages" = value "false" /> </sqlMapGenerator> <- targetPackage:! Mapper interface generation location -> <javaClientGenerator type="XMLMAPPER" targetPackage="com.xjs.ssm.mapper" targetProject=".\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定数据库表 --> <table tableName="items"></table> <table tableName="orders"></table> <table tableName="orderdetail"></table> <table tableName="user"></table> <!- <table schema = "" tableName =Some fields specify java table type </ context> </ Table> -> <columnOverride column = "" the javaType = "" /> </generatorConfiguration>
9.3.3 generation program execution
package generatorSqlmapCustom; import java.io.File; import java.io.IOException; 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.exception.XMLParserException; 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) throws Exception { try { GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }