您好,欢迎访问一九零五行业门户网

MongoDB:mongodb在项目开发时的安全验证、分页查询操作

mongodb:mongodb在项目开发时的安全验证、分页查询操作。 对于数据库而言,在项目应用中都需要安全验证,不然,就会报错,呵呵~~ 现在贴出来我在项目中是怎么做的。 数据源bean: package com.ishowchina.user.dao;import com.mongodb.basicdbobject;impo
mongodb:mongodb在项目开发时的安全验证、分页查询操作。
对于数据库而言,在项目应用中都需要安全验证,不然,就会报错,呵呵~~
现在贴出来我在项目中是怎么做的。
数据源bean:
package com.ishowchina.user.dao;import com.mongodb.basicdbobject;import com.mongodb.db;import com.mongodb.dbcollection;import com.mongodb.dbcursor;import com.mongodb.dbobject;import com.mongodb.mongoclient;public class datasource { private string ip;//数据库连接信息要从配置文件中获取 参考appconfig.properties文件 private integer port; protected string dbname; protected boolean auth; protected string username; protected string password; //打开连接 此处在获取数据库信息(ip、账户、密码等)后,打开数据库。 public mongoclient openconnection() throws exception{ mongoclient mongoclient = new mongoclient( ip,port); return mongoclient; } //获取数据集 此处为数据库安全验证 public dbcollection getcollection(mongoclient client,string tablename){ if(client==null){ return null; }else { db db = client.getdb(getdbname());//获取数据库 boolean r=true;//验证用户及密码 if(auth!=null && auth.equals(true)){ r = db.authenticate(username, password.tochararray());//密码验证 } if(r){ dbcollection coll = db.getcollection(tablename);//获取数据集 return coll; }else { return null; } } } //释放连接 public void releaseconnection(mongoclient mongoclient){ if(mongoclient!=null){ mongoclient.close(); } } //删除操作,注意要传入参数 public void deleteobject(dbobject o,dbcollection col){ col.remove(o); } //分页查询 分页查询mongodb已经为我们集成了,只需调用api就行 public dbcursor querypage(dbobject query,dbobject sort,int start,int limit,dbcollection col){ //.sort('account',1).limit(10),int count = cursor.count() basicdbobject exp=new basicdbobject(_id,0); return col.find(query,exp).sort(sort).skip(start).limit(limit); } public string getip() { return ip; } public void setip(string ip) { this.ip = ip; } public integer getport() { return port; } public void setport(integer port) { this.port = port; } public string getdbname() { return dbname; } public boolean getauth() { return auth; } public void setauth(boolean auth) { this.auth = auth; } public void setdbname(string dbname) { this.dbname = dbname; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; }}
appconfig.properties配置文件的内容,配置数据库源信息
mongo.ip=160.0.0.243mongo.port=27017mongo.auth=truemongo.user=ucentermongo.pwd=user2showmongo.dbname=ucenter
此外,肯定还要把配置文件appconfig.properties导入到spring bean factory,也就是需要让datasource.java 知道去appconfig.properties 中找数据库配置信息spring-servlet.xml
classpath:appconfig.properties
这样做还不够,虽然知道了去那找数据库配置信息,但是,怎么获取数据库配置信息呢,还是在spring-servlet.xml里。
以上就是完全的相关配置,接下来我们就可以调用getcollection、openconnection等方法了
举个例子:public dbobject getuserinfo(dbobject o) throws exception{ mongoclient mongoclient = datasource.openconnection(); dbcollection coll = datasource.getcollection(mongoclient,tablename);//获取数据集userinfo //mongoclient.setwriteconcern(writeconcern.journaled);//setting write concern basicdbobject exp=new basicdbobject(_id,0);//排除id字段 dbobject mydoc = coll.findone(o,exp); datasource.releaseconnection(mongoclient) ;//释放连接 return mydoc; }
以上,大致流程就这样。。
其它类似信息

推荐信息