目錄
使用示例
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);
}
}
}