博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
逆向工程
阅读量:7006 次
发布时间:2019-06-27

本文共 25893 字,大约阅读时间需要 86 分钟。

1.概念

①正向工程:Java类→数据库表 MyBatis不支持
②逆向工程:数据库表→Java类

总结:通过MyBatis的jar包自动的生成数据库所对应的Javabean。

步骤:

1.①创建一个专门的工程用于生成Java文件

 先导包: 

  1. log4j-1.2.17.jar:日志包
  2. mybatis-3.2.8.jar
  3. mybatis-generator-core-1.3.2.jar
  4. mysql-connector-java-5.1.37-bin.jar

2.②pom.xml配置

org.mybatis
mybatis
3.2.8
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.0
org.mybatis.generator
mybatis-generator-core
1.3.2
com.mchange
c3p0
0.9.2
mysql
mysql-connector-java
5.1.8

3.

③创建generatorConfig.xml,保存到src/main/resources目录下

说明信息参见:mybatis-generator-core-1.3.2的官方文档

            

4.

④执行Maven命令:mybatis-generator:generate

⑤简单版context标签设置:targetRuntime="MyBatis3Simple" defaultModelType="flat"

案例:

1.

2.配置generatorConfig.xml

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
warnings = new ArrayList
(); 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
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
(); } /** * 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
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
criteria; protected GeneratedCriteria() { super(); criteria = new ArrayList
(); } public boolean isValid() { return criteria.size() > 0; } public List
getAllCriteria() { return criteria; } public List
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
values) { addCriterion("cust_id in", values, "custId"); return (Criteria) this; } public Criteria andCustIdNotIn(List
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
values) { addCriterion("cust_name in", values, "custName"); return (Criteria) this; } public Criteria andCustNameNotIn(List
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
values) { addCriterion("cust_age in", values, "custAge"); return (Criteria) this; } public Criteria andCustAgeNotIn(List
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
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 Sat May 13 18:30:28 CST 2017 */ int updateByExample(@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 Sat May 13 18:30:28 CST 2017 */ int updateByPrimaryKeySelective(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 updateByPrimaryKey(Customer record);}

CustomerMapper.xml

and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
cust_id, cust_name, cust_age
delete from tbl_cust where cust_id = #{custId,jdbcType=INTEGER}
delete from tbl_cust
insert into tbl_cust (cust_id, cust_name, cust_age ) values (#{custId,jdbcType=INTEGER}, #{custName,jdbcType=CHAR}, #{custAge,jdbcType=INTEGER} )
insert into tbl_cust
cust_id,
cust_name,
cust_age,
#{custId,jdbcType=INTEGER},
#{custName,jdbcType=CHAR},
#{custAge,jdbcType=INTEGER},
update tbl_cust
cust_id = #{record.custId,jdbcType=INTEGER},
cust_name = #{record.custName,jdbcType=CHAR},
cust_age = #{record.custAge,jdbcType=INTEGER},
update tbl_cust set cust_id = #{record.custId,jdbcType=INTEGER}, cust_name = #{record.custName,jdbcType=CHAR}, cust_age = #{record.custAge,jdbcType=INTEGER}
update tbl_cust
cust_name = #{custName,jdbcType=CHAR},
cust_age = #{custAge,jdbcType=INTEGER},
where cust_id = #{custId,jdbcType=INTEGER}
update tbl_cust set cust_name = #{custName,jdbcType=CHAR}, cust_age = #{custAge,jdbcType=INTEGER} where cust_id = #{custId,jdbcType=INTEGER}

 

转载于:https://www.cnblogs.com/likeyou1/p/6849916.html

你可能感兴趣的文章