本地跑起来!IntellliJ IDEA+maven+spring+springMVC+tomcat+mongodb搭建本地开发环境

在前面搭建的环境上加上mongodb配置文件,pom.xml增加mongo相关依赖,增加一些代码即可搭建成功。

1、增加mongdb-context.xml和mongodb.properties

mongdb-context.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 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/beans
 7        http://www.springframework.org/schema/beans/spring-beans.xsd
 8        http://www.springframework.org/schema/context
 9        http://www.springframework.org/schema/context/spring-context.xsd
10        http://www.springframework.org/schema/data/mongo
11        http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
12     <!--加载MongoDB属性配置文件-->
13     <context:property-placeholder location="classpath*:mongodb.properties"/>
14     <!--mongodb数据库配置-->
15     <mongo:mongo-client replica-set="${mongo.host}" id="mongo">
16         <mongo:client-options connections-per-host="${mongo.connectionsPerHost}"
17                 threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
18                 max-wait-time="${mongo.maxWaitTime}"
19                 socket-timeout="${mongo.socketTimeout}"
20                 connect-timeout="${mongo.connectTimeout}"/>
21     </mongo:mongo-client>
22     <!--工厂获取mongo实例-->
23     <mongo:db-factory id="mongoDbFactory" dbname="ivydb" mongo-ref="mongo"/>
24     <!--创建mongoTemplate-->
25     <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
26         <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
27     </bean>
28     <!--mongodb bean的仓库目录,会自动扫描扩展MongoRepository接口到接口进行注入-->
29     <mongo:repositories base-package="com.example.ivy"/>
30 </beans>

mongodb.properties

#mongoDB连接配置
mongo.host=127.0.0.1:27017
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#连接超时时间
mongo.connectTimeout=1000
#等待时间
mongo.maxWaitTime=1500
#Socket超时时间
mongo.socketTimeout=1500

 

2、修改applicationContext.xml,增加mongodb相关配置

1     <!--引入MongoDB配置文件-->
2     <import resource="mongodb-context.xml"/>

 

3、在pom.xml文件中增加mongodb相关依赖

 1       <dependency>
 2         <groupId>org.springframework.data</groupId>
 3         <artifactId>spring-data-mongodb</artifactId>
 4           <version>2.0.1.RELEASE</version>
 5       </dependency>
 6         <dependency>
 7             <groupId>org.mongodb</groupId>
 8             <artifactId>mongo-java-driver</artifactId>
 9             <version>3.11.2</version>
10         </dependency>

 

4、添加实体User

 1 package com.example.ivy.entity;
 2 
 3 import org.springframework.data.annotation.Id;
 4 import org.springframework.data.mongodb.core.mapping.Document;
 5 
 6 @Document(collection = "user")
 7 public class User {
 8     private int id;
 9     private String name;
10     private String password;
11 
12     public User() {
13     }
14 
15     public User(int id, String name, String password) {
16         this.id = id;
17         this.name = name;
18         this.password = password;
19     }
20 
21 
22     public String getName() {
23         return name;
24     }
25 
26     public void setName(String name) {
27         this.name = name;
28     }
29 
30     public String getPassword() {
31         return password;
32     }
33 
34     public void setPassword(String password) {
35         this.password = password;
36     }
37 
38     public int getId() {
39         return id;
40     }
41 
42     public void setId(int id) {
43         this.id = id;
44     }
45 
46     @Override
47     public String toString() {
48         return "User{" +
49                 "id=" + id +
50                 ", name='" + name + '\'' +
51                 ", password='" + password + '\'' +
52                 '}';
53     }
54 }

 

5、添加DAO

 1 package com.example.ivy.dao.impl;
 2 
 3 import com.example.ivy.entity.User;
 4 import com.example.ivy.dao.IBaseDao;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.data.mongodb.core.MongoTemplate;
 7 import org.springframework.data.mongodb.core.query.Criteria;
 8 import org.springframework.data.mongodb.core.query.Query;
 9 import org.springframework.data.mongodb.core.query.Update;
10 import org.springframework.stereotype.Component;
11 import org.springframework.stereotype.Repository;
12 import org.springframework.stereotype.Service;
13 
14 import javax.annotation.Resource;
15 import java.util.List;
16 
17 @Service(value = "iBaseDaoImpl")
18 public class IBaseDaoImpl implements IBaseDao {
19     //MongoTemplate是数据库和代码之间的接口,对数据库的操作都在它里面
20     @Resource
21     private MongoTemplate mongoTemplate;
22     @Override
23     public List<User> findAll() {
24         return mongoTemplate.findAll(User.class,"user");
25     }
26 
27     @Override
28     public User findOne(int id) {
29         Query query = new Query();
30         Criteria criteria = Criteria.where("_id").is(id);
31         query.addCriteria(criteria);
32         return mongoTemplate.findOne(query,User.class);
33     }
34 
35     @Override
36     public void update(User user) {
37         Update update = new Update();
38         Query query = new Query();
39         update.set("name", user.getName());
40         update.set("password", user.getPassword());
41         mongoTemplate.updateFirst(query.addCriteria(Criteria.where("_id").is(user.getId())), update, User.class);
42     }
43 
44     @Override
45     public void delete(int id) {
46         mongoTemplate.remove(Query.query(Criteria.where("_id").is(id)), User.class);
47     }
48 
49     @Override
50     public void add(User user) {
51         mongoTemplate.insert(user, "user");
52     }
53 
54     @Override
55     public void save(User user) {
56         mongoTemplate.save(user);
57     }
58 }

 

6、增加测试,查看环境是否搭建成功

 1 package com.example.ivy.dao.impl;
 2 
 3 import com.example.ivy.dao.IBaseDao;
 4 import com.example.ivy.entity.User;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.test.context.ContextConfiguration;
 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 
11 import javax.annotation.Resource;
12 import java.util.ArrayList;
13 import java.util.List;
14 
15 
16 @RunWith(SpringJUnit4ClassRunner.class)
17 @ContextConfiguration({"classpath:applicationContext.xml","classpath:mongodb-context.xml"})
18 public class IBaseDaoImplTest {
19     @Resource
20     private IBaseDaoImpl baseDao ;
21 
22     @Test
23     public void testMongo(){
24         for (int i = 0; i < 10; i++) {
25             User user = new User(i,"李丹" + i,"123");
26             baseDao.save(user);
27         }
28     }
29 
30     @Test
31     public void findAll() {
32         List<User> users = new ArrayList<>();
33         users = baseDao.findAll();
34         for (User user :
35                 users) {
36             System.out.println(user.toString());
37         }
38     }
39 
40     @Test
41     public void findOne() {
42         User user = new User();
43         user = baseDao.findOne(2);
44         System.out.println(user.toString());
45     }
46 
47     @Test
48     public void update() {
49         User user = new User(7,"七宝","mamababa");
50         baseDao.update(user);
51         System.out.println(baseDao.findOne(7).toString());
52     }
53 
54     @Test
55     public void delete() {
56         baseDao.delete(5);
57         if (null == baseDao.findOne(5)){
58             System.out.println("success");
59         }
60     }
61 
62     @Test
63     public void add() {
64         User user = new User(15,"ivy","ivy");
65         baseDao.add(user);
66         System.out.println(baseDao.findOne(15).toString());
67     }
68 
69 
70 }

 

这些只是很简单的配置,可以有更高级更简洁的配置进行搭建。 

这个项目可以完全在本地跑起来,不需要该任何配置。

项目完整代码:https://github.com/Ivyvivid/springmvc-mongo 

posted @ 2020-02-15 17:59  Ivy_Xu  阅读(504)  评论(0编辑  收藏  举报