文章目录

涉及到springmvc自动注入和模糊查询分页

applicationContext.xml 这里网上有许多版本 ,但是copy过来不是版本不符就是各种找不到类

用到的版本

mongodb2.10.jar

mongo-java-driver-3.0.1.jar

对应spring集成的版本必须是4.0以上

mongodb和spring jar:https://yun.baidu.com/share/link?shareid=3559056118&uk=958682606

下面是 applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8" />
<mongo:mongo host="host" port="port"/>
<mongo:db-factory dbname="XXX"
username="XXX"
password="XXX"
mongo-ref="mongo"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
</beans>

下面是比较重要的daoImpl实现层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
TestDaoImpl.java
package com.dao.implement;
import java.sql.Array;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapreduce.MapReduceResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import com.dao.TestDao;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MapReduceCommand;
import com.mongodb.MapReduceOutput;
@Repository("TestDaoImpl")
public class TestDaoImpl implements TestDao {
/* @Autowired
private SqlSessionTemplate sqlSessionTemplate;*/
@Autowired
private MongoTemplate mongoTemplate;
private static String DBNAME_BAIDUURL="baiduurl";
private static String DBNAME_P2PURL="projectlist";
@Override
public List<Map> getCatsByUser(String id, String pageNo, String pageCount) {
/* 1、完全匹配 Pattern pattern = Pattern.compile("^name$", Pattern.CASE_INSENSITIVE);
2、右匹配 Pattern pattern = Pattern.compile("^.*name$", Pattern.CASE_INSENSITIVE);
3、左匹配 Pattern pattern = Pattern.compile("^name.*$", Pattern.CASE_INSENSITIVE);
4、模糊匹配 Pattern pattern = Pattern.compile("^.*name8.*$", Pattern.CASE_INSENSITIVE);*/
Pattern pattern = Pattern.compile("^.*"+id+".*$", Pattern.CASE_INSENSITIVE);
Date date=new Date();
List<Map> list=mongoTemplate.find(new Query(new Criteria("projectTitle").regex(pattern))
.skip((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageCount))
.limit(Integer.parseInt(pageCount)), Map.class,DBNAME_P2PURL);
System.out.println(new Date()+":关键词:"+id+":分页:"+pageNo+","+pageCount+":查询耗时"+(new Date().getTime()-date.getTime())/1000);
return list;
/* Date date1=new Date();
String map = "function(){emit(this.name,this.url);}";
String reduce = "function(key, values) { " +
"var sum = 0; " +
"values.forEach(function(doc) { " +
"sum += 1; "+
"}); " +
"return {baiduurl: sum};} ";
//另一种方法
//com.mongodb.MapReduceOutput.getDuration() 找不到此方法
MapReduceResults<Map> mrrList=mongoTemplate.mapReduce(
new Query(new Criteria("name").regex(pattern)),
"baiduurl",
map,
reduce, Map.class);*/
/* DBCollection baiduurl = mongoTemplate.getCollection("baiduurl");
MapReduceCommand cmd = new MapReduceCommand(baiduurl, map, reduce,
null, MapReduceCommand.OutputType.INLINE, null );
MapReduceOutput out = baiduurl.mapReduce(cmd);
System.out.println("用mapreduce的查询耗时"+(new Date().getTime()-date1.getTime())/1000);*/
}
@Override
public Long getCatsByUserCount(String name) {
Pattern pattern = Pattern.compile("^.*"+name+".*$", Pattern.CASE_INSENSITIVE);
Long lo=mongoTemplate.count(new Query(new Criteria("projectTitle").regex(pattern)),DBNAME_P2PURL);
if (lo>210) {
lo=210l;
}
return lo;
}
}

文章目录