首页 > 代码库 > MongoDBDao 工具类
MongoDBDao 工具类
工具类 public class MongoDBDao { /** * 获取MONGODB客户端实例 * * @return * @throws Exception */ public static MongoClient getMongoClient() throws Exception { try { // 解密用密钥 String sKey = SysPropertyJdbc.getProperty("jdbc.deskey") .substring(4, 28); String sIp = DESUtil.decryptMode(sKey, MongoDBProperty.getProperty("mip")); int iPort = Integer.valueOf(DESUtil.decryptMode(sKey, MongoDBProperty.getProperty("mport"))); String sUser = DESUtil.decryptMode(sKey, MongoDBProperty.getProperty("muser")); String sPasword = DESUtil.decryptMode(sKey, MongoDBProperty.getProperty("mpassword")); String sDbNm = DESUtil.decryptMode(sKey, MongoDBProperty.getProperty("mdb")); // ===================================================// List<ServerAddress> serverList = new ArrayList<ServerAddress>(); serverList.add(new ServerAddress(sIp, iPort)); // ===================================================// List<MongoCredential> mcList = new ArrayList<MongoCredential>(); mcList.add(MongoCredential.createCredential(sUser, sDbNm, sPasword.toCharArray())); // ===================================================// MongoClientOptions.Builder builder = MongoClientOptions.builder(); // 与目标数据库能够建立的最大connection数量为50 builder.connectionsPerHost(50); // 如果当前所的connection都在使用中,则每个connection上可以50个线程排队等待 builder.threadsAllowedToBlockForConnectionMultiplier(50); // 一个线程访问数据库的时候,在成功获取到一个可用数据库连接之前的最长等待时间为2分钟 // 这里比较危险,如果超过maxWaitTime都没获取到这个连接的话,该线程就会抛出Exception // 故这里设置的maxWaitTime应该足够大,以免由于排队线程过多造成的数据库访问失败 builder.maxWaitTime(1000 * 60 * 2); // 与数据库建立连接的timeout设置为1分钟 builder.connectTimeout(1000 * 60 * 1); // ===================================================// MongoClientOptions mco = builder.build(); return new MongoClient(serverList, mcList, mco); } catch (Exception e) { throw e; } } } 操作类 logger.debug("START..."); int page = 0;// 滚动加载页码 int page10 = 0; if (this.getPara("pagetimer") != null) { page = Integer.parseInt(this.getPara("pagetimer"));// 滚动加载页码 page10 = page * 10; } String umac = this.getPara("umac"); Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, -1); // System.out.println(c.getTime()); 上个月时间 MongoDBDao mongoDBDao = new MongoDBDao(); MongoClient mongoClient = mongoDBDao.getMongoClient(); MongoDatabase database = mongoClient .getDatabase(DESUtil.decryptMode(SysPropertyJdbc.getProperty("jdbc.deskey").substring(4, 28), MongoDBProperty.getProperty("mdb"))); MongoCollection<Document> collection = database.getCollection("user_timeline"); // 查询条件 MongoCursor<Document> mongoCursor = collection.find(Filters.and(Filters.eq("umac", umac), Filters.gt("datetime", c.getTime()))).skip(page10) .limit(10).iterator(); String timerShaft = "";// 返回显示项 while (mongoCursor.hasNext()) { Iterator<Entry<String, Object>> iter = mongoCursor.next().entrySet().iterator(); Date datetime = null; String host = ""; String resultdate = ""; while (iter.hasNext()) { Entry eTmp = iter.next(); String sKeyTmp = eTmp.getKey().toString(); switch (sKeyTmp) { case "datetime": datetime = (Date) eTmp.getValue(); break; case "host": host = eTmp.getValue().toString(); break; default: break; } if (datetime != null) { resultdate = dateDeal(datetime); } } timerShaft = timerShaft + "<li><div class=\"animationTime\">" + resultdate + "</div><div class=\"animationTimeN\">" + host + "</div></li>"; } this.setAttr("pagetimer", page); this.setAttr("timerShaft", timerShaft); 工具类 public class SysPropertyJdbc { /** * 初期化 */ private SysPropertyJdbc() throws Exception { this.props = new Properties(); props.load(this.getClass().getClassLoader() .getResourceAsStream(PROPS_NAME)); } private static final String PROPS_NAME = "jdbc.properties"; private static SysPropertyJdbc sysProps; private Properties props; public static String getProperty(String key) throws Exception { return SysPropertyJdbc.getInstance().getProperties().getProperty(key); } private Properties getProperties() { return this.props; } private static synchronized SysPropertyJdbc getInstance() throws Exception { if (sysProps == null) { sysProps = new SysPropertyJdbc(); } return sysProps; } } public class MongoDBProperty { private MongoDBProperty() throws Exception { this.props = new Properties(); props.load(this.getClass().getClassLoader() .getResourceAsStream(PROPS_NAME)); } private static final String PROPS_NAME = "mongodb.properties"; private static MongoDBProperty sysProps; private Properties props; public static String getProperty(String key) throws Exception { return MongoDBProperty.getInstance().getProperties().getProperty(key); } private Properties getProperties() { return this.props; } private static synchronized MongoDBProperty getInstance() throws Exception { if (sysProps == null) { sysProps = new MongoDBProperty(); } return sysProps; } }
#mongodb.properties
mip=D9iw8GElYiWgjBrhRZgTRQ==
mport=3YtpA2QmgVM=
muser=RiT52btdOdCcVP34zzX6Xg==
mpassword=HjCIDeJQfMmo9w1ghPC+UPiE88wPzOLh
mdb=SiO7ZySzZh3l2G2+OCVjMw==
MongoDBDao 工具类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。