有个需求需要实现多条件查询,然后mybaits使用map进行参数的传递,刚开始的时候一直报错:Caused by: org.apache.ibatis.binding.BindingException: Parameter ‘pcode’ not fond,就是说我的参数一直传不进去。然后去搜了一下,才发现我的map传参有问题
接下来直接上代码吧
//controller层
public Result<?> searchPolice(PageInfo pageInfo, @RequestBody JSONObject jsonObject) {
Result<Page<PoliceInfo>> result = new Result<>();
try {
Map<String, Object> map = jsonObject.getInnerMap();
Page<PoliceInfo> page = new Page<>(pageInfo.getCurrentPage(), pageInfo.getPageSize());
List<PoliceInfo> list = policeInfoService.getPoliceInfoByConditions(page, map);
page.setRecords(list);
result.setSuccess(true);
result.setCode(200);
result.setResult(page);
} catch (Exception e) {
result.error500("查询失败" + e.getMessage());
log.error(e.getMessage(), e);
}
return result;
}
//service层
public List<PoliceInfo> getPoliceInfoByConditions(Page page, Map<String, Object> map) {
return policeInfoMapper.getPoliceInfoByConditions(page, map);
}
//mapper层
List<PoliceInfo> getPoliceInfoByConditions(Page page, @Param("params") Map<String, Object> map);
//xml文件
<select id="getPoliceInfoByConditions" parameterType="java.util.Map"
resultType="org.jeecg.modules.schedule.entity.PoliceInfo">
select * from XXX where DEPT=#{
params.dept}
<if test="params.pcode!= null and params.pcode !=''">
and pcode=#{
params.pcode}
</if>
<if test="params.name != null and params.name !=''">
and name=#{
params.name}
</if>
<if test="params.xb != null and params.xb !=''">
and xb=#{
params.xb}
</if>
<if test="params.sfzhm != null and params.sfzhm !=''">
and sfzhm=#{
params.sfzhm}
</if>
<if test="params.zw != null and params.zw !=''">
and zw=#{
params.zw}
</if>
<if test="params.gs != null and params.gs !=''">
and gs=#{
params.gs}
</if>
<if test="params.zc != null and params.zc !=''">
and zc=#{
params.zc}
</if>
<if test="params.blyx != null and params.blyx !=''">
and blyx=#{
params.blyx}
</if>
<if test="params.zl != null and params.zl !=''">
and zl=#{
zl}
</if>
<if test="params.sj != null and params.sj !=''">
and sj=#{
sj}
</if>
<if test="params.iszzjy != null and params.iszzjy !=''">
and iszzjy=#{
iszzjy}
</if>
<if test="params.age1 != null and params.age1 !='' and params.age2 != null and params.age2 !=''">
and nl between #{
params.age1} and #{
params.age2}
</if>
</select>
记录一下第一次用map传参留下的坑~