mybatis中Example的使用

目录

 

使用示例

类解析

原TbGoodsExample代码


使用示例

1.mapper.java代码

public interface TbGoodsMapper {
    int countByExample(TbGoodsExample example);
    int updateByExampleSelective(@Param("record") TbGoods record, @Param("example") TbGoodsExample example);
    int updateByExample(@Param("record") TbGoods record, @Param("example") TbGoodsExample example);
}

2.mapper.xml代码

<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.pinyougou.pojo.TbGoodsExample" >
    select
    <if test="distinct" > distinct </if>
    <include refid="Base_Column_List" />
    from tb_goods
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
</select>
<sql id="Example_Where_Clause" >
    <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>

类解析

1.都有哪些类参与了此次搞事情

      TbGoodsExample、Criteria、GeneratedCriteria、Criterion 关系如下(原本是其他3个类都在TbGoodsExample里面,是内部类,为了方便分析,拆开了.整个类的代码我会粘贴到最后)

        

2.类代码说明

TbGoodsExample.java

package com.test.criteria;

import java.util.ArrayList;
import java.util.List;

//select * from t_user where (id > 45 and createTime < "2019") or (age < 32 and weight > 160) order by id
//id > 45,createTime < "2019",age < 32,weight > 160 这是4个Criterion
//(id > 45 and createTime < "2019"),(age < 32 and weight > 160) 这是2个Criteria
//(id > 45 and createTime < "2019") or (age < 32 and weight > 160) 这是一个List<Criteria> 
//order by id 中id是 orderByClause
//这玩意和mybatis底层好像并么有什么关系,只不过是利用了mybatis的动态sql.
public class TbGoodsExample {
	protected String orderByClause; //order by id 中的id

    protected boolean distinct; //是否启用distinct关键字 <if test="distinct" > distinct </if>
    //多组(多个一组)条件
    protected List<Criteria> oredCriteria;//(id > 45 and createTime < "2019") or (age < 32 and weight > 160)

    public TbGoodsExample() {
        oredCriteria = new ArrayList<Criteria>();
    }
    // getter setter 
    //创建一个Criteria(一组条件)
    protected Criteria createCriteriaInternal() {
        Criteria criteria = new Criteria();
        return criteria;
    }
  //创建一组条件,并添加到list条件中, 多个条件
    public Criteria or() {
        Criteria criteria = createCriteriaInternal();
        oredCriteria.add(criteria);
        return criteria;
    }
    //创建一组条件,并添加到空list条件中
    public Criteria createCriteria() {
        Criteria criteria = createCriteriaInternal();
        if (oredCriteria.size() == 0) {
            oredCriteria.add(criteria);
        }
        return criteria;
    }
    //条件清除
    public void clear() {
        oredCriteria.clear();
        orderByClause = null;
        distinct = false;
    }
   
}

GeneratedCriteria.java

package com.test.criteria;

import java.util.ArrayList;
import java.util.List;
/**
 * 静态内部类
 * 一组条件(多个Criterion,多个单个条件),比如 id=23 name="zhangsan" age = "34"
 * @author Administrator
 */
public abstract class GeneratedCriteria {
	//很多个条件
    protected List<Criterion> criteria;
    //子类Criteria创建对象的时候就创建多个条件的列表
    protected GeneratedCriteria() {
        super();
        criteria = new ArrayList<Criterion>();
    }
    //条件list不为空,就有效
    public boolean isValid() {
        return criteria.size() > 0;
    }
    //返回条件列表
    public List<Criterion> getAllCriteria() {
        return criteria;
    }
    //返回条件列表
    public List<Criterion> getCriteria() {
        return criteria;
    }
    //添加condition的novalue条件
    protected void addCriterion(String condition) {
        if (condition == null) {
            throw new RuntimeException("Value for condition cannot be null");
        }
        criteria.add(new Criterion(condition));
    }
    //添加condition, value(singleOrListValue),和property无关(只做提示用)
    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));
    }
    //添加condition, value,secondValue(betweenValue)   和property无关(只做提示用)
    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(Long value) {
        addCriterion("id =", value, "id");
        return (Criteria) this;
    }

    public Criteria andIdNotEqualTo(Long value) {
        addCriterion("id <>", value, "id");
        return (Criteria) this;
    }

    public Criteria andIdGreaterThan(Long value) {
        addCriterion("id >", value, "id");
        return (Criteria) this;
    }

    public Criteria andIdGreaterThanOrEqualTo(Long value) {
        addCriterion("id >=", value, "id");
        return (Criteria) this;
    }

    public Criteria andIdLessThan(Long value) {
        addCriterion("id <", value, "id");
        return (Criteria) this;
    }

    public Criteria andIdLessThanOrEqualTo(Long value) {
        addCriterion("id <=", value, "id");
        return (Criteria) this;
    }

    public Criteria andIdIn(List<Long> values) {
        addCriterion("id in", values, "id");
        return (Criteria) this;
    }

    public Criteria andIdNotIn(List<Long> values) {
        addCriterion("id not in", values, "id");
        return (Criteria) this;
    }

    public Criteria andIdBetween(Long value1, Long value2) {
        addCriterion("id between", value1, value2, "id");
        return (Criteria) this;
    }

    public Criteria andIdNotBetween(Long value1, Long value2) {
        addCriterion("id not between", value1, value2, "id");
        return (Criteria) this;
    }

    public Criteria andSellerIdIsNull() {
        addCriterion("seller_id is null");
        return (Criteria) this;
    }
   
}

Criterion.java

package com.test.criteria;

import java.util.List;
/**
 * 单个的条件,比如id = 23 是一个单个条件 name='张三'是一个单个条件 depno = '30'也是一个单个条件
 * @author Administrator
 *
 */
public class Criterion {
	 private String condition;//这是一个字符串,sql语句中条件的前一部分
	 /**
	  * eg:id is null,id >=, id between ,id in
	  * 
	  */

     private Object value;//singleValue listValue

     private Object secondValue;//value和secondValue都有值时,betweenValue

     private String typeHandler;
     
     private boolean noValue;

     private boolean singleValue;

     private boolean betweenValue;

     private boolean listValue;


     //get function
     //condition = condition,typeHandler = null,noValue = true
     protected Criterion(String condition) {
         super();
         this.condition = condition;
         this.typeHandler = null;
         this.noValue = true;
     }
     //condition = condition,value = value,singleOrListValue
     protected Criterion(String condition, Object value) {
         this(condition, value, null);
     }
     //condition = condition,typeHandler = typeHandler,value = value,singleOrListValue
     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;
         }
     }
     //condition = condition,value = value;secondValue = secondValue;betweenValue = true
     protected Criterion(String condition, Object value, Object secondValue) {
         this(condition, value, secondValue, null);
     }
     //condition = condition,typeHandler = typeHandler;value = value;secondValue = secondValue;betweenValue = true
     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;
     }

    
}

Criteria.java

package com.test.criteria;

import java.util.List;

/**
 * GeneratedCriteria是个抽象类所以没法创建对象,才用Criteria继承
 * 但是Criteria里面啥都没有啊,为什么不把GeneratedCriteria写成非抽象类呢?这个目前业没搞懂
 * @author Administrator
 */
public class Criteria  extends GeneratedCriteria {
    protected Criteria() {
        super();
    }
}

mapper.xml

<!-- 现在还有这个也没搞懂 <if test="_parameter != null" >
_parameter是参数,如果testParameter(Village village, ApiPort apiPort)  _parameter就是这2个参数,调用的时候可以#{_parameter.apiPort.id};(当然也可以village.id, apiPort.id 与_parameter互不影响) 如果testParameter(Village village),则调用的时候可以#{_parameter.id} _parameter就代表village, 

 -->
   <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" ><!-- 遍历List<Criteria> oredCriteria -->
        <if test="criteria.valid" ><!-- 判断是否为空 -->
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" ><!-- 遍历List<Criterion> criteria -->
              <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" ><!-- 如果是List类型的值 -->
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," ><!-- 循环该值,例id in (23,45) -->
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>

原TbGoodsExample代码

        其他三个类都在这里面

package com.pinyougou.pojo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class TbGoodsExample {
    protected String orderByClause;

    protected boolean distinct;

    protected List<Criteria> oredCriteria;

    public TbGoodsExample() {
        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;
        }

    }
    /**
     * 一组条件
     * @author Administrator
     *
     */
    public static class Criteria extends GeneratedCriteria {

        protected Criteria() {
            super();
        }
    }
    /**
     * 单个的条件
     * @author Administrator
     *
     */
    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);
        }
    }
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值