/** * 公共方法:设置Object类型缓存 * @author shijing * @param param * @param sysGuid */ public void setObjData(Map<String,Object> param, String sysGuid, String enumBpd){ DBObject dbObject = new BasicDBObject(); dbObject.putAll(param); String collectionName = EnumBpd.getBpdType(enumBpd) + sysGuid; mongoDao.insertToCol(dbObject,collectionName); } /** * 公共方法:设置List缓存 * @author shijing * @param paramList * @param sysGuid */ public void setListData(List<Map<String,Object>> paramList, String sysGuid, String enumBpd){ List<DBObject> list = new ArrayList<>(); if(CollectionUtils.isNotNull(paramList)){ for (Map<String,Object> param : paramList){ DBObject dbObject = new BasicDBObject(); dbObject.putAll(param); list.add(dbObject); } } String collectionName = EnumBpd.getBpdType(enumBpd) + sysGuid; mongoDao.insertToCol(list,collectionName);
public void insertToCol(DBObject document, String collectionName) { dropCol(collectionName); DBCollection dbCollection = mongoTemplate.createCollection(collectionName); dbCollection.insert(document); } public void insertToCol(List<DBObject> documents, String collectionName) { dropCol(collectionName); DBCollection dbCollection = mongoTemplate.createCollection(collectionName); dbCollection.insert(documents); }
/** * 通过关键字模糊查询问题和答案库 * @param param * @return */ @Override public List<Map<String, Object>> searchQuestionAndAnswerByKeyword(Map<String, Object> param) { List<Map<String,Object>> searchList = new ArrayList<>(); Map<String,Object> userInfo = SessionUtils.getUserInfo(); String sysGuid = userInfo.get("sys_guid").toString(); String collectionName = EnumBpd.getBpdType(EnumBpd.HELP_PAGE_LIST.getType())+sysGuid; //注释里面这种方式虽然能模糊查询,但是容易漏掉数据,切记切记!!! //Pattern pattern = Pattern.compile("^.*" + param.get("keyword") +".*$", Pattern.CASE_INSENSITIVE); BasicDBObject query= new BasicDBObject(); //模糊查询的字段设置 query.put("page_html", Pattern.compile((String) param.get("keyword"))); DBCursor dbCursor = mongoDao.findAll(query,collectionName); List<DBObject> list = dbCursor.toArray(); for (DBObject dbObject: list){ searchList.add(dbObject.toMap()); } //模糊查到的数据进行组装 return getQuestionAndAnswerList(searchList); } /** * 公共方法:批量—— in方法查询List数据 * @author shijing * @param ids id集合 * @param paramMap 其他参数 * @param columnName in字段列名 * @param collectionName 表名 * @return */ @Override public List<Map<String, Object>> batchSearchPageListByIds(List<String> ids, Map<String, Object> paramMap, String columnName, String collectionName) { List<Map<String,Object>> searchList = new ArrayList<>(); BasicDBObject query= new BasicDBObject(); //批量查询,in if (CollectionUtils.isNotEmpty(ids)){ BasicDBList values = new BasicDBList(); values.addAll(ids); query.put(columnName, new BasicDBObject("$in",values)); } //拼接参数 if(MapUtils.isNotEmpty(paramMap)){ for (String mapKey: paramMap.keySet()){ query.put(mapKey, paramMap.get(mapKey)); } } DBCursor dbCursor = mongoDao.findAll(query,collectionName); List<DBObject> list = dbCursor.toArray(); if (dbCursor!=null && dbCursor.size()>0){ for (DBObject dbObject: list){ searchList.add(dbObject.toMap()); } } return searchList; } /** * 公共方法:通过参数获取List数据 * @author shijing * @param paramMap 参数 * @param collectionName 表名 * @return */ @Override public List<Map<String, Object>> getListByParam(Map<String, Object> paramMap,String collectionName){ List<Map<String,Object>> searchList = new ArrayList<>(); BasicDBObject query= new BasicDBObject(); //拼接参数 if(MapUtils.isNotEmpty(paramMap)){ for (String mapKey: paramMap.keySet()){ query.put(mapKey, paramMap.get(mapKey)); } } DBCursor dbCursor = mongoDao.findAll(query,collectionName); List<DBObject> list = dbCursor.toArray(); if (dbCursor!=null && dbCursor.size()>0){ for (DBObject dbObject: list){ searchList.add(dbObject.toMap()); } } return searchList; } /** * 公共方法:通过参数获取Object数据 * @author shijing * @param paramMap * @param collectionName 表名 * @return */ @Override public Map<String, Object> getObjectByParam(Map<String, Object> paramMap, String collectionName) { Map<String,Object> webSiteInfo = new HashMap<>(); BasicDBObject query= new BasicDBObject(); //拼接参数 if(MapUtils.isNotEmpty(paramMap)){ for (String mapKey: paramMap.keySet()){ query.put(mapKey, paramMap.get(mapKey)); } } DBObject dbObject = mongoDao.findOne(query,collectionName); if(dbObject!=null){ return dbObject.toMap(); } return webSiteInfo; }