最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装、客户端操作、安全认证、副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很大。特此记录,以备查看。
文章目录:
本文记录如何使用XML配置方式整合Spring data和MongoDB
1、环境准备
JDK 1.8
Spring 4.2.5.RELEASE
Junit 4.10
Spring-data-mongodb 1.9.0.RELEASE
Logback 1.1.7
Maven 3.2.1
依赖
1
2 UTF-8
3 1.8
4 1.8
5 4.2.5.RELEASE
6
7
8
9
10
11 junit
12 junit
13 4.10
14 test
15
16
17
18 org.springframework
19 spring-context-support
20 ${spring.version}
21
22
23 org.springframework
24 spring-aop
25 ${spring.version}
26
27
28 org.springframework
29 spring-test
30 ${spring.version}
31 test
32
33
34
35 org.springframework.data
36 spring-data-mongodb
37 1.9.0.RELEASE
38
39
40
41
42 ch.qos.logback
43 logback-core
44 1.1.7
45
46
47 ch.qos.logback
48 logback-classic
49 1.1.7
50
51
52 org.aspectj
53 aspectjweaver
54 1.6.1
55
56
57
58
59
60 security-demo
61
62
63 org.apache.maven.plugins
64 maven-compiler-plugin
65 3.3
66
67 ${maven.compiler.source}
68 ${maven.compiler.target}
69 ${project.build.sourceEncoding}
70
71
72
73
2、mongodb配置文件
这个是spring的子配置文件,通过从.properties文件读取连接属性创建mongoClient对象,再创建DBFactory对象,通过DBFactory创建MongoTemplate,我们的数据层实现类就是使用MongoTemplate操作MongoDB的。
1
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mongo="http://www.springframework.org/schema/data/mongo"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd8 http://www.springframework.org/schema/context9 http://www.springframework.org/schema/context/spring-context-4.2.xsd10 http://www.springframework.org/schema/data/mongo11 http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd">
12
13
14
15 ignore-unresolvable="true" />
16
17
18
19
20 threads-allowed-to-block-for-connection-multiplier=21 "${mongo.threadsAllowedToBlockForConnectionMultiplier}"
22 connect-timeout="${mongo.connectTimeout}"max-wait-time="${mongo.maxWaitTime}"
23 socket-keep-alive="${mongo.socketKeepAlive}"
24 socket-timeout="${mongo.socketTimeout}" />
25
26
27
28
29 mongo-ref="mongo" />
30
31
32
33
34
35
36
View Code
mongodb.properties
1 mongo.hostport=10.10.12.195:27017
2 mongo.dbname=test3 mongo.connectionsPerHost=8
4 mongo.threadsAllowedToBlockForConnectionMultiplier=4
5 mongo.connectTimeout=1000
6 mongo.maxWaitTime=1500
7 mongo.socketKeepAlive=true
8 mongo.socketTimeout=1500
3、spring主配置文件
application.xml是spring的主配置文件,配置组件扫描,另外还要引入mongo的配置
1
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xmlns:aop="http://www.springframework.org/schema/aop"
7 xsi:schemaLocation="http://www.springframework.org/schema/beans8 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd9 http://www.springframework.org/schema/context10 http://www.springframework.org/schema/context/spring-context-4.2.xsd">
11
12
13
14
15
16
17
18
19
4、实体类和数据层
Employee
1 @Document(collection = "employee")2 public class Employee implementsSerializable {3
4 @Id5 @Field(value = "_id")6 privateString id;7
8 @Field(value = "name")9 privateString name;10
11 @Field12 privateInteger age;13
14 @Field15 private Date createTime = newDate();16
17 publicEmployee() {18 super();19 }20 publicEmployee(String name, Integer age) {21 super();22 this.name =name;23 this.age =age;24 }25
26 //getter & setter
27 }
数据层实现
1 @Repository2 public class EmployeeDaoImpl implementsEmployeeDao {3
4 @Autowired5 privateMongoTemplate mongoTemplate;6
7 @Override8 public ListfindAll() {9 return mongoTemplate.findAll(Employee.class);10 }11
12 @Override13 public voidinsertOneEmployee(Employee employee) {14 mongoTemplate.insert(employee);15 }16
17 @Override18 public voiddeleteOneEmployeeByName(String name) {19 Criteria c = newCriteria();20 c.and("name").is(name);21 Query query = newQuery(c);22 mongoTemplate.remove(query, Employee.class);23 }24
25 @Override26 public voiddeleteOneEmployee(String id) {27 Criteria c = newCriteria();28 c.and("_id").is(id);29 Query query = newQuery(c);30 mongoTemplate.remove(query, Employee.class);31 }32
33 @Override34 publicEmployee findByName(String name) {35 Criteria c = newCriteria();36 c.and("name").is(name);37 Query query = newQuery(c);38 return mongoTemplate.findOne(query, Employee.class);39 }40
41 @Override42 publicEmployee find(String id) {43 return mongoTemplate.findById(id, Employee.class);44 }45 }
View Code
代码中把MongoTemplate注入到了实现类,然后使用它的API进行数据库操作。
5、Junit测试类
1 @RunWith(SpringJUnit4ClassRunner.class)2 @ContextConfiguration(locations = { "classpath:application.xml"})3 public classEmployeeDaoImplTest {4
5 @Autowired6 privateEmployeeDao employeeDao;7
8 @Autowired9 privateApplicationContext context;10
11 @Test12 public voidtestFindAll() {13 List list =employeeDao.findAll();14 for(Employee employee : list) {15 System.out.println(employee);16 }17 }18
19 @Test20 public voidtestInsertOneEmployee() {21 Employee e = new Employee("admin", 28);22 employeeDao.insertOneEmployee(e);23 }24
25 @Test26 public voidtestDeleteOneEmployeeByName() {27 employeeDao.deleteOneEmployeeByName("admin");28 }29
30 @Test31 public voidtestDeleteOneEmployee() {32 Employee e = employeeDao.findByName("admin");33 employeeDao.deleteOneEmployee(e.getId());34 }35
36 @Test37 public voidtestFindByName() {38 Employee e = employeeDao.findByName("admin");39 System.out.println(e);40 }41
42 @Test43 public voidtestFind() {44 Employee e = employeeDao.findByName("admin");45 Employee e2 =employeeDao.find(e.getId());46 System.out.println(e2);47 }48 }
6、参考资料
Spring Data MongoDB - Reference Documentation