1. 程式人生 > >SpringMVC整合MongoDB

SpringMVC整合MongoDB

image 1.8 component size extends ges data- location one

首先,在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="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 = "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 = "userInfoService")
public class UserInfoServiceImpl extends MongoDBBaseDaoImpl implements UserInfoService {
}
最後定義了Controller類如下:
@Controller
public class TestController {

@Resource
private UserInfoService userInfoService;

@RequestMapping(value = "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