zoukankan      html  css  js  c++  java
  • Mybatis逆向工程

    Mybaits需要程序员自己编写SQL语句,为了更方便开发Mybaits,官方提供逆向工程,可以针对单表自动生成Mybaits执行所需要的代码。实际开发中,常用的逆向工程方式,由数据库的表生成Java代码。

    pom文件

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.1</version>
    </dependency>
    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.22</version>
    </dependency>
    
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.7</version>
    </dependency>
    

    配置文件

    <?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>
                <!--是否去除自动生成的注释 true:是 : false:否-->
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
    
            <!--数据库连接的信息:驱动类、连接地址、用户名、密码-->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/test" userId="root"
                password="123456">
            </jdbcConnection>
    
            <!--默认false,把JDBC的DECIMAL和NUMERIC类型解析为 Integer-->
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <!--targetProject:生成PO类的位置 -->
            <javaModelGenerator targetPackage="com.feige.mapper" targetProject=".srcmainjava">
                <!--enableSubPackages:是否让schema作为包的后缀-->
                <property name="enableSubPackages" value="false" />
                <!--从数据库返回的值被清理前后的空格-->
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
    
            <!--targetProject:Mapper映射xml文件生成的位置-->
            <sqlMapGenerator targetPackage="com.feige.mapper" targetProject=".srcmainjava">
                <!--enableSubPackages:是否让schema作为包的后缀-->
                <property name="enableSubPackages" value="false" />
            </sqlMapGenerator>
    
            <!--targetPackage:Mapper接口文件生成的位置-->
            <javaClientGenerator type="XMLMAPPER" targetPackage="com.feige.mapper" targetProject=".srcmainjava">
                <!-- enableSubPackages:是否让schema作为包的后缀-->
                <property name="enableSubPackages" value="false" />
            </javaClientGenerator>
    
            <!--指定数据库表-->
            <table schema="" tableName="user"></table>       
        </context>
    </generatorConfiguration>
    

    Java代码

    import java.io.File;
    import java.util.*;
    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("E://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);
        }
        // 执行main方法以生成代码
        public static void main(String[] args) {
            try {
                GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
                generatorSqlmap.generator();
                System.out.println("执行完毕了,一个log也没有给老子打印出来!艹!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    生成文件User.java

    public class User {
        private Integer id;
    
        private String name;
    
        private String hobby;
    
        private Integer age;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name == null ? null : name.trim();
        }
    .......
    

    生成文件UserExample.java

    package com.feige.mapper;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class UserExample {
        protected String orderByClause;
    
        protected boolean distinct;
    
        protected List<Criteria> oredCriteria;
    
        public UserExample() {
            oredCriteria = new ArrayList<Criteria>();
        }
    
        public void setOrderByClause(String orderByClause) {
            this.orderByClause = orderByClause;
        }
    
        public String getOrderByClause() {
            return orderByClause;
        }
    
        public void setDistinct(boolean distinct) {
            this.distinct = distinct;
        }
    
        public boolean isDistinct() {
            return distinct;
        }
    
        public List<Criteria> getOredCriteria() {
            return oredCriteria;
        }
    
        public void or(Criteria criteria) {
            oredCriteria.add(criteria);
        }
    
        public Criteria or() {
            Criteria criteria = createCriteriaInternal();
            oredCriteria.add(criteria);
            return criteria;
        }
    
        public Criteria createCriteria() {
            Criteria criteria = createCriteriaInternal();
            if (oredCriteria.size() == 0) {
                oredCriteria.add(criteria);
            }
            return criteria;
        }
    
        protected Criteria createCriteriaInternal() {
            Criteria criteria = new Criteria();
            return criteria;
        }
    
        public void clear() {
            oredCriteria.clear();
            orderByClause = null;
            distinct = false;
        }
    
        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 andIdIsNull() {
                addCriterion("id is null");
                return (Criteria) this;
            }
    
            public Criteria andIdIsNotNull() {
                addCriterion("id is not null");
                return (Criteria) this;
            }
    
            public Criteria andIdEqualTo(Integer value) {
                addCriterion("id =", value, "id");
                return (Criteria) this;
            }
    
            public Criteria andIdNotEqualTo(Integer value) {
                addCriterion("id <>", value, "id");
                return (Criteria) this;
            }
    
            public Criteria andIdGreaterThan(Integer value) {
                addCriterion("id >", value, "id");
                return (Criteria) this;
            }
    
            public Criteria andIdGreaterThanOrEqualTo(Integer value) {
                addCriterion("id >=", value, "id");
                return (Criteria) this;
            }
    
            public Criteria andIdLessThan(Integer value) {
                addCriterion("id <", value, "id");
                return (Criteria) this;
            }
    
            public Criteria andIdLessThanOrEqualTo(Integer value) {
                addCriterion("id <=", value, "id");
                return (Criteria) this;
            }
    
            public Criteria andIdIn(List<Integer> values) {
                addCriterion("id in", values, "id");
                return (Criteria) this;
            }
    
            public Criteria andIdNotIn(List<Integer> values) {
                addCriterion("id not in", values, "id");
                return (Criteria) this;
            }
    
            public Criteria andIdBetween(Integer value1, Integer value2) {
                addCriterion("id between", value1, value2, "id");
                return (Criteria) this;
            }
    
            public Criteria andIdNotBetween(Integer value1, Integer value2) {
                addCriterion("id not between", value1, value2, "id");
                return (Criteria) this;
            }
    
            public Criteria andNameIsNull() {
                addCriterion("name is null");
                return (Criteria) this;
            }
    
            public Criteria andNameIsNotNull() {
                addCriterion("name is not null");
                return (Criteria) this;
            }
    
            public Criteria andNameEqualTo(String value) {
                addCriterion("name =", value, "name");
                return (Criteria) this;
            }
    
            public Criteria andNameNotEqualTo(String value) {
                addCriterion("name <>", value, "name");
                return (Criteria) this;
            }
    
            public Criteria andNameGreaterThan(String value) {
                addCriterion("name >", value, "name");
                return (Criteria) this;
            }
    
            public Criteria andNameGreaterThanOrEqualTo(String value) {
                addCriterion("name >=", value, "name");
                return (Criteria) this;
            }
    
            public Criteria andNameLessThan(String value) {
                addCriterion("name <", value, "name");
                return (Criteria) this;
            }
    
            public Criteria andNameLessThanOrEqualTo(String value) {
                addCriterion("name <=", value, "name");
                return (Criteria) this;
            }
    
            public Criteria andNameLike(String value) {
                addCriterion("name like", value, "name");
                return (Criteria) this;
            }
    
            public Criteria andNameNotLike(String value) {
                addCriterion("name not like", value, "name");
                return (Criteria) this;
            }
    
            public Criteria andNameIn(List<String> values) {
                addCriterion("name in", values, "name");
                return (Criteria) this;
            }
    
            public Criteria andNameNotIn(List<String> values) {
                addCriterion("name not in", values, "name");
                return (Criteria) this;
            }
    
            public Criteria andNameBetween(String value1, String value2) {
                addCriterion("name between", value1, value2, "name");
                return (Criteria) this;
            }
    
            public Criteria andNameNotBetween(String value1, String value2) {
                addCriterion("name not between", value1, value2, "name");
                return (Criteria) this;
            }
    
            public Criteria andHobbyIsNull() {
                addCriterion("hobby is null");
                return (Criteria) this;
            }
    
            public Criteria andHobbyIsNotNull() {
                addCriterion("hobby is not null");
                return (Criteria) this;
            }
    
            public Criteria andHobbyEqualTo(String value) {
                addCriterion("hobby =", value, "hobby");
                return (Criteria) this;
            }
    
            public Criteria andHobbyNotEqualTo(String value) {
                addCriterion("hobby <>", value, "hobby");
                return (Criteria) this;
            }
    
            public Criteria andHobbyGreaterThan(String value) {
                addCriterion("hobby >", value, "hobby");
                return (Criteria) this;
            }
    
            public Criteria andHobbyGreaterThanOrEqualTo(String value) {
                addCriterion("hobby >=", value, "hobby");
                return (Criteria) this;
            }
    
            public Criteria andHobbyLessThan(String value) {
                addCriterion("hobby <", value, "hobby");
                return (Criteria) this;
            }
    
            public Criteria andHobbyLessThanOrEqualTo(String value) {
                addCriterion("hobby <=", value, "hobby");
                return (Criteria) this;
            }
    
            public Criteria andHobbyLike(String value) {
                addCriterion("hobby like", value, "hobby");
                return (Criteria) this;
            }
    
            public Criteria andHobbyNotLike(String value) {
                addCriterion("hobby not like", value, "hobby");
                return (Criteria) this;
            }
    
            public Criteria andHobbyIn(List<String> values) {
                addCriterion("hobby in", values, "hobby");
                return (Criteria) this;
            }
    
            public Criteria andHobbyNotIn(List<String> values) {
                addCriterion("hobby not in", values, "hobby");
                return (Criteria) this;
            }
    
            public Criteria andHobbyBetween(String value1, String value2) {
                addCriterion("hobby between", value1, value2, "hobby");
                return (Criteria) this;
            }
    
            public Criteria andHobbyNotBetween(String value1, String value2) {
                addCriterion("hobby not between", value1, value2, "hobby");
                return (Criteria) this;
            }
    
            public Criteria andAgeIsNull() {
                addCriterion("age is null");
                return (Criteria) this;
            }
    
            public Criteria andAgeIsNotNull() {
                addCriterion("age is not null");
                return (Criteria) this;
            }
    
            public Criteria andAgeEqualTo(Integer value) {
                addCriterion("age =", value, "age");
                return (Criteria) this;
            }
    
            public Criteria andAgeNotEqualTo(Integer value) {
                addCriterion("age <>", value, "age");
                return (Criteria) this;
            }
    
            public Criteria andAgeGreaterThan(Integer value) {
                addCriterion("age >", value, "age");
                return (Criteria) this;
            }
    
            public Criteria andAgeGreaterThanOrEqualTo(Integer value) {
                addCriterion("age >=", value, "age");
                return (Criteria) this;
            }
    
            public Criteria andAgeLessThan(Integer value) {
                addCriterion("age <", value, "age");
                return (Criteria) this;
            }
    
            public Criteria andAgeLessThanOrEqualTo(Integer value) {
                addCriterion("age <=", value, "age");
                return (Criteria) this;
            }
    
            public Criteria andAgeIn(List<Integer> values) {
                addCriterion("age in", values, "age");
                return (Criteria) this;
            }
    
            public Criteria andAgeNotIn(List<Integer> values) {
                addCriterion("age not in", values, "age");
                return (Criteria) this;
            }
    
            public Criteria andAgeBetween(Integer value1, Integer value2) {
                addCriterion("age between", value1, value2, "age");
                return (Criteria) this;
            }
    
            public Criteria andAgeNotBetween(Integer value1, Integer value2) {
                addCriterion("age not between", value1, value2, "age");
                return (Criteria) this;
            }
        }
    
        public static class Criteria extends GeneratedCriteria {
    
            protected Criteria() {
                super();
            }
        }
    
        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);
            }
        }
    }
    

    生成文件UserMapper.java

    public interface UserMapper {
        int countByExample(UserExample example);
    
        int deleteByExample(UserExample example);
    
        int deleteByPrimaryKey(Integer id);
    
        int insert(User record);
    
        int insertSelective(User record);
    
        List<User> selectByExample(UserExample example);
    
        User selectByPrimaryKey(Integer id);
    
        int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);
    
        int updateByExample(@Param("record") User record, @Param("example") UserExample example);
    
        int updateByPrimaryKeySelective(User record);
    
        int updateByPrimaryKey(User record);
    }
    

    生成文件UserMapper.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.feige.mapper.UserMapper">
      <resultMap id="BaseResultMap" type="com.feige.mapper.User">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="hobby" jdbcType="VARCHAR" property="hobby" />
        <result column="age" jdbcType="INTEGER" property="age" />
      </resultMap>
      <sql id="Example_Where_Clause">
        <where>
          <foreach collection="oredCriteria" item="criteria" separator="or">
            <if test="criteria.valid">
              <trim prefix="(" prefixOverrides="and" suffix=")">
                <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 close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                        #{listItem}
                      </foreach>
                    </when>
                  </choose>
                </foreach>
              </trim>
            </if>
          </foreach>
        </where>
      </sql>
      <sql id="Update_By_Example_Where_Clause">
        <where>
          <foreach collection="example.oredCriteria" item="criteria" separator="or">
            <if test="criteria.valid">
              <trim prefix="(" prefixOverrides="and" suffix=")">
                <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 close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                        #{listItem}
                      </foreach>
                    </when>
                  </choose>
                </foreach>
              </trim>
            </if>
          </foreach>
        </where>
      </sql>
      <sql id="Base_Column_List">
        id, name, hobby, age
      </sql>
      <select id="selectByExample" parameterType="com.feige.mapper.UserExample" resultMap="BaseResultMap">
        select
        <if test="distinct">
          distinct
        </if>
        <include refid="Base_Column_List" />
        from user
        <if test="_parameter != null">
          <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null">
          order by ${orderByClause}
        </if>
      </select>
      <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select 
        <include refid="Base_Column_List" />
        from user
        where id = #{id,jdbcType=INTEGER}
      </select>
      <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete from user
        where id = #{id,jdbcType=INTEGER}
      </delete>
      <delete id="deleteByExample" parameterType="com.feige.mapper.UserExample">
        delete from user
        <if test="_parameter != null">
          <include refid="Example_Where_Clause" />
        </if>
      </delete>
      <insert id="insert" parameterType="com.feige.mapper.User">
        insert into user (id, name, hobby, 
          age)
        values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{hobby,jdbcType=VARCHAR}, 
          #{age,jdbcType=INTEGER})
      </insert>
      <insert id="insertSelective" parameterType="com.feige.mapper.User">
        insert into user
        <trim prefix="(" suffix=")" suffixOverrides=",">
          <if test="id != null">
            id,
          </if>
          <if test="name != null">
            name,
          </if>
          <if test="hobby != null">
            hobby,
          </if>
          <if test="age != null">
            age,
          </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
          <if test="id != null">
            #{id,jdbcType=INTEGER},
          </if>
          <if test="name != null">
            #{name,jdbcType=VARCHAR},
          </if>
          <if test="hobby != null">
            #{hobby,jdbcType=VARCHAR},
          </if>
          <if test="age != null">
            #{age,jdbcType=INTEGER},
          </if>
        </trim>
      </insert>
      <select id="countByExample" parameterType="com.feige.mapper.UserExample" resultType="java.lang.Integer">
        select count(*) from user
        <if test="_parameter != null">
          <include refid="Example_Where_Clause" />
        </if>
      </select>
      <update id="updateByExampleSelective" parameterType="map">
        update user
        <set>
          <if test="record.id != null">
            id = #{record.id,jdbcType=INTEGER},
          </if>
          <if test="record.name != null">
            name = #{record.name,jdbcType=VARCHAR},
          </if>
          <if test="record.hobby != null">
            hobby = #{record.hobby,jdbcType=VARCHAR},
          </if>
          <if test="record.age != null">
            age = #{record.age,jdbcType=INTEGER},
          </if>
        </set>
        <if test="_parameter != null">
          <include refid="Update_By_Example_Where_Clause" />
        </if>
      </update>
      <update id="updateByExample" parameterType="map">
        update user
        set id = #{record.id,jdbcType=INTEGER},
          name = #{record.name,jdbcType=VARCHAR},
          hobby = #{record.hobby,jdbcType=VARCHAR},
          age = #{record.age,jdbcType=INTEGER}
        <if test="_parameter != null">
          <include refid="Update_By_Example_Where_Clause" />
        </if>
      </update>
      <update id="updateByPrimaryKeySelective" parameterType="com.feige.mapper.User">
        update user
        <set>
          <if test="name != null">
            name = #{name,jdbcType=VARCHAR},
          </if>
          <if test="hobby != null">
            hobby = #{hobby,jdbcType=VARCHAR},
          </if>
          <if test="age != null">
            age = #{age,jdbcType=INTEGER},
          </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
      </update>
      <update id="updateByPrimaryKey" parameterType="com.feige.mapper.User">
        update user
        set name = #{name,jdbcType=VARCHAR},
          hobby = #{hobby,jdbcType=VARCHAR},
          age = #{age,jdbcType=INTEGER}
        where id = #{id,jdbcType=INTEGER}
      </update>
      <resultMap id="BaseResultMap" type="com.feige.mapper.User">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="hobby" jdbcType="VARCHAR" property="hobby" />
        <result column="age" jdbcType="INTEGER" property="age" />
      </resultMap>
      <sql id="Example_Where_Clause">
        <where>
          <foreach collection="oredCriteria" item="criteria" separator="or">
            <if test="criteria.valid">
              <trim prefix="(" prefixOverrides="and" suffix=")">
                <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 close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                        #{listItem}
                      </foreach>
                    </when>
                  </choose>
                </foreach>
              </trim>
            </if>
          </foreach>
        </where>
      </sql>
      <sql id="Update_By_Example_Where_Clause">
        <where>
          <foreach collection="example.oredCriteria" item="criteria" separator="or">
            <if test="criteria.valid">
              <trim prefix="(" prefixOverrides="and" suffix=")">
                <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 close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                        #{listItem}
                      </foreach>
                    </when>
                  </choose>
                </foreach>
              </trim>
            </if>
          </foreach>
        </where>
      </sql>
      <sql id="Base_Column_List">
        id, name, hobby, age
      </sql>
      <select id="selectByExample" parameterType="com.feige.mapper.UserExample" resultMap="BaseResultMap">
        select
        <if test="distinct">
          distinct
        </if>
        <include refid="Base_Column_List" />
        from user
        <if test="_parameter != null">
          <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null">
          order by ${orderByClause}
        </if>
      </select>
      <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select 
        <include refid="Base_Column_List" />
        from user
        where id = #{id,jdbcType=INTEGER}
      </select>
      <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete from user
        where id = #{id,jdbcType=INTEGER}
      </delete>
      <delete id="deleteByExample" parameterType="com.feige.mapper.UserExample">
        delete from user
        <if test="_parameter != null">
          <include refid="Example_Where_Clause" />
        </if>
      </delete>
      <insert id="insert" parameterType="com.feige.mapper.User">
        insert into user (id, name, hobby, 
          age)
        values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{hobby,jdbcType=VARCHAR}, 
          #{age,jdbcType=INTEGER})
      </insert>
      <insert id="insertSelective" parameterType="com.feige.mapper.User">
        insert into user
        <trim prefix="(" suffix=")" suffixOverrides=",">
          <if test="id != null">
            id,
          </if>
          <if test="name != null">
            name,
          </if>
          <if test="hobby != null">
            hobby,
          </if>
          <if test="age != null">
            age,
          </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
          <if test="id != null">
            #{id,jdbcType=INTEGER},
          </if>
          <if test="name != null">
            #{name,jdbcType=VARCHAR},
          </if>
          <if test="hobby != null">
            #{hobby,jdbcType=VARCHAR},
          </if>
          <if test="age != null">
            #{age,jdbcType=INTEGER},
          </if>
        </trim>
      </insert>
      <select id="countByExample" parameterType="com.feige.mapper.UserExample" resultType="java.lang.Integer">
        select count(*) from user
        <if test="_parameter != null">
          <include refid="Example_Where_Clause" />
        </if>
      </select>
      <update id="updateByExampleSelective" parameterType="map">
        update user
        <set>
          <if test="record.id != null">
            id = #{record.id,jdbcType=INTEGER},
          </if>
          <if test="record.name != null">
            name = #{record.name,jdbcType=VARCHAR},
          </if>
          <if test="record.hobby != null">
            hobby = #{record.hobby,jdbcType=VARCHAR},
          </if>
          <if test="record.age != null">
            age = #{record.age,jdbcType=INTEGER},
          </if>
        </set>
        <if test="_parameter != null">
          <include refid="Update_By_Example_Where_Clause" />
        </if>
      </update>
      <update id="updateByExample" parameterType="map">
        update user
        set id = #{record.id,jdbcType=INTEGER},
          name = #{record.name,jdbcType=VARCHAR},
          hobby = #{record.hobby,jdbcType=VARCHAR},
          age = #{record.age,jdbcType=INTEGER}
        <if test="_parameter != null">
          <include refid="Update_By_Example_Where_Clause" />
        </if>
      </update>
      <update id="updateByPrimaryKeySelective" parameterType="com.feige.mapper.User">
        update user
        <set>
          <if test="name != null">
            name = #{name,jdbcType=VARCHAR},
          </if>
          <if test="hobby != null">
            hobby = #{hobby,jdbcType=VARCHAR},
          </if>
          <if test="age != null">
            age = #{age,jdbcType=INTEGER},
          </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
      </update>
      <update id="updateByPrimaryKey" parameterType="com.feige.mapper.User">
        update user
        set name = #{name,jdbcType=VARCHAR},
          hobby = #{hobby,jdbcType=VARCHAR},
          age = #{age,jdbcType=INTEGER}
        where id = #{id,jdbcType=INTEGER}
      </update>
    </mapper>
    

    总结

    在平常的开发中,有时会使用Mybatis的逆向工程,来快速的创建类,其中在创建实例的过程中有一个以Example结尾的类,这个类是用来专门对这个单表操作的类,就相当于,对该单表的增、删、改、查是脱离SQL的,直接在Service层就可以完成。

  • 相关阅读:
    个人作业——软件工程实践总结作业
    团队作业——Beta冲刺5
    Beta冲刺博客集合贴
    团队作业——Beta冲刺4
    团队作业——Beta冲刺3
    团队作业——Beta冲刺2
    团队作业——Beta冲刺1
    Alpha事后诸葛亮(阳光普照队)
    软件产品案例分析
    个人作业——软件工程实践总结作业
  • 原文地址:https://www.cnblogs.com/feiqiangsheng/p/11782527.html
Copyright © 2011-2022 走看看