1. 程式人生 > >springboot+mybatis 模糊搜尋

springboot+mybatis 模糊搜尋

首先看是單表查詢還是多表查詢

1.單表查詢  

      使用mybatisGeneratorConfig.xml逆向生成實體類和mapper,在 Service中使用Example進行模糊搜尋

/**
 * 根據地理位置和分類篩選banner
 * @param geolocation
 * @param category
 * @return
 */
public List<BannerAllDO> listByCategoryAndGeo(String geolocation,String category){
    try {
        BannerAllDOExample bannerAllDOExample = new BannerAllDOExample();
        BannerAllDOExample.Criteria criteria = bannerAllDOExample.createCriteria();
        //等於地區+類別
        bannerAllDOExample.or().andGeolocationLike("%"+geolocation+"%").andCategoryEqualTo(category);
        //等於全國+類別
        bannerAllDOExample.or().andGeolocationEqualTo("全國").andCategoryEqualTo(category);
        List<BannerAllDO> bannerAllDOS = bannerAllDOMapper.selectByExample(bannerAllDOExample);
        if(bannerAllDOS.size()!=0)
            return bannerAllDOS;
        else
            return null;
    }catch (Exception e){
        e.printStackTrace();
        return null;
    }
}

2.多表需要手寫sql語句 

<select id="selectSearch" parameterType="com.community.shop.entity.dto.UserSearchDTO" resultType="com.community.shop.entity.db.WXUserDO">
  select * from d_wx_user where uid in (select userid from d_wx_member where shopid=#{shopid} ) and (userName like #{search} or phone like #{search})
</select> 

把引數封裝成了物件進行儲存

在Service實現類裡 

    public List<WXUserDO> selectSearch(UserSearchDTO userSearchDTO){
        try{
            userSearchDTO.setSearch("%"+userSearchDTO.getSearch()+"%");
            List<WXUserDO>wxUserDOList= wxMemberMapper.selectSearch(userSearchDTO);

            if (wxUserDOList!=null){
                return wxUserDOList;
            }
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

Controller:

@ApiOperation(value = "根據姓名或者手機號模糊搜尋使用者", notes = "根據姓名或者手機號模糊搜尋使用者")
@RequestMapping(value = "/selectUserByUserNameOrPhone",method = RequestMethod.POST)
@ResponseBody
@ApiImplicitParams({
        @ApiImplicitParam(paramType = "query", name = "search", value = "姓名或手機號模糊搜尋", required = true, dataType = "String"),
        @ApiImplicitParam(paramType = "query", name = "shopid", value = "店鋪id", required = true, dataType = "String"),
})
public ResultDO selectUserByUserNameOrPhone(@ModelAttribute UserSearchDTO userSearchDTO){
    //TODO 判斷輸入的是手機號還是漢字
    ResultDO resultDO=new ResultDO();
    try{
        //正則驗證輸入的是否是使用者姓名
        Pattern u=Pattern.compile("^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$");
        //正則驗證輸入的是否是手機號
        //Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = u.matcher(userSearchDTO.getSearch());
        boolean b= m.matches();
        //如果正則匹配的是漢字 那麼就模糊搜尋使用者姓名
        if (b){
            List<WXUserDO>wxUserDOS=wxMemberService.selectSearch(userSearchDTO);
            List<WXUserVO>wxUserVOS=new ArrayList<>();
            if (wxUserDOS!=null){
                for (WXUserDO wxUserDO:wxUserDOS){
                    WXUserVO wxUserVO=new WXUserVO();
                    BeanUtils.copyProperties(wxUserDO,wxUserVO);
                    wxUserVOS.add(wxUserVO);
                }
            }else {
                resultDO.setMsg("請正確輸入使用者姓名");
                resultDO.setCode(StatusCode.HTTP_FAILURE);
                return resultDO;
            }
            resultDO.setData(wxUserVOS);
            resultDO.setCode(StatusCode.HTTP_SUCCESS);
            return resultDO;
        }
        //如果不是漢字 那麼久模糊查詢手機號
        if (b==false){
            List<WXUserDO>wxUserDOS=wxMemberService.selectSearch(userSearchDTO);
            List<WXUserVO>wxUserVOS=new ArrayList<>();
            if (wxUserDOS!=null){
                for (WXUserDO wxUserDO:wxUserDOS){
                    WXUserVO wxUserVO=new WXUserVO();
                    BeanUtils.copyProperties(wxUserDO,wxUserVO);
                    wxUserVOS.add(wxUserVO);
                }
            }else {
                resultDO.setMsg("請正確輸入手機號");
                resultDO.setCode(StatusCode.HTTP_FAILURE);
                return resultDO;
            }
            resultDO.setData(wxUserVOS);
            resultDO.setCode(StatusCode.HTTP_SUCCESS);
            return resultDO;
        }
            resultDO.setData("會員不存在");
            resultDO.setCode(StatusCode.HTTP_FAILURE);
            return resultDO;
    }catch (Exception e){
        e.printStackTrace();
        resultDO.setCode(StatusCode.HTTP_FAILURE);
        resultDO.setMsg("請求資料出現異常");
        return resultDO;
    }
}