在沒有critia条件下的应用
<select id="selectDataByDifferentTime"resultMap="BaseResultMap1" parameterType="com.adtec.db.yizooo.entity.ReceiptExample">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List"/>
from plat2_receipt
where
addressis not null
and
part_type= "01"
<if test="receiptTime=0">
and
<![CDATA[ DATE_SUB(CURDATE(), INTERVAL 7 DAY) <=date(receipt_time) ]]>
</if>
<if test="receiptTime=1">
and
DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date(receipt_time)
</if>
<if test="receiptType=1">
and
logistic_codeis not null
</if>
<if test="receiptType=0">
and
logistic_codeis null
</if>
<if test="orderByClause !=null" >
order by ${orderByClause}
</if>
limit #{offset}, #{rows}
</select>
以上是mapper.xml中的文件
Example文件中
privateStringreceiptType;
publicStringgetReceiptType() {
returnreceiptType;
}
publicvoidsetReceiptType(StringreceiptType) {
this.receiptType = receiptType;
}
privateStringrecriptTime;
publicStringgetRecriptTime() {
returnrecriptTime;
}
mapper.xml中sql语句用到的<if test=”字段”>必须和exmaple中定义的字段一致即实体类中的字段
在业务层中
ReceiptExampleexample=newReceiptExample();
example.setOffset(offset);
example.setRows(rows);
example.setOrderByClause("receipt_time desc");
// Criteria cri=example.createCriteria();
// cri.andAddressIsNotNull();
// cri.andPartTypeEqualTo(Constants.DIFFERENT_DRAW);
if(StringUtils.hasText(po.getReceiptType())){
if(po.getReceiptType().equals("0"))
{
example.setReceiptType("0");
}elseif(po.getReceiptType().equals("1"))
{
example.setReceiptType("1");
}
}
if (po.getReceiptTime()!=null || !"".equals(po.getReceiptTime())) {
if (po.getReceiptTime().equals("0")) {
example.setRecriptTime("0");
}elseif(po.getReceiptTime().equals("1")) {
example.setRecriptTime("1");
}
}
List<ReceiptPo> receiptList=receiptMapper.selectDataByDifferentTime(example);
for(ReceiptPoreceipt : receiptList)//遍历receiptList
{
//
DrawPodrawPo=drawMapper.selectByMyDrawLucy(receipt.getPeriodId());
intbetCnt = drawBetService.sumDrawBetCnt(receipt.getPeriodId(), receipt.getCid());
receipt.setCmdtName(drawPo.getCmdtName());
receipt.setBetCount(betCnt);
}
在业务层中
if(StringUtils.hasText(po.getReceiptType())){
if(po.getReceiptType().equals("0"))
{
example.setReceiptType("0");
}elseif(po.getReceiptType().equals("1"))
{
example.setReceiptType("1");
}
}
通过上述代码决定mapper.xml中的文件sql语句执行条件