MyBatis3 动态SQL 判断状态和集合解析

符号 阅读:205 2021-03-31 16:56:56 评论:0

今天在做通知公共模块与用户关联通知公共模块的时候,遇到这样一个问题:判断指定用户是否阅读或未阅读通知功能,如果用户选择已经阅读公共通知,需要返回相关已经阅读相关通知数据,如果用户选择未阅读功能通知,需要返回未阅读公共通知数据。

注意:用户关联通知公共:仅仅记录用户已经阅读的公共通知记录。

核心mapp接口定义和mapp.xml文件:

List<T> selectBySids(Map<String,Object> parame);
 <select id="selectBySids" parameterType="java.util.Map" resultMap="BaseResultMap"> 
  	select  
    	<include refid="Base_Column_List" /> 
    	from *** 
    	where 1 = 1  
    	<!-- 判断用户已读公共信息  --> 
    	<if test="state != null and state !=''  and state == '1'.toString()"> 
    		<!-- 判断list 集合不能为空  --> 
    		<if test="list != null and list.size() > 0"> 
    			and sid in 
    			<foreach collection="list" item="sid" index="index" open="(" close=")" separator=","> 
      				#{sid} 
    			</foreach> 
    		</if> 
    	</if> 
    	<!-- 判断用户未读未读信息  --> 
    	<if test ="state != null and state !=''  and state == '2'.toString()"> 
    		<!-- 判断list 集合不能为空  --> 
    		<if test="list != null and list.size() > 0"> 
    			and sid not in 
    			<foreach collection="list" item="sid" index="index" open="(" close=")" separator=","> 
      				#{sid} 
    			</foreach> 
    		</if> 
    	</if> 
  </select>

service 方法定义和实现:T

 PageData<T> selectAllPage(List<String> sids, PageParam rb, String state);

    @Override
    public PageData<T> selectAllPage(List<String> sids, PageParam rb, String state) {
        // TODO Auto-generated method stubT
        PageData<T> pageData = new PageData<T>();
        T
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("state", state);
        map.put("list", sids);
        PageHelper.startPage(rb.getPageNo(), rb.getLimit());
        List<T> rs = mapper.selectBySids(map);
        
        PageInfo<T> pageInfo = new PageInfo<T>(rs);
        pageData.setData(pageInfo.getList());
        pageData.setPageNum(pageInfo.getPageNum());
        pageData.setPageSize(pageInfo.getPageSize());T
        pageData.setTotalCount(pageInfo.getTotal());
        return pageData;
    }

controller 方法调用:

@RequestMapping(value="/find", method={RequestMethod.POST}, produces = "application/json;charset=UTF-8") 
	@ResponseBody 
	@ApiOperation(httpMethod = "POST", value = "用户通知公共") 
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "sid", value = "主键", required = false, dataType = "String", paramType = "query"), 
		@ApiImplicitParam(name = "userSid", value = "用户sid", required = false, dataType = "String", paramType = "query"), 
		@ApiImplicitParam(name = "announceSid", value = "公共sid", required = false,  dataType = "String", paramType = "query"), 
		@ApiImplicitParam(name = "state", value = "状态类型:1:已读  2:未读", required = false,  dataType = "String", paramType = "query"), 
		@ApiImplicitParam(name = "createDt", value = "创建时间", required = false,  dataType = "Date", paramType = "query"), 
		@ApiImplicitParam(name = "readDt", value = "阅读时间", required = false,  dataType = "Date", paramType = "query") 
	}) 
	public Result find(@RequestBody @ApiParam(name = "用户公告通知", value = "json格式对象", required = true) JSONObject entity) { 
		// 特定用户处理 
		String userSid = StringUtils.isEmpty(entity.getString("userSid")) == true ? "1" : entity.getString("userSid"); 
		entity.put("userSid", userSid); 
		 
		String state = entity.getString("state"); 
		entity.remove("state"); 
		// 默认数据用户已阅读 
		Map<String,Object> parame = JSONObject.toJavaObject(entity, Map.class); 
		List<**> list = service.selectAll(parame); 
		 
		List<String> sids = list.stream().map(SysNotifyAnnounceUser :: getAnnounceSid).collect(Collectors.toList()); 
		PageParam rb = super.initPageBounds(parame); 
		PageData<**> pageList = null; 
		if(!CollectionUtils.isEmpty(sids)){ 
			pageList = service.selectAllPage(sids, rb, state); 
			return Result.ok().setDatas("list",pageList.getData()).setDatas("limit", pageList.getPageSize()).setDatas("page", pageList.getPageNum()).setDatas("total_count", pageList.getTotalCount()); 
		} else { 
			if("unread".equalsIgnoreCase(state)){ 
				sids = new ArrayList<String>(); 
				pageList = service.selectAllPage(sids, rb, state); 
				return Result.ok().setDatas("list",pageList.getData()).setDatas("limit", pageList.getPageSize()).setDatas("page", pageList.getPageNum()).setDatas("total_count", pageList.getTotalCount());	 
			} 
			return Result.ok().setDatas("list", new ArrayList()).setDatas("limit", entity.getInteger("limit")).setDatas("page", entity.getInteger("page")).setDatas("total_count", 0); 
		} 
	}

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们