Ibatis取多表多条件查询记录时,遇到了问题??
条件参数:采用了一个实体类
返回结果集:采用的是HashMap
实现分页:采用了高人已经写好的分页类(Paginated),采用的是反射.适用于Spring+ibatis开发.
问题一:我的条件参数放在实体类(MaterialMap),当我用下面这个方法传参数时ibatis映射文件能取参数,但是执行结果是'SQL语句错误'.
实体类:
package com.exeerp.qm.bean; /** * 原料检验多表采集数据实体类 * @author 谢刚 * */ @SuppressWarnings("serial") public class MaterialMap { /** * 原料检验ID */ private String qmMaTransferCheckID; /** * 原料名称 */ private String rdMaName; /** * 批号 */ private String stMaBatchCode; /** * 供应商 */ private String bmSuName;
Action类:
@SuppressWarnings({ "finally", "unchecked" }) public String getMaterialList(){ String result=ERROR; MaterialMap map=new MaterialMap(); try{ map.setQmMaTransferCheckID("20090508193711238"); map.setRdMaName(rdMaName); map.setStMaBatchCode(BatchCode); map.setBmSuName(bmSuName); paging=materialManager.getListForPage(map, 10, currentPage, "materiallList.do"); ArrayList list=(ArrayList)paging.getRecordList(); this.getRequest().setAttribute("hashMapList", list); result=SUCCESS; }catch(Exception ex){ this.setMessage(ex.getMessage()); ex.printStackTrace(); }finally{ return result; } }
Ibatis映射:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="qm"> <typeAlias alias="HashMap" type="java.util.HashMap" /> <typeAlias alias="MaterialMap" type="com.exeerp.qm.bean.MaterialMap" /> <select id="selectForPage-MaterialMap" remapResults="true" parameterClass="Map" resultClass="HashMap"> <![CDATA[ select * from ( select qmMa.Materialcheckid,rdMak.Name as rdMakName,rdMa.Name as rdMaName,rdMa.Specs, bmSu.Name as bmSuName,qmMa.manufactureDate,qmMa.checkDate,qmMa.checker, qmMa.checkResult,qmMa.auditer,qmMa.status,rownum rn from QM_MATERIAL qmMa,ST_TRANSFERCHECK stTr,ST_MATERIAL stMa, ST_MATERIALBOOKIN stMab,BM_SUPPLIER bmSu,BM_SPRODUCT bmSp, RD_Material rdMa,RD_MATERIALKIND rdMak ]]> <dynamic prepend="where"> qmMa.TransferCheckID=stTr.TransferCheckID and stTr.ST_MaterialID=stMa.ST_MaterialID and stMa.BookID=stMab.BookID and stMab.SupplierID=bmSu.SupplierID and bmSu.SupplierID=bmSp.SupplierID and bmSp.MaterialID=rdMa.MaterialID and rdMa.Kind=rdMak.Kind <isNotEmpty prepend="and" property="qmMaTransferCheckID"> and qmMa.MaterialcheckID=#qmMaTransferCheckID# </isNotEmpty> <isNotEmpty prepend="and" property="rdMaName"> and rdMa.Name like '%$rdMaName$%' </isNotEmpty> <isNotEmpty prepend="and" property="stMaBatchCode"> and stMa.BatchCode=#stMaBatchCode# </isNotEmpty> <isNotEmpty prepend="and" property="bmSuName"> and bmSu.Name like '%$bmSuName$%' </isNotEmpty> </dynamic>
解决办法:
<isNotEmpty prepend="and" property="qmMaTransferCheckID"> and qmMa.MaterialcheckID=#qmMaTransferCheckID# </isNotEmpty> <isNotEmpty prepend="and" property="rdMaName"> and rdMa.Name like '%$rdMaName$%' </isNotEmpty> <isNotEmpty prepend="and" property="stMaBatchCode"> and stMa.BatchCode=#stMaBatchCode# </isNotEmpty> <isNotEmpty prepend="and" property="bmSuName"> and bmSu.Name like '%$bmSuName$%' </isNotEmpty>
只不过加了个and ,开始我以为'prepend'属性会自动加'and',属性property='实体对象属性'.总之对东西了解不够,浪费了很多时间.