mybatis是一个非常好用且灵活的持久层框架,但也正是因为太过灵活,导致有时候参数很难整理。我把我在项目中遇到的一个特殊情况列出来,希望下次再碰到时,也有个印象。
实体类如下:
package com.kxlive.erp.sc.stock.vo;
import java.util.Date;
import java.util.List;
import org.springframework.format.annotation.DateTimeFormat;
import com.kxlive.erp.sc.stock.po.PurchaseDeliver;
public class QueryPurchaseDeliverVo extends PurchaseDeliver{
private Long merchantId;
private Long stockId;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date startTime;//发货开始时间
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date endTime;//发货结束时间
private Integer page;//当前页
private Integer rows;//每页记录数
private String stockName;//仓库名
private String supplierName;//供应商名称
private List<Long> condSupplierIds;//供应商IDs
private List<Integer> condStatuss;//状态s
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getRows() {
return rows;
}
public void setRows(Integer rows) {
this.rows = rows;
}
public String getStockName() {
return stockName;
}
public void setStockName(String stockName) {
this.stockName = stockName;
}
public String getSupplierName() {
return supplierName;
}
public void setSupplierName(String supplierName) {
this.supplierName = supplierName;
}
public List<Long> getCondSupplierIds() {
return condSupplierIds;
}
public void setCondSupplierIds(List<Long> condSupplierIds) {
this.condSupplierIds = condSupplierIds;
}
public List<Integer> getCondStatuss() {
return condStatuss;
}
public void setCondStatuss(List<Integer> condStatuss) {
this.condStatuss = condStatuss;
}
public Long getMerchantId() {
return merchantId;
}
public void setMerchantId(Long merchantId) {
this.merchantId = merchantId;
}
public Long getStockId() {
return stockId;
}
public void setStockId(Long stockId) {
this.stockId = stockId;
}
}
这里的实体类中,包含了一个List对象,所以在写xml文件的时候多少对我这种菜鸟有点难度。
<select id="selectPurchaseDeliverByPrimaryKeySelective" resultMap="purchaseResultMap">
select
t2. name stock_name,
t3. supplier_name, t1.*
from
T_SC_PURCHASE_DELIVER t1
left join T_SC_STOCK t2 on t1.purchase_stock_id = t2.id
left join T_SC_SUPPLIER t3 on t1.supplier_id = t3.id
<where>
<if test="queryCondition.ifDel != null">
and t1.if_del=#{queryCondition.ifDel,jdbcType=INTEGER}
</if>
<if test="queryCondition.condSupplierIds != null">
and t1.supplier_id in
<foreach collection="queryCondition.condSupplierIds" item="supplierId" index="index" separator="," open="(" close=")">
#{queryCondition.condSupplierIds[${index}],jdbcType=BIGINT}
</foreach>
</if>
<if test="queryCondition.condStatuss != null">
and t1.status in
<span style="color:#ff0000;"><foreach collection="queryCondition.condStatuss" item="status" separator="," index="index" open="(" close=")">
#{queryCondition.condStatuss[${index}],jdbcType=INTEGER}
</foreach></span>
</if>
<if test="queryCondition.merchantId != null">
and t1.merchant_id=#{queryCondition.merchantId,jdbcType=BIGINT}
</if>
<if test="queryCondition.stockId != null">
and t1.purchase_stock_id=#{queryCondition.stockId,jdbcType=BIGINT}
</if>
<if test="queryCondition.startTime != null">
<![CDATA[ and DATE_FORMAT(t1.create_time, '%Y-%m-%d') >= DATE_FORMAT(#{queryCondition.startTime}, '%Y-%m-%d')]]>
</if>
<if test="queryCondition.endTime != null">
<![CDATA[ and DATE_FORMAT(t1.create_time, '%Y-%m-%d') <= DATE_FORMAT(#{queryCondition.endTime}, '%Y-%m-%d')]]>
</if>
<if test="queryCondition.name != null and queryCondition.name != '' ">
and t1.name like CONCAT('%', #{queryCondition.name, jdbcType=VARCHAR}, '%')
</if>
<if test="queryCondition.deliverNo != null">
and t1.deliver_no like CONCAT('%', #{queryCondition.deliverNo, jdbcType=VARCHAR}, '%')
</if>
</where>
order by t1.create_time desc
</select>
标红的地方,即为应用。在foreach循环中,引用index作为list的下标,这样即可将对象中的所有数据取出。