mongoDB数据库和Spring MVC的整合

时间:2021-08-05 05:24:35

之前一直用到的项目是Spring MVC+maven+mysql的,最近有些数据需要用到mongoDB数据库,现在做一些总结。

第一步:加载jar、maven配置

<!-- mongodb开始 -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<!-- mongodb结束 -->

第二步:配置

1、集群配置

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/schema/util
http://www.springframework.org/schema/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd
http://www.springframework.org/schema/data/repository
http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd
"> <mongo:mongo id="mongo" replica-set="${mongodb.replicaSet}" />
<mongo:db-factory dbname="${mongodb.database}" mongo-ref="mongo"/> <!-- mongo模板操作对象 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean> </beans>

配置文件common-config.properties

mongodb.database=cxstatistics
mongodb.replicaSet = 10.0.11.28:27017,10.0.11.29:27017,10.0.11.30:27017

2、单机配置

applicationContext.xml

<mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}" >
<mongo:options connections-per-host="${mongo.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}"
auto-connect-retry="${mongo.autoConnectRetry}" socket-keep-alive="${mongo.socketKeepAlive}"
socket-timeout="${mongo.socketTimeout}" slave-ok="${mongo.slaveOk}" write-number="1"
write-timeout="0" write-fsync="true"/>
</mongo:mongo>

配置文件common-config.properties

mongo.dbname = test_db #数据库名称
mongo.password = test_pwd #密码
mongo.username = test_user #用户名
mongo.host = 127.0.0.1 #主机
mongo.port= 27017 #端口号
mongo.connectionsPerHost= 8 #一个线程变为可用的最大阻塞数
mongo.threadsAllowedToBlockForConnectionMultiplier= 4 #线程队列数,它以上面connectionsPerHost值相乘的结果就是线程队列最大值
mongo.connectTimeout= 1500 #连接超时时间(毫秒)
mongo.maxWaitTime= 1500 #最大等待时间
mongo.autoConnectRetry= true #自动重连
mongo.socketKeepAlive= true #scoket保持活动
mongo.socketTimeout=1500 #scoket超时时间
mongo.slaveOk=true #读写分离

第三步、MongoDB的增删改查

1、Mongodb对象模型

Interview.java

@Document(collection="pagelog")
public class Interview implements Serializable { /**
*
*/
private static final long serialVersionUID = 1L; @Id
private String id; private String time;
private String desc;
private long _createtime;
private Integer _processed;
private long fid;
private String templateType;
private long puid; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public long get_createtime() {
return _createtime;
}
public void set_createtime(long _createtime) {
this._createtime = _createtime;
}
public Integer get_processed() {
return _processed;
}
public void set_processed(Integer _processed) {
this._processed = _processed;
}
public long getFid() {
return fid;
}
public void setFid(long fid) {
this.fid = fid;
}
public String getTemplateType() {
return templateType;
}
public void setTemplateType(String templateType) {
this.templateType = templateType;
}
public long getPuid() {
return puid;
}
public void setPuid(long puid) {
this.puid = puid;
} }

Pagecount.java

@Document(collection="countlog")
public class Pagecount implements Serializable{ /**
*
*/
private static final long serialVersionUID = 1L; @Id
private String id; private String time;
//private long fid;
List<Integer> fid = new ArrayList<Integer>();
//private Integer pagenum;
List<Integer> pagenum = new ArrayList<Integer>(); public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public List<Integer> getFid() {
return fid;
}
public void setFid(List<Integer> fid) {
this.fid = fid;
}
public List<Integer> getPagenum() {
return pagenum;
}
public void setPagenum(List<Integer> pagenum) {
this.pagenum = pagenum;
} }

2、查询示例代码

接口:

/**
* 得到时间区间内的统计数据
* @param beginDate
* @param endDate
* @return
*/
public long getInterviewNum(String beginDate, String endDate);
/**
* 得到时间区间内根据fid排序的统计数据list
* @param beginDate
* @return
*/
public List<Pagecount> getPageGroup(String beginDate);

接口实现:

@Override
@Path("getInterviewNum")
@GET
@Produces({"application/json; charset=UTF-8"})
public long getInterviewNum(@QueryParam("beginDate")String beginDate, @QueryParam("endDate")String endDate) {
long Num=mongo.count(new Query(Criteria.where("time").gt(beginDate).lte(endDate)), Interview.class);
return Num;
}
@Override
@Path("getPageGroup")
@GET
@Produces({"application/json; charset=UTF-8"})
public List<Pagecount> getPageGroup(@QueryParam("beginDate")String beginDate) {
Criteria criatira= new Criteria();
criatira.andOperator(Criteria.where("time").is(beginDate));
List<Pagecount> pagecount = new ArrayList<Pagecount>();
String year= beginDate.substring(0,4);
String month=beginDate.substring(5);
mongo.getDb().eval("pagelog("+year+","+month+")");
//mongo.getDb().eval("pagelog("+beginDate+")", pagecount);
pagecount= mongo.find(new Query(criatira).with(new Sort(new Order(Direction.DESC, "pagenum"))), Pagecount.class);
return pagecount;
}

控制层:

@Path("")
public class InterviewController { @Autowired
StatisticsService statisticsService; @Post("/interview")
@Get("/interview")
@AdminLoginRequired
public String index(Invocation inv) {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String mDateTime=formatter.format(c.getTime());
inv.addModel("dateTime", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
inv.addModel("mDateTime", mDateTime);
return "/views/statistics/interview.vm";
} @Post("/getInterview")
@Get("/getInterview")
public String getInterview(Invocation inv,@Param("beginDate")String beginDate,@Param("endDate")String endDate){
long Num = statisticsService.getInterviewNum(beginDate, endDate);
inv.addModel("Num", Num);
JSONObject json = new JSONObject();
json.put("Num", Num);
return "@"+json;
} @Post("/interviewGroup")
@Get("/interviewGroup")
@AdminLoginRequired
public String indexGroup(Invocation inv) {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
String mDateTime=formatter.format(c.getTime());
inv.addModel("mDateTime", mDateTime);
return "/views/statistics/interviewGroup.vm";
} @Post("/getPageGroup")
@Get("/getPageGroup")
public String getPageGroup(Invocation inv,@Param("beginDate")String beginDate){
List<Pagecount> Pagecount = new ArrayList<Pagecount>();
Pagecount = statisticsService.getPageGroup(beginDate);
JSONObject json = new JSONObject();
json.put("list", Pagecount);
return "@"+json;
}
}

3、mongoDB存储过程:

function(year,month){
var time=year+'-'+month;
if(month<10){
time=year+'-0'+month;
}
var num=[];
var fid=[];
db.getCollection('pagelog').aggregate([ { $match : {'time':{$regex: "^" + time + ".*$"}} },{$group : { _id : "$fid", num : {$sum : 1}}}]).forEach(function(obj){
num.push(obj.num);
fid.push(obj._id);
})
document1=num;
document2=fid;
db.countlog.update({'time':time},{$set:{'fid':document2,'pagenum':document1,'time':time}},true,true);
return time;
}