首页 > 代码库 > SpringMVC整合MongoDB

SpringMVC整合MongoDB

 首先,在pom文件中新增spring-data-mongodb的依赖:

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.8.1.RELEASE</version>
</dependency>
然后,新建spring-mongo.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:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<mongo:mongo host="localhost" port="27017"/>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="http://www.mamicode.com/test"/>
<!--<constructor-arg ref="userCredentials"/>-->
</bean>

</beans>
然后,在Spring的配置文件Spring-context中导入spring-mongo配置文件
<?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:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
   
   <context:annotation-config />
   <context:component-scan base-package="com.pudp" />    
   
   <!-- 导入mongodb的配置文件 -->
   <import resource="spring-mongo.xml"/>
       
</beans>

在model层,新建UserInfo类
@Document
public class UserInfo {

private String id;
private String username;
private String nickname;
private String sex;
private String age;

public UserInfo(){}

public UserInfo(String username, String nickname, String sex, String age){
super();
this.username = username;
this.nickname = nickname;
this.sex = sex;
this.age = age;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}
}
在DAO层,先新建BaseDao接口
public interface BaseDao {
void add(Object obj);
}
其后实现该接口
@Repository(value = "http://www.mamicode.com/baseDaoImpl")
public class MongoDBBaseDaoImpl implements BaseDao {

@Autowired
@Qualifier("mongoTemplate")
protected MongoTemplate mongoTemplate;

@Override
public void add(Object obj) {
this.mongoTemplate.insert(obj);
}
}
service层如下:
@Service(value = "http://www.mamicode.com/userInfoService")
public class UserInfoServiceImpl extends MongoDBBaseDaoImpl implements UserInfoService {
}
最后定义了Controller类如下:
@Controller
public class TestController {

@Resource
private UserInfoService userInfoService;

@RequestMapping(value = "http://www.mamicode.com/test",method = RequestMethod.GET)
public String test(){
UserInfo userInfo = new UserInfo();
userInfo.setNickname("nick");
userInfo.setUsername("Nike2");
userInfo.setSex("female");
userInfo.setAge("27");
userInfoService.add(userInfo);
return "test";
}
}
到此为止,一个简单的SpringMVC整合MongoDB就完成,启动该项目,在浏览器中输入http://localhost:8080/test后,查询Mongo数据库,可以发现其中多了一条数据。
技术分享

 

 
 


 
 


 

 

SpringMVC整合MongoDB