Springboot+Mybatis接收react前端POST请求查询不到结果

后台可以收到请求的json数据,用相同的数据单独测试后台也可以搜到结果。但是直接从react前端发送请求,搜索结果就为空。
单独测试后台StaffManagementController,main方法中创建staffAO对象department=chemistry, 可以搜索到staffVOList.size=6个结果。从react前端表单department输入chemistry,发送POST请求,搜索到结果却是staffVOList.size=0个结果. esclipse后台console也显示staffAO.department=chemistry,说明前端请求可以正确接收。但是不知道为什么搜索不到结果。问题应该出在后台,但是完全想不出还哪里能出问题。。
下面是controller,function,mapper代码。希望有高手指教,非常感谢!

 @RestController
@RequestMapping("/api")
public class StaffManagementController {
    private StaffManagementFunction function;

    @PostMapping("staffs/find")
    List<StaffManagementViewObject> getStaffByKeys(@Valid @RequestBody StaffManagementArgumentObject staffAO) throws Exception {
        log.info("Request to search for staff: {}", staffAO);
        List<StaffManagementViewObject> staffs = function.findStaffByKeys(staffAO);
        if (staffs == null) {
            new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
        return staffs;
 @Service
public class StaffManagementFunctionImpl implements StaffManagementFunction{
 @Override
    public  List<StaffManagementViewObject> findStaffByKeys(StaffManagementArgumentObject staffManagementArgumentObject) throws Exception {
        SqlSession session = MyBatisUtil.getSession();
        StaffManagementMapper smMapper = session.getMapper(StaffManagementMapper.class);

        if(staffManagementArgumentObject==null){
            throw new IllegalArgumentException("argumentObject cannot be blank");
        }
        String genderInput = staffManagementArgumentObject.getGender();
        String gender= (genderInput==null)?null:(genderInput=="male")?"true":"false";

        staffManagementArgumentObject.setGender(gender);

        List<StaffManagement> staffList = smMapper.selectKeysLike(staffManagementArgumentObject);
        MyBatisUtil.closeSession();

        List<StaffManagementViewObject> staffVOList = new ArrayList<StaffManagementViewObject> ();
        for(int i = 0 ; i < staffList.size() ; i++)  {
            StaffManagement staff = staffList.get(i);
            String genderVO = AssertThrowUtil.parseGenderToString(staff.getGender());
            String yearOfGraduationVO = AssertThrowUtil.parseYearToString(staff.getYear

            StaffManagementViewObject staffVO = new StaffManagementViewObject();
            staffVO.setId(staff.getId());
            staffVO.setName(staff.getName());
            staffVO.setGender(genderVO);
            staffVO.setDepartment(staff.getDepartment());
            staffVO.setYearOfGraduation(yearOfGraduation);
            staffVOList.add(staffVO);
        }
        return staffVOList;
    }
    StaffManagementMapper.xml 
        <select id="selectKeysLike" parameterType="com.springboot.demo.controller.StaffManagementArgumentObject" resultMap="BaseResultMap"> 
    select * from staff_management where 
            Name like '%${name}%'
            AND Gender like '%${gender}%' 
            AND Department like '%${department}%' 
            AND Position like '%${position}%' 
                AND Year_of_graduation like '%${yearOfGraduation}%'
  </select>
查看全部
wzhuwzhu
wzhuwzhu
2018/11/27 00:26
  • mybatis
  • 点赞
  • 收藏
  • 回答
    私信

1个回复