逆向工程
Posted 恋红尘,醉江山
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了逆向工程相关的知识,希望对你有一定的参考价值。
1.概念
①正向工程:Java类→数据库表 MyBatis不支持
②逆向工程:数据库表→Java类
总结:通过MyBatis的jar包自动的生成数据库所对应的Javabean。
步骤:
1.①创建一个专门的工程用于生成Java文件
先导包:
- log4j-1.2.17.jar:日志包
- mybatis-3.2.8.jar
- mybatis-generator-core-1.3.2.jar
- mysql-connector-java-5.1.37-bin.jar
2.②pom.xml配置
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.0</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
3.
③创建generatorConfig.xml,保存到src/main/resources目录下
说明信息参见:mybatis-generator-core-1.3.2的官方文档
<!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="atguiguTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是;false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis0407"
userId="root"
password="root">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成Entity类的路径 -->
<javaModelGenerator targetProject=".\\src\\main\\java" targetPackage="com.atguigu.mybatis.entities">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:XxxMapper.xml映射文件生成的路径 -->
<sqlMapGenerator targetProject=".\\src\\main\\java" targetPackage="com.atguigu.mybatis.mappers">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:Mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER" targetProject=".\\src\\main\\java" targetPackage="com.atguigu.mybatis.mappers">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 数据库表名字和我们的entity类对应的映射指定 -->
<table tableName="tbl_book" domainObjectName="Book" />
</context>
</generatorConfiguration>
4.
④执行Maven命令:mybatis-generator:generate
⑤简单版context标签设置:targetRuntime="MyBatis3Simple" defaultModelType="flat"
案例:
1.
2.配置generatorConfig.xml
<!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="atguiguTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是;false:否 --> <property name="suppressAllComments" value="FALSE" /> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" 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:生成Entity类的路径 --> <javaModelGenerator targetProject=".\\src" targetPackage="com.atguigu.mybatis.entities"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:XxxMapper.xml映射文件生成的路径 --> <sqlMapGenerator targetProject=".\\src" targetPackage="com.atguigu.mybatis.mappers"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:Mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetProject=".\\src" targetPackage="com.atguigu.mybatis.mappers"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 数据库表名字和我们的entity类对应的映射指定 --> <table tableName="tbl_cust" domainObjectName="Customer" /> </context> </generatorConfiguration>
3.一个测试逆向工程的test类:
package com.gui.bean.test; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; 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; /** * 创建测试生成的bean程序 * * @author Administrator * */ public class CreateBean { @SuppressWarnings("resource") public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; InputStream resourceAsStream = CreateBean .class .getClassLoader() .getResourceAsStream("generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(resourceAsStream); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); }
4.测试结果:
自动生成:
代码如下:
package com.atguigu.mybatis.entities; public class Customer { /** * This field was generated by MyBatis Generator. * This field corresponds to the database column tbl_cust.cust_id * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ private Integer custId; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column tbl_cust.cust_name * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ private String custName; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column tbl_cust.cust_age * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ private Integer custAge; /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tbl_cust.cust_id * * @return the value of tbl_cust.cust_id * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public Integer getCustId() { return custId; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tbl_cust.cust_id * * @param custId the value for tbl_cust.cust_id * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public void setCustId(Integer custId) { this.custId = custId; } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tbl_cust.cust_name * * @return the value of tbl_cust.cust_name * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public String getCustName() { return custName; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tbl_cust.cust_name * * @param custName the value for tbl_cust.cust_name * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public void setCustName(String custName) { this.custName = custName == null ? null : custName.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tbl_cust.cust_age * * @return the value of tbl_cust.cust_age * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public Integer getCustAge() { return custAge; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tbl_cust.cust_age * * @param custAge the value for tbl_cust.cust_age * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public void setCustAge(Integer custAge) { this.custAge = custAge; } }
package com.atguigu.mybatis.entities; import java.util.ArrayList; import java.util.List; public class CustomerExample { /** * This field was generated by MyBatis Generator. * This field corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ protected String orderByClause; /** * This field was generated by MyBatis Generator. * This field corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ protected boolean distinct; /** * This field was generated by MyBatis Generator. * This field corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ protected List<Criteria> oredCriteria; /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public CustomerExample() { oredCriteria = new ArrayList<Criteria>(); } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public void setOrderByClause(String orderByClause) { this.orderByClause = orderByClause; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public String getOrderByClause() { return orderByClause; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public void setDistinct(boolean distinct) { this.distinct = distinct; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public boolean isDistinct() { return distinct; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public List<Criteria> getOredCriteria() { return oredCriteria; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public void or(Criteria criteria) { oredCriteria.add(criteria); } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public Criteria or() { Criteria criteria = createCriteriaInternal(); oredCriteria.add(criteria); return criteria; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public Criteria createCriteria() { Criteria criteria = createCriteriaInternal(); if (oredCriteria.size() == 0) { oredCriteria.add(criteria); } return criteria; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ protected Criteria createCriteriaInternal() { Criteria criteria = new Criteria(); return criteria; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public void clear() { oredCriteria.clear(); orderByClause = null; distinct = false; } /** * This class was generated by MyBatis Generator. * This class corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ protected abstract static class GeneratedCriteria { protected List<Criterion> criteria; protected GeneratedCriteria() { super(); criteria = new ArrayList<Criterion>(); } public boolean isValid() { return criteria.size() > 0; } public List<Criterion> getAllCriteria() { return criteria; } public List<Criterion> getCriteria() { return criteria; } protected void addCriterion(String condition) { if (condition == null) { throw new RuntimeException("Value for condition cannot be null"); } criteria.add(new Criterion(condition)); } protected void addCriterion(String condition, Object value, String property) { if (value == null) { throw new RuntimeException("Value for " + property + " cannot be null"); } criteria.add(new Criterion(condition, value)); } protected void addCriterion(String condition, Object value1, Object value2, String property) { if (value1 == null || value2 == null) { throw new RuntimeException("Between values for " + property + " cannot be null"); } criteria.add(new Criterion(condition, value1, value2)); } public Criteria andCustIdIsNull() { addCriterion("cust_id is null"); return (Criteria) this; } public Criteria andCustIdIsNotNull() { addCriterion("cust_id is not null"); return (Criteria) this; } public Criteria andCustIdEqualTo(Integer value) { addCriterion("cust_id =", value, "custId"); return (Criteria) this; } public Criteria andCustIdNotEqualTo(Integer value) { addCriterion("cust_id <>", value, "custId"); return (Criteria) this; } public Criteria andCustIdGreaterThan(Integer value) { addCriterion("cust_id >", value, "custId"); return (Criteria) this; } public Criteria andCustIdGreaterThanOrEqualTo(Integer value) { addCriterion("cust_id >=", value, "custId"); return (Criteria) this; } public Criteria andCustIdLessThan(Integer value) { addCriterion("cust_id <", value, "custId"); return (Criteria) this; } public Criteria andCustIdLessThanOrEqualTo(Integer value) { addCriterion("cust_id <=", value, "custId"); return (Criteria) this; } public Criteria andCustIdIn(List<Integer> values) { addCriterion("cust_id in", values, "custId"); return (Criteria) this; } public Criteria andCustIdNotIn(List<Integer> values) { addCriterion("cust_id not in", values, "custId"); return (Criteria) this; } public Criteria andCustIdBetween(Integer value1, Integer value2) { addCriterion("cust_id between", value1, value2, "custId"); return (Criteria) this; } public Criteria andCustIdNotBetween(Integer value1, Integer value2) { addCriterion("cust_id not between", value1, value2, "custId"); return (Criteria) this; } public Criteria andCustNameIsNull() { addCriterion("cust_name is null"); return (Criteria) this; } public Criteria andCustNameIsNotNull() { addCriterion("cust_name is not null"); return (Criteria) this; } public Criteria andCustNameEqualTo(String value) { addCriterion("cust_name =", value, "custName"); return (Criteria) this; } public Criteria andCustNameNotEqualTo(String value) { addCriterion("cust_name <>", value, "custName"); return (Criteria) this; } public Criteria andCustNameGreaterThan(String value) { addCriterion("cust_name >", value, "custName"); return (Criteria) this; } public Criteria andCustNameGreaterThanOrEqualTo(String value) { addCriterion("cust_name >=", value, "custName"); return (Criteria) this; } public Criteria andCustNameLessThan(String value) { addCriterion("cust_name <", value, "custName"); return (Criteria) this; } public Criteria andCustNameLessThanOrEqualTo(String value) { addCriterion("cust_name <=", value, "custName"); return (Criteria) this; } public Criteria andCustNameLike(String value) { addCriterion("cust_name like", value, "custName"); return (Criteria) this; } public Criteria andCustNameNotLike(String value) { addCriterion("cust_name not like", value, "custName"); return (Criteria) this; } public Criteria andCustNameIn(List<String> values) { addCriterion("cust_name in", values, "custName"); return (Criteria) this; } public Criteria andCustNameNotIn(List<String> values) { addCriterion("cust_name not in", values, "custName"); return (Criteria) this; } public Criteria andCustNameBetween(String value1, String value2) { addCriterion("cust_name between", value1, value2, "custName"); return (Criteria) this; } public Criteria andCustNameNotBetween(String value1, String value2) { addCriterion("cust_name not between", value1, value2, "custName"); return (Criteria) this; } public Criteria andCustAgeIsNull() { addCriterion("cust_age is null"); return (Criteria) this; } public Criteria andCustAgeIsNotNull() { addCriterion("cust_age is not null"); return (Criteria) this; } public Criteria andCustAgeEqualTo(Integer value) { addCriterion("cust_age =", value, "custAge"); return (Criteria) this; } public Criteria andCustAgeNotEqualTo(Integer value) { addCriterion("cust_age <>", value, "custAge"); return (Criteria) this; } public Criteria andCustAgeGreaterThan(Integer value) { addCriterion("cust_age >", value, "custAge"); return (Criteria) this; } public Criteria andCustAgeGreaterThanOrEqualTo(Integer value) { addCriterion("cust_age >=", value, "custAge"); return (Criteria) this; } public Criteria andCustAgeLessThan(Integer value) { addCriterion("cust_age <", value, "custAge"); return (Criteria) this; } public Criteria andCustAgeLessThanOrEqualTo(Integer value) { addCriterion("cust_age <=", value, "custAge"); return (Criteria) this; } public Criteria andCustAgeIn(List<Integer> values) { addCriterion("cust_age in", values, "custAge"); return (Criteria) this; } public Criteria andCustAgeNotIn(List<Integer> values) { addCriterion("cust_age not in", values, "custAge"); return (Criteria) this; } public Criteria andCustAgeBetween(Integer value1, Integer value2) { addCriterion("cust_age between", value1, value2, "custAge"); return (Criteria) this; } public Criteria andCustAgeNotBetween(Integer value1, Integer value2) { addCriterion("cust_age not between", value1, value2, "custAge"); return (Criteria) this; } } /** * This class was generated by MyBatis Generator. * This class corresponds to the database table tbl_cust * * @mbggenerated do_not_delete_during_merge Sat May 13 18:30:28 CST 2017 */ public static class Criteria extends GeneratedCriteria { protected Criteria() { super(); } } /** * This class was generated by MyBatis Generator. * This class corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ public static class Criterion { private String condition; private Object value; private Object secondValue; private boolean noValue; private boolean singleValue; private boolean betweenValue; private boolean listValue; private String typeHandler; public String getCondition() { return condition; } public Object getValue() { return value; } public Object getSecondValue() { return secondValue; } public boolean isNoValue() { return noValue; } public boolean isSingleValue() { return singleValue; } public boolean isBetweenValue() { return betweenValue; } public boolean isListValue() { return listValue; } public String getTypeHandler() { return typeHandler; } protected Criterion(String condition) { super(); this.condition = condition; this.typeHandler = null; this.noValue = true; } protected Criterion(String condition, Object value, String typeHandler) { super(); this.condition = condition; this.value = value; this.typeHandler = typeHandler; if (value instanceof List<?>) { this.listValue = true; } else { this.singleValue = true; } } protected Criterion(String condition, Object value) { this(condition, value, null); } protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { super(); this.condition = condition; this.value = value; this.secondValue = secondValue; this.typeHandler = typeHandler; this.betweenValue = true; } protected Criterion(String condition, Object value, Object secondValue) { this(condition, value, secondValue, null); } } }
CustomerMapper.java
package com.atguigu.mybatis.mappers; import com.atguigu.mybatis.entities.Customer; import com.atguigu.mybatis.entities.CustomerExample; import java.util.List; import org.apache.ibatis.annotations.Param; public interface CustomerMapper { /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ int countByExample(CustomerExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ int deleteByExample(CustomerExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ int deleteByPrimaryKey(Integer custId); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ int insert(Customer record); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ int insertSelective(Customer record); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ List<Customer> selectByExample(CustomerExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ Customer selectByPrimaryKey(Integer custId); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated Sat May 13 18:30:28 CST 2017 */ int updateByExampleSelective(@Param("record") Customer record, @Param("example") CustomerExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tbl_cust * * @mbggenerated S以上是关于逆向工程的主要内容,如果未能解决你的问题,请参考以下文章
Android 逆向ART 脱壳 ( DexClassLoader 脱壳 | DexClassLoader 构造函数 | 参考 Dalvik 的 DexClassLoader 类加载流程 )(代码片段
Android 逆向ART 脱壳 ( DexClassLoader 脱壳 | DexClassLoader 构造函数 | 参考 Dalvik 的 DexClassLoader 类加载流程 )(代码片段
Android 逆向整体加固脱壳 ( DEX 优化流程分析 | DexPrepare.cpp 中 dvmOptimizeDexFile() 方法分析 | /bin/dexopt 源码分析 )(代码片段
Android 逆向Android 进程注入工具开发 ( Visual Studio 开发 Android NDK 应用 | Visual Studio 中 SDK 和 NDK 安装位置 )(代码片段