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;
    }
}

猜你喜欢

转载自blog.csdn.net/shixiansen6535/article/details/83926207
今日推荐