今天我要分享的是我项目中的模糊查询,它是根据监督部门名称和时间段查询数据,它可以单个条件进行查询,也可以组合起来查询数据。
数据表格
通过名称进行筛选数据
通过名称和时间段进行筛选数据
首先是时间段的input标签,我用的是layui的插件,通过ID绑定标签,让它可以选择日期。
var createTimeS = laydate.render({
elem : '#createTimeStart',
type : 'datetime',
max : nowTime,
done : function(value, date) {
endMax = createTimeE.config.max;
createTimeE.config.min = date;````````
createTimeE.config.min.month = date.month - 1;
}
});
elem是标签的ID名
type是类型,这里是datetime类型
max: nowTime是最大值为现在的时间,不能超出时间。
然后根据ID获取监督部门名称和开始时间与结束时间中的值,然后进行传参到controller控制器中。
active = {
search : function(){
var supervisiondepartmentsname = $('#mingcheng').val();
var createTimeStart = $('#createTimeStart').val();
var createTimeEnd = $('#createTimeEnd').val();
layuiTable.reload('employee',{
page : {
curr : 1
},
where : {
supervisiondepartmentsname : supervisiondepartmentsname,
createTimeStart : createTimeStart,
createTimeEnd : createTimeEnd,
}
});
}
};
传参到这个方法里面进行查询数据。
//查询数据
@RequestMapping("selectSuper")
@ResponseBody
public ResultUtil selectSuper(Integer page, Integer limit,SupervisionSearch Search){
ResultUtil admins = supervisiondepartmentService.selectSuper(page, limit,Search);
return admins;
}
在ServiceImpl层中对数据进行分页处理,这里用的是PageHelper进行分页。
然后对数据进行筛选并查询出来。
@Override
public ResultUtil selectSuper(Integer page, Integer limit,SupervisionSearch Search) {
PageHelper.startPage(page,limit);
List<Supervisiondepartments> Super=supervisiondepartmentsDAO.selectSuper(Search);
PageInfo<Supervisiondepartments> pageInfo=new PageInfo<Supervisiondepartments>(Super);
ResultUtil resultUtil = new ResultUtil();
resultUtil.setCode(0);
resultUtil.setCount(pageInfo.getTotal());
resultUtil.setData(pageInfo.getList());
return resultUtil;
}
下面是查询方法中的sql语句,先查询出监督部门这个表。
如果监督部门名称不等于null and不是空的字符串就查询出监督部门名称中相对应的数据。
然后执行下面的if语句,如果开始时间与结束时间不等于null and 开始时间与结束时间不是空的字符串就查询出相关时间的数据。
这样,当我们选择时间进行筛选数据或者通过名称进行模糊查询就可以得到我们想要的数据了。
<select id="selectSuper" resultMap="BaseResultMap" parameterType="com.gx.po.Supervisiondepartments">
select * from b_supervisiondepartments
<where>
1=1
<if test="null != supervisiondepartmentsname and '' != supervisiondepartmentsname">
and SupervisionDepartmentsName like "%"#{supervisiondepartmentsname}"%"
</if>
<if test="null != createTimeStart and null != createTimeEnd and '' != createTimeStart and '' != createTimeEnd">
and RegistrationDate between #{createTimeStart} and #{createTimeEnd}
</if>
<if test="'' != createTimeStart and '' == createTimeEnd">
and RegistrationDate between #{createTimeStart} and curtime()
</if>
<if test="'' == createTimeStart and '' != createTimeEnd">
and RegistrationDate between '1900-01-01 00:00:00' and #{createTimeEnd}
</if>
</where>