Mybatis引數為物件中包含list情況處理
阿新 • • 發佈:2019-02-11
mybatis是一個非常好用且靈活的持久層框架,但也正是因為太過靈活,導致有時候引數很難整理。我把我在專案中遇到的一個特殊情況列出來,希望下次再碰到時,也有個印象。
實體類如下:
這裡的實體類中,包含了一個List物件,所以在寫xml檔案的時候多少對我這種菜鳥有點難度。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; } }
<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的下標,這樣即可將物件中的所有資料取出。