SPRINGMVC Integrated MongoDB

Source: Internet
Author: User

First, add the Spring-data-mongodb dependency in the Pom file:

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.8.1.RELEASE</version>
</dependency>
Then, create a new 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>
then, import the Spring-mongo configuration file in spring's profile Spring-context
<?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=" c Om.pudp "/> <!--importing MongoDB configuration Files--<import resource=" Spring-mongo.xml "/> </beans>

in the model layer, create a new UserInfo class
@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;
}
}
on the DAO layer, first create a new Basedao interface
Public interface Basedao {
void Add (Object obj);
}
The interface is then implemented
@Repository (value = "Basedaoimpl")
public class Mongodbbasedaoimpl implements Basedao {

@Autowired
@Qualifier ("Mongotemplate")
protected Mongotemplate mongotemplate;

@Override
public void Add (Object obj) {
This.mongoTemplate.insert (obj);
}
}
The service layer is as follows:
@Service (value = "Userinfoservice")
public class Userinfoserviceimpl extends Mongodbbasedaoimpl implements Userinfoservice {
}
Finally, the controller class is defined as follows:
@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";
}
}
So far, a simple SPRINGMVC integration MongoDB is complete, start the project, in the browser input http://localhost:8080/test, query the MONGO database, you can find that one of the data.





SPRINGMVC Integrated MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.