使用 foreach标签+List「Map」 出现 Source is null for getProperty(null,“XXX“)异常解决

    技术2025-06-06  14

    伪代码…

    EmployeeController

    @RequestMapping("/xxx/xxx") @ResponseBody public List employeeList(HttpServletRequest req, HttpServletResponse resp) { Map map= new HashMap(); map.put("department_id",department_id); List list = EmployeeService.findEmployee(map); return list; }

    EmployeeService接口

    List findEmployee(Map map);

    EmployeeServiceImpl实现类

    @Autowired private UserDao userDao; public List findEmployee(Map map) { return EmployeeDao.findEmployee(map); }

    EmployeeDao接口

    public List<Map> findEmployee(Map map);

    EmployeeDao.xml 重点是在下面,报错原代码

    <if test="department_id !='' and department_id = 'xxx'"> and sys_user.department_id in <foreach collection="map" item="deptId" open="(" separator="," close=")"> #{deptId} </foreach> </if> <if test="department_id !='' and department_id != 'xxx'"> and user.department_id in <foreach collection="map" item="deptId" open="(" separator="," close=")"> #{deptId} </foreach> </if>

    解决:在if标签外加一层判断

    <if test="map != null"> <if test="department_id !='' and department_id = 'xxx'"> and sys_user.department_id in <foreach collection="map" item="deptId" open="(" separator="," close=")"> #{deptId} </foreach> </if> </if> <if test="map!=null"> <if test="department_id !='' and department_id != 'xxx'"> and sys_user.department_id in <foreach collection="map" item="deptId" open="(" separator="," close=")"> #{deptId} </foreach> </if> </if>

    查了一上午,太浪费时间了。。。

    Processed: 0.011, SQL: 9