<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>
  <sql id="Update_By_Example_Where_Clause" >
    <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>

解决方案 »

  1.   

    //生成規則    只有一個屬性:就是規則集合
        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;
            }
            //添加一個條件     String型條件
            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 andIdEqualTo(Integer value) {
                addCriterion("id =", value, "id");
                return (Criteria) this;
            }
            public Criteria andIdNotEqualTo(Integer value) {
                addCriterion("id <>", value, "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 andMenoLessThanOrEqualTo(String value) {
                addCriterion("meno <=", value, "meno");
                return (Criteria) this;
            }        public Criteria andMenoLike(String value) {
                addCriterion("meno like", value, "meno");
                return (Criteria) this;
            }        public Criteria andMenoNotLike(String value) {
                addCriterion("meno not like", value, "meno");
                return (Criteria) this;
            }        public Criteria andMenoIn(List<String> values) {
                addCriterion("meno in", values, "meno");
                return (Criteria) this;
            }        public Criteria andMenoNotIn(List<String> values) {
                addCriterion("meno not in", values, "meno");
                return (Criteria) this;
            }        public Criteria andMenoBetween(String value1, String value2) {
                addCriterion("meno between", value1, value2, "meno");
                return (Criteria) this;
            }        public Criteria andMenoNotBetween(String value1, String value2) {
                addCriterion("meno not between", value1, value2, "meno");
                return (Criteria) this;
            }        public Criteria andUrlIsNull() {
                addCriterion("url is null");
                return (Criteria) this;
            }        public Criteria andUrlIsNotNull() {
                addCriterion("url is not null");
                return (Criteria) this;
            }        public Criteria andUrlEqualTo(String value) {
                addCriterion("url =", value, "url");
                return (Criteria) this;
            }        public Criteria andUrlNotEqualTo(String value) {
                addCriterion("url <>", value, "url");
                return (Criteria) this;
            }        public Criteria andUrlGreaterThan(String value) {
                addCriterion("url >", value, "url");
                return (Criteria) this;
            }        public Criteria andUrlGreaterThanOrEqualTo(String value) {
                addCriterion("url >=", value, "url");
                return (Criteria) this;
            }        public Criteria andUrlLessThan(String value) {
                addCriterion("url <", value, "url");
                return (Criteria) this;
            }        public Criteria andUrlLessThanOrEqualTo(String value) {
                addCriterion("url <=", value, "url");
                return (Criteria) this;
            }        public Criteria andUrlLike(String value) {
                addCriterion("url like", value, "url");
                return (Criteria) this;
            }        public Criteria andUrlNotLike(String value) {
                addCriterion("url not like", value, "url");
                return (Criteria) this;
            }        public Criteria andUrlIn(List<String> values) {
                addCriterion("url in", values, "url");
                return (Criteria) this;
            }        public Criteria andUrlNotIn(List<String> values) {
                addCriterion("url not in", values, "url");
                return (Criteria) this;
            }        public Criteria andUrlBetween(String value1, String value2) {
                addCriterion("url between", value1, value2, "url");
                return (Criteria) this;
            }        public Criteria andUrlNotBetween(String value1, String value2) {
                addCriterion("url not between", value1, value2, "url");
                return (Criteria) this;
            }        public Criteria andPriorityIsNull() {
                addCriterion("priority is null");
                return (Criteria) this;
            }        public Criteria andPriorityIsNotNull() {
                addCriterion("priority is not null");
                return (Criteria) this;
            }        public Criteria andPriorityEqualTo(Integer value) {
                addCriterion("priority =", value, "priority");
                return (Criteria) this;
            }        public Criteria andPriorityNotEqualTo(Integer value) {
                addCriterion("priority <>", value, "priority");
                return (Criteria) this;
            }        public Criteria andPriorityIn(List<Integer> values) {
                addCriterion("priority in", values, "priority");
                return (Criteria) this;
            }        public Criteria andPriorityNotIn(List<Integer> values) {
                addCriterion("priority not in", values, "priority");
                return (Criteria) this;
            }        public Criteria andPriorityBetween(Integer value1, Integer value2) {
                addCriterion("priority between", value1, value2, "priority");
                return (Criteria) this;
            }        public Criteria andPriorityNotBetween(Integer value1, Integer value2) {
                addCriterion("priority not between", value1, value2, "priority");
                return (Criteria) this;
            }
            public Criteria andTypeEqualTo(Integer value) {
                addCriterion("`type` =", value, "type");
                return (Criteria) this;
            }        public Criteria andTypeNotEqualTo(Integer value) {
                addCriterion("`type` <>", value, "type");
                return (Criteria) this;
            }        public Criteria andTypeGreaterThan(Integer value) {
                addCriterion("`type` >", value, "type");
                return (Criteria) this;
            }         public Criteria andTypeIn(List<Integer> values) {
                addCriterion("`type` in", values, "type");
                return (Criteria) this;
            }        public Criteria andTypeNotIn(List<Integer> values) {
                addCriterion("`type` not in", values, "type");
                return (Criteria) this;
            }        public Criteria andTypeBetween(Integer value1, Integer value2) {
                addCriterion("`type` between", value1, value2, "type");
                return (Criteria) this;
            }        public Criteria andTypeNotBetween(Integer value1, Integer value2) {
                addCriterion("`type` not between", value1, value2, "type");
                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 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 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;
            }
      

  2.   

     //准则,标准
        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;
            }
            //构造方法,通过String条件
            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;
                //判断value是否符合list集合,符合则为集合    不符合,则是单条记录
                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);
            }
        }
      

  3.   

    //这个类   包含前面两个类(前面是这个里面的内部类) pojoExample
    package com.pojo;
    import java.util.ArrayList;
    import java.util.List;public class ResourceTblExample {
        protected String pk_name = "id";    protected String orderByClause;    protected boolean distinct;
        //一個ArrayList裏面保存Criteria
        protected List<Criteria> oredCriteria;    public ResourceTblExample() {
            oredCriteria = new ArrayList<Criteria>();
        }    public void setPk_name(String pk_name) {
            this.pk_name = pk_name;
        }    public String getPk_name() {
            return pk_name;
        }    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;
        }
    }