​MongoDB学习笔记之 第1章 MongoDB的安装​


​MongoDB学习笔记之 第2章 MongoDB的增删改查​

​MongoDB学习笔记之 第3章 MongoDB的Java驱动​



 第4章 MongoDB整合Spring

(黎明你好原创作品,转载请注明)

4.1 创建maven项目


MongoDB学习笔记之 第4章 MongoDB整合Spring_spring

4.1.1 repositories

创建maven项目,其中repositories使用spring的maven库:

 

1. <repositories>  
2. <repository>
3. <id>central</id>
4. <name>Maven Central</name>
5. //repo1.maven.org/maven2/</url>
6. </repository>
7. <repository>
8. <id>spring-release</id>
9. <name>Spring Maven Release Repository</name>
10. //repo.springsource.org/libs-release</url>
11. </repository>
12. <repository>
13. <id>atlassian-m2-repository</id>
14. //m2proxy.atlassian.com/repository/public</url>
15. </repository>
16. </repositories>


 

4.1.2 Dependencies

使用到的jar包:

1. <dependencies>  
2. <dependency>
3. <groupId>javax.servlet</groupId>
4. <artifactId>servlet-api</artifactId>
5. 2.5</version>
6. <type>jar</type>
7. <scope>provided</scope>
8. </dependency>
9. <dependency>
10. <groupId>org.slf4j</groupId>
11. <artifactId>slf4j-api</artifactId>
12. 1.6.1</version>
13. <type>jar</type>
14. <scope>compile</scope>
15. </dependency>
16. <dependency>
17. <groupId>org.slf4j</groupId>
18. <artifactId>slf4j-log4j12</artifactId>
19. 1.7.5</version>
20. <type>jar</type>
21. <scope>runtime</scope>
22. </dependency>
23. <dependency>
24. <groupId>org.mongodb</groupId>
25. <artifactId>mongo-java-driver</artifactId>
26. 2.10.1</version>
27. <type>jar</type>
28. <scope>compile</scope>
29. </dependency>
30. <dependency>
31. <groupId>org.springframework.data</groupId>
32. <artifactId>spring-data-mongodb</artifactId>
33. 1.2.1.RELEASE</version>
34. <type>jar</type>
35. <scope>compile</scope>
36. </dependency>
37. <dependency>
38. <groupId>org.springframework.data</groupId>
39. <artifactId>spring-data-mongodb-cross-store</artifactId>
40. 1.2.1.RELEASE</version>
41. <type>jar</type>
42. <scope>compile</scope>
43. </dependency>
44. <dependency>
45. <groupId>org.springframework.data</groupId>
46. <artifactId>spring-data-mongodb-log4j</artifactId>
47. 1.2.1.RELEASE</version>
48. <type>jar</type>
49. <scope>compile</scope>
50. </dependency>
51. </dependencies>


 

 

4.2 添加spring配置文件

spring的配置文件applicationContext.xml

 

1. <?xml versinotallow="1.0" encoding="UTF-8"?>  
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. "http://www.w3.org/2001/XMLSchema-instance"
4. "http://www.springframework.org/schema/context"
5. "http://www.springframework.org/schema/data/mongo"
6. //www.springframework.org/schema/beans
7. //www.springframework.org/schema/beans/spring-beans-3.0.xsd
8. //www.springframework.org/schema/data/mongo
9. //www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
10. //www.springframework.org/schema/context
11. //www.springframework.org/schema/context/spring-context-3.0.xsd">
12.
13. package="liming.mongodb.example"
14.
15. "127.0.0.1" port="27017"
16.
17. <!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
18. "student" mongo-ref="mongo"
19.
20. <!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 -->
21. "mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
22. "mongoDbFactory" ref="mongoDbFactory"
23. </bean>
24.
25. package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 -->
26. package="climing.mongodb.example.data.model"
27.
28. <!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 -->
29. package="liming.mongodb.example.data.impl"
30.
31. <context:annotation-config />
32.
33. </beans>

 

 

 

4.3 增删改查

Userl实现的增删改查:

4.3.1UserEntity

1. package
2.
3. import
4.
5. import
6. import
7.
8. @Document(collection = "user")
9. public class
10.
11. @Id
12. private
13. private
14. private int
15. private int
16. private
17. private
18. private
19. private
20.
21. public
22. return
23. }
24.
25. public void
26. this.id = id;
27. }
28.
29. public
30. return
31. }
32.
33. public void
34. this.name = name;
35. }
36.
37. public int
38. return
39. }
40.
41. public void setAge(int
42. this.age = age;
43. }
44.
45. public int
46. return
47. }
48.
49. public void setWorks(int
50. this.works = works;
51. }
52.
53. public
54. return
55. }
56.
57. public void
58. this.birth = birth;
59. }
60.
61. public
62. return
63. }
64.
65. public void
66. this.password = password;
67. }
68.
69. public
70. return
71. }
72.
73. public void
74. this.regionName = regionName;
75. }
76.
77. public
78. return
79. }
80.
81. public void
82. this.special = special;
83. }
84.
85. }

 

4.3.2 NameEntity

 

1. package
2.
3. public class
4.
5. private
6.
7. private
8.
9. public
10. return
11. }
12.
13. public void
14. this.username = username;
15. }
16.
17. public
18. return
19. }
20.
21. public void
22. this.nickname = nickname;
23. }
24.
25. }

 

4.3.3 UserDao

 

1. package
2.
3. import
4.
5. import
6.
7. import
8.
9. @Transactional
10. public interface
11.
12. public abstract void
13.
14. public abstract void
15.
16. public abstract List<UserEntity> findList(int skip, int
17.
18. public abstract List<UserEntity> findListByAge(int
19.
20. public abstract
21.
22. public abstract
23.
24. public abstract void
25.
26. public abstract void
27.
28. }

 

 

4.3.4 UserDaoImpl

 

1. package
2.
3. import
4. import
5.
6. import
7. import
8.
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16. import
17. import
18. import
19. import
20.
21. import
22.
23. @Repository
24. public class UserDaoImpl implements
25.
26. public static final Logger logger = LoggerFactory.getLogger(UserDaoImpl.class);
27.
28. @Autowired
29. private
30.
31. @Override
32. public void
33. this.mongoTemplate.getCollectionNames();
34. for
35. "Collectinotallow="
36. }
37. this.mongoTemplate.getDb();
38. "db="
39. }
40.
41. @Override
42. public void
43. if (!this.mongoTemplate.collectionExists(UserEntity.class)) {
44. this.mongoTemplate.createCollection(UserEntity.class);
45. }
46. }
47.
48. @Override
49. public List<UserEntity> findList(int skip, int
50. new
51. new Sort(new Order(Direction.ASC, "_id")));
52. query.skip(skip).limit(limit);
53. return this.mongoTemplate.find(query, UserEntity.class);
54. }
55.
56. @Override
57. public List<UserEntity> findListByAge(int
58. new
59. new Criteria("age").is(age));
60. return this.mongoTemplate.find(query, UserEntity.class);
61. }
62.
63. @Override
64. public
65. new
66. new Criteria("_id").is(id));
67. return this.mongoTemplate.findOne(query, UserEntity.class);
68. }
69.
70. @Override
71. public
72. new
73. new Criteria("name.username").is(username));
74. return this.mongoTemplate.findOne(query, UserEntity.class);
75. }
76.
77. @Override
78. public void
79. this.mongoTemplate.insert(entity);
80.
81. }
82.
83. @Override
84. public void
85. new
86. new Criteria("_id").is(entity.getId()));
87. new
88. "age", entity.getAge());
89. "password", entity.getPassword());
90. "regionName", entity.getRegionName());
91. "special", entity.getSpecial());
92. "works", entity.getWorks());
93. "name", entity.getName());
94. this.mongoTemplate.updateFirst(query, update, UserEntity.class);
95.
96. }
97.
98. }

 

4.3.5 测试代码

 

1. package
2.
3. import
4. import
5. import
6.
7. import
8. import
9. import
10.
11. import
12. import
13.
14. public class
15.
16. public static void
17.
18. "Bootstrapping HelloMongo");
19.
20. null;
21. new ClassPathXmlApplicationContext("applicationContext.xml");
22.
23. class);
24. userDao._test();
25. new
26. "5");
27. 1);
28. new
29. "asdfasdf");
30. "北京");
31. 1);
32. userDao.insert(entity1);
33. userDao.update(entity1);
34. userDao.createCollection();
35.
36. 0, 10);
37. for
38. "all - id=" + e.getId() + ", age=" + e.getAge() + ", password=" + e.getPassword() + ", reginotallow=" + e.getRegionName() + ", special="
39. ", name=" + e.getName().getUsername() + "-" + e.getName().getNickname() + ", birth="
40. }
41.
42. 1);
43. for
44. "age=1 - id=" + e.getId() + ", age=" + e.getAge() + ", password=" + e.getPassword() + ", reginotallow=" + e.getRegionName() + ", special="
45. ", name=" + e.getName().getUsername() + "-" + e.getName().getNickname() + ", birth="
46. }
47.
48. "1");
49. "id=1 - id=" + e.getId() + ", age=" + e.getAge() + ", password=" + e.getPassword() + ", reginotallow=" + e.getRegionName() + ", special="
50. ", name=" + e.getName().getUsername() + "-" + e.getName().getNickname() + ", birth="
51.
52. "limingnihao");
53. "username=limingnihao - id=" + e.getId() + ", age=" + e.getAge() + ", password=" + e.getPassword() + ", reginotallow=" + e.getRegionName() + ", special="
54. ", name=" + e.getName().getUsername() + "-" + e.getName().getNickname() + ", birth="
55.
56.
57. "DONE!");
58. }
59.
60. }