Home  >  Q&A  >  body text

mybatis - Why can't the Java backend filter out objects using Boolean attributes, but can be changed to String type?

 public JSONArray getTreeNodes(String departmentSn) {
        Department department = new Department();
        JSONArray jsonArray = new JSONArray();
        if (!departmentSn.equals("-1")) {
            department.setParentDepartmentSn(departmentSn);
        } else {
//这里将department的一个布尔属性设置为true
         department.setHasActivated(true);
        }
//在这里进行筛选
        List<Department> departments = departmentMapper.select(department);
        System.out.print(departments);
        for (Department DEP : departments) {
            Department d = new Department();
            Department sonDepartment = departmentMapper.selectByPrimaryKey(DEP.getDepartmentSn());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("label", sonDepartment.getDepartmentName());
            jsonObject.put("data", sonDepartment.getDepartmentSn());
            d.setParentDepartmentSn(DEP.getDepartmentSn());
            if (departmentMapper.selectCount(department) == 0) {
                jsonObject.put("leaf", true);
            } else {
                jsonObject.put("leaf", false);
            }
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }
}

In the above code, when I pass in a departmentSn that is not -1, it does not enter the else branch. However, after the following filtering, the result set is empty, as shown below


The size here is 0.
And when I change the boolean attribute in the entity class to a string attribute (as shown in the picture)

before fixing

After modification

The filtering results are normal.

The size here is 8.
Why is this?
In addition, I also tried to manually change the boolean The attribute is assigned to false, and the result is also empty. As shown below.

为情所困为情所困2686 days ago973

reply all(1)I'll reply

  • typecho

    typecho2017-06-12 09:23:52

    Don’t use basic types for entity classes. You can try Boolean first. If you have any problems, post the SQL and take a look.

    reply
    0
  • Cancelreply