逆向工程

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配置

<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=".srcmainjava" targetPackage="com.atguigu.mybatis.entities">
                        <!-- enableSubPackages:是否让schema作为包的后缀 -->
                        <property name="enableSubPackages" value="false" />
                        <!-- 从数据库返回的值被清理前后的空格 -->
                        <property name="trimStrings" value="true" />
                    </javaModelGenerator>

                    <!-- targetProject:XxxMapper.xml映射文件生成的路径 -->
                    <sqlMapGenerator targetProject=".srcmainjava" targetPackage="com.atguigu.mybatis.mappers">
                        <!-- enableSubPackages:是否让schema作为包的后缀 -->
                        <property name="enableSubPackages" value="false" />
                    </sqlMapGenerator>

                    <!-- targetPackage:Mapper接口生成的位置 -->
                    <javaClientGenerator type="XMLMAPPER" targetProject=".srcmainjava" 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 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

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.atguigu.mybatis.mappers.CustomerMapper">
    <resultMap id="BaseResultMap" type="com.atguigu.mybatis.entities.Customer">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        <id column="cust_id" property="custId" jdbcType="INTEGER" />
        <result column="cust_name" property="custName" jdbcType="CHAR" />
        <result column="cust_age" property="custAge" jdbcType="INTEGER" />
    </resultMap>
    <sql id="Example_Where_Clause">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        <where>
            <foreach collection="oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" suffix=")" prefixOverrides="and">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and
                                    #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach collection="criterion.value" item="listItem"
                                        open="(" close=")" separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Update_By_Example_Where_Clause">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        <where>
            <foreach collection="example.oredCriteria" item="criteria"
                separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" suffix=")" prefixOverrides="and">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and
                                    #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach collection="criterion.value" item="listItem"
                                        open="(" close=")" separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Base_Column_List">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        cust_id, cust_name, cust_age
    </sql>
    <select id="selectByExample" resultMap="BaseResultMap"
        parameterType="com.atguigu.mybatis.entities.CustomerExample">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List" />
        from tbl_cust
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap"
        parameterType="java.lang.Integer">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        select
        <include refid="Base_Column_List" />
        from tbl_cust
        where cust_id = #{custId,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        delete from tbl_cust
        where cust_id = #{custId,jdbcType=INTEGER}
    </delete>
    <delete id="deleteByExample" parameterType="com.atguigu.mybatis.entities.CustomerExample">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        delete from tbl_cust
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </delete>
    <insert id="insert" parameterType="com.atguigu.mybatis.entities.Customer">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        insert into tbl_cust (cust_id, cust_name, cust_age
        )
        values (#{custId,jdbcType=INTEGER}, #{custName,jdbcType=CHAR},
        #{custAge,jdbcType=INTEGER}
        )
    </insert>
    <insert id="insertSelective" parameterType="com.atguigu.mybatis.entities.Customer">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        insert into tbl_cust
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="custId != null">
                cust_id,
            </if>
            <if test="custName != null">
                cust_name,
            </if>
            <if test="custAge != null">
                cust_age,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="custId != null">
                #{custId,jdbcType=INTEGER},
            </if>
            <if test="custName != null">
                #{custName,jdbcType=CHAR},
            </if>
            <if test="custAge != null">
                #{custAge,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>
    <select id="countByExample" parameterType="com.atguigu.mybatis.entities.CustomerExample"
        resultType="java.lang.Integer">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        select count(*) from tbl_cust
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </select>
    <update id="updateByExampleSelective" parameterType="map">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        update tbl_cust
        <set>
            <if test="record.custId != null">
                cust_id = #{record.custId,jdbcType=INTEGER},
            </if>
            <if test="record.custName != null">
                cust_name = #{record.custName,jdbcType=CHAR},
            </if>
            <if test="record.custAge != null">
                cust_age = #{record.custAge,jdbcType=INTEGER},
            </if>
        </set>
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause" />
        </if>
    </update>
    <update id="updateByExample" parameterType="map">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        update tbl_cust
        set cust_id = #{record.custId,jdbcType=INTEGER},
        cust_name = #{record.custName,jdbcType=CHAR},
        cust_age = #{record.custAge,jdbcType=INTEGER}
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause" />
        </if>
    </update>
    <update id="updateByPrimaryKeySelective" parameterType="com.atguigu.mybatis.entities.Customer">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        update tbl_cust
        <set>
            <if test="custName != null">
                cust_name = #{custName,jdbcType=CHAR},
            </if>
            <if test="custAge != null">
                cust_age = #{custAge,jdbcType=INTEGER},
            </if>
        </set>
        where cust_id = #{custId,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.atguigu.mybatis.entities.Customer">
        <!-- WARNING - @mbggenerated This element is automatically generated by 
            MyBatis Generator, do not modify. This element was generated on Sat May 13 
            18:30:28 CST 2017. -->
        update tbl_cust
        set cust_name = #{custName,jdbcType=CHAR},
        cust_age = #{custAge,jdbcType=INTEGER}
        where cust_id = #{custId,jdbcType=INTEGER}
    </update>
</mapper>
原文地址:https://www.cnblogs.com/likeyou1/p/6849916.html