人员列表条件查询

人员列表条件查询

  • controller
//人员模块表现实现类
@WebServlet(name = "PersonnelController",urlPatterns = "/personnel.do",loadOnStartup = 0
)
public class PersonnelController extends HttpServlet {//表现层中获取逻辑层实现类private PersonnelService personnelService = PersonnelFactory.getPersonnelService();//查询数据和添加数据都用到所以写在前面private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//定义Servlet运行方法, 接收客户端请求, 返回服务器响应@Overrideprotected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//当前页面int current = 1;//list.jsp获得的页面和Java的连接String currentStr = request.getParameter("current");if (Validator.isNotEmpty(currentStr) && Validator.isInteger(currentStr)) {current = Integer.parseInt(currentStr);}//每页条数int size = 10;//起始条数int start = (current - 1) * size;//封装查询参数Map<String, Object> params = new HashMap<>();params.put("start", start);params.put("size", size);//查询nameparams.put("name", request.getParameter("name"));//查询cardparams.put("card", request.getParameter("card"));//查询起薪时间String startDate = request.getParameter("start");String endDate = request.getParameter("end");//验证两个参数是否有值if (Validator.isNotEmpty(startDate) && Validator.isNotEmpty(endDate)) {//日期必须是yyyy-MM-ddif (Validator.isDate(startDate) && Validator.isDate(endDate)) {try {params.put("startDate", format.parse(startDate));params.put("endDate", format.parse(endDate));} catch (ParseException e) {e.printStackTrace();}}}//工作状态params.put("state", request.getParameter("state"));//车辆补贴params.put("vehiclesSubsidies", request.getParameter("vehiclesSubsidies"));//供暖补贴params.put("heatingSubsidies", request.getParameter("heatingSubsidies"));//物业补贴params.put("propertySubsidies", request.getParameter("propertySubsidies"));//医疗保险params.put("healthInsurance", request.getParameter("healthInsurance"));//养老保险params.put("endowmentInsurance", request.getParameter("endowmentInsurance"));//养老失业params.put("pensionUnemployment", request.getParameter("pensionUnemployment"));//职业年金params.put("occupationalPension", request.getParameter("occupationalPension"));//工伤生育params.put("injuryFertility", request.getParameter("injuryFertility"));//查询数据库总条数long count = personnelService.queryByCount(params);//最大页数int max = (int) Math.ceil(count * 1.0 / size);//1.查询人员列表 queryByPage:分页查询List<Personnel> list = personnelService.queryByPage(params);//2.写入请求作用域request.setAttribute("list", list);request.setAttribute("current", current);request.setAttribute("max", max);request.setAttribute("count", count);request.setAttribute("start", start);request.setAttribute("size", size);//数据回显request.setAttribute("params", params);//3.分发器跳转页面request.getRequestDispatcher("/view/personnel/list.jsp").forward(request, response);}
}
  • service
//人员模块逻辑层接口
public interface PersonnelService {//分页查询List<Personnel> queryByPage(Map<String, Object> params);//查询条数long queryByCount(Map<String, Object> params);
}
//人员模块逻辑层实现类
public class PersonnelServiceImpl implements PersonnelService {//逻辑层中获取数据层实现类private PersonnelDao personnelDao = PersonnelFactory.getPersonnelDao();//分页查询@Overridepublic List<Personnel> queryByPage(Map<String, Object> params) {return personnelDao.queryByPage(params);}//查询条数@Overridepublic long queryByCount(Map<String, Object> params) {return personnelDao.queryByCount(params);}
}
  • dao
//人员数据层接口
public interface PersonnelDao {//分页查询List<Personnel> queryByPage(Map<String, Object> params);//查询条数long queryByCount(Map<String, Object> params);
}
//人员模块数据层实现类
public class PersonnelDaoImpl implements PersonnelDao {//构造方法public PersonnelDaoImpl() {}//懒汉模式private static PersonnelDaoImpl instance = null;public static PersonnelDaoImpl getInstance() {//双重校验锁:保证不会出现并发if (instance == null) {synchronized (PersonnelDaoImpl.class) {if (instance == null) {instance = new PersonnelDaoImpl();}}}return instance;}//分页查询@Overridepublic List<Personnel> queryByPage(Map<String, Object> params) {//查询数据库SqlSession session = MybatisUtils.openSession();//分页查询:两个参数:第一个找select语句,第二个找参数List<Personnel> list = session.selectList("personnel.queryByPage", params);session.close();return list;}@Override//查询条数public long queryByCount(Map<String, Object> params) {//查询数据库SqlSession session = MybatisUtils.openSession();//selectOne:返回一行long count = session.selectOne("personnel.queryByCount", params);session.close();return count;}
}
  • xml

DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="personnel"><select id="queryByPage" parameterType="Map" resultType="com.javakc.rms.personnel.entity.Personnel">select id,name,unit,card,grade,starting_date,state,contract,vehicles_subsidies,heating_subsidies,property_subsidies,health_insurance,endowment_insurance,pension_unemployment,occupational_pension,injury_fertility,reason from rms_personnel<where><if test="name!=null and name!='' ">name like concat('%',#{name},'%')if><if test="card!=null and card!='' ">and card=#{card}if><if test="startDate!=null and endDate!=null ">and starting_date between #{startDate} and #{endDate}if><if test="state!=null and state!=0 ">and state =#{state}if><if test="vehiclesSubsidies!=null and vehiclesSubsidies==1 ">and vehicles_subsidies=1if><if test="heatingSubsidies!=null and heatingSubsidies==1 ">and heating_subsidies=1if><if test="healthInsurance!=null and healthInsurance==1 ">and health_insurance=1if><if test="endowmentInsurance!=null and endowmentInsurance==1 ">and endowment_insurance=1if><if test="pensionUnemployment!=null and pensionUnemployment==1 ">and pension_unemployment=1if><if test="occupationalPension!=null and occupationalPension==1 ">and occupational_pension=1if><if test="injuryFertility!=null and injuryFertility==1 ">and injury_fertility=1if>where>limit #{start},#{size}select><select id="queryByCount" parameterType="Map" resultType="Long">select count(1) from rms_personnel<where><if test="name!=null and name!='' ">name like concat('%',#{name},'%')if><if test="card!=null and card!='' ">and card=#{card}if><if test="startDate!=null and endDate!=null ">and starting_date between #{startDate} and #{endDate}if><if test="state!=null and state!=0 ">and state =#{state}if><if test="vehiclesSubsidies!=null and vehiclesSubsidies==1 ">and vehicles_subsidies=1if><if test="heatingSubsidies!=null and heatingSubsidies==1 ">and heating_subsidies=1if><if test="propertySubsidies!=null and propertySubsidies==1 ">and property_subsidies=1if><if test="healthInsurance!=null and healthInsurance==1 ">and health_insurance=1if><if test="endowmentInsurance!=null and endowmentInsurance==1 ">and endowment_insurance=1if><if test="pensionUnemployment!=null and pensionUnemployment==1 ">and pension_unemployment=1if><if test="occupationalPension!=null and occupationalPension==1 ">and occupational_pension=1if><if test="injuryFertility!=null and injuryFertility==1 ">and injury_fertility=1if>where>select>
mapper>


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部