我们这个直接看源码看如何由example映射到sql语句
Stock.java
private Integer id;
private String name;
private Integer count;
private Integer sale;
private Integer version;
只有5个属性。
StockMapper.java:
public interface StockMapper {
long countByExample(StockExample example);
}
我看只看countByExample接口,看名字就知道是返回给定example参数的结果个数。
再看
StockMapper.xml该方法实现:
<select id="countByExample" parameterType="com.example.model.mapper.StockExample" resultType="java.lang.Long">
select count(*) from stock
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
很简单给定参数类型StockExample 返回Long类型 select count(*) from stock 返回满足条件的stock表中数据数量
再看<include refid="Example_Where_Clause" />
<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>
foreach表示循环,分隔符是or(或者)重点在于数据集oredCriteria,我们去给定参数StockExample中寻找oredCriteria
protected List<Criteria> oredCriteria;
是一个List刚好和上面的集合对应了,看看单个Criteria是什么
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
再看GeneratedCriteria
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList<Criterion>();
}
}
我们发现Criteria里面居然还有一个list,我们翻上去看刚好与上面一个foreach里面还有一个foreach对应,那接下来研究Criterion
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;
}
这就是该类的声明,里面有这些值,这时候应该结合着之前xml第二层foreach属性看了,知道这些值有啥用
<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>
一看我们根据when知道有四类情况,noValue,singleValue,betweenValue,listValue,对应着查询中四种情况
1.只是单纯是否非空
2.value=?多少查询
3.两者之间查询
4.在list中查询。
所以我们构建的查询参数就是criterion,看一下上面四种咋构建吧
以下均是lGeneratedCriterial类中方法,addCriterion是把生成的Criterion加到Ctriteria中
1.是否非空:
public Criteria andIdIsNull() {
addCriterion("id is null");
return (Criteria) this;
}
2.值查询
public Criteria andIdEqualTo(Integer value) {
addCriterion("id =", value, "id");
return (Criteria) this;
}
3.两者之间
public Criteria andSaleNotBetween(Integer value1, Integer value2) {
addCriterion("sale not between", value1, value2, "sale");
return (Criteria) this;
}
4.list
public Criteria andVersionNotIn(List<Integer> values) {
addCriterion("version not in", values, "version");
return (Criteria) this;
}
完结,相信通过上面的源码剖析,大家都知道如何构建查询条件了把。
总结
StockExample.java 构造
public class StockExample {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
}
三个参数
orderByClause:返回结果升降排序,字段+空格+acs/dcs(升序/降序)例子count acs
setOrderByClause方法注入
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
distinct:true/false 返回结果去不去重复,setDistinct注入
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
oredCriteria:就是查询参数,之前狗讲了啥意思,构造方法
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
构造Criteria
StockExample.Criteria criteria=stockExample.createCriteria();
criteria.andCountEqualTo(1);
收工。