1.首先建立一个po表
po表中 添加不是本表的数据
例如
public class UserInfoPo extends UserInfo { private String deptId;//部门id private String uid;//用户id private String dept;//部门 private String position;//部门的职位 public String getDeptId() { return deptId; }
}
在example文件中添加 模糊查询用到的字段
public class UserInfoExample {
private String position;//部门的职位
}
在xml文件中
映射字段
并且写出sql
select u.*,d.* from t_userinfo u,t_user_dept d where u.id = d.uid and d.position like #{position,jdbcType=VARCHAR}
order by ${orderByClause}
在mapper文件中
写出方法:
/** * 根据部门的模糊查询获取所有用户 * @description: * @author:xuzn * @date:2017-2-13 上午9:41:52 * @modify: * @param example * @return * @version: * */ List selectUserByExample(UserInfoExample example);
在serviceImpl中 方法如下:
/** * 模糊查询获取用户 */ @Override public List selectUserByExample(UserInfoPo po) { UserInfoExample example = new UserInfoExample(); Criteria criteria = example.createCriteria();
//判断 字段是否为空 由po获取传过来的数据 if(StringUtils.hasText(po.getPosition())){
//example文件中 将 position赋值 example.setPosition("%"+po.getPosition()+"%"); } List list = userInfoMapper.selectUserByExample(example); if (list.size()>0) { return list; }else { return null; } }
controller层 方法 如下:
@RequestMapping("selectUserByExample") @ResponseBody public LBResult selectUserByExample(UserInfoPo po, HttpServletRequest request, HttpServletResponse response) { List list = userInfoService.selectUserByExample(po); try { if (list.size()>0) { return LBResult.build(400, "成功", list); }else { return LBResult.build(404, "失败"); } } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); return LBResult.build(500, ExceptionUtil.getStackTrace(e)); } }