Simple crud for spring MongoDB

Source: Internet
Author: User

Official reference: http://www.mkyong.com/mongodb/
Use myeclipse to add a Maven-Project project. Before that, you may need to configure your m2eclipse
1. Configure Maven to your own installed version.

2. Set user settings
Of course, you can also modify the default directory of the repository and enter the maven installation directory CONF/setting. xml.
In the <Settings> node
Add: <localrepository> D:/myrepository </localrepository>.

Pom. xml

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.test</groupId>  <artifactId>mongodb</artifactId>  <version>0.0.1-SNAPSHOT</version>  <name>springmongodb</name>  <description>This is test case</description>  <build>    <plugins>      <plugin>        <artifactId>maven-compiler-plugin</artifactId>        <configuration>          <source>1.6</source>          <target>1.6</target>        </configuration>      </plugin>    </plugins>  </build>    <repositories>        <repository>            <id>spring-milestone</id>            <name>Spring Maven MILESTONE Repository</name>            <url>http://maven.springframework.org/milestone</url>        </repository>    </repositories>     <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.8.2</version>            <scope>test</scope>        </dependency>         <!-- Spring framework -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>3.0.5.RELEASE</version>        </dependency>         <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>3.0.5.RELEASE</version>        </dependency>         <!-- mongodb java driver -->        <dependency>            <groupId>org.mongodb</groupId>            <artifactId>mongo-java-driver</artifactId>            <version>2.5.2</version>        </dependency>         <dependency>            <groupId>org.springframework.data</groupId>            <artifactId>spring-data-mongodb</artifactId>            <version>1.0.0.M2</version>        </dependency>         <dependency>            <groupId>cglib</groupId>            <artifactId>cglib</artifactId>            <version>2.2</version>        </dependency>     </dependencies>    </project>

After finishing the Pom. xml file, run project-right-click-run-Maven install
After downloading the jar package, you can write the code after the test is successful.
Springbench config. Java

Package entity; import Java. util. list; public class user {private string ID; private string username; private string password; // set Private list <string> position in the document; // You must declare the default constructor, otherwise, an error will be reported (not instantiated) public user () {} 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 GetPassword () {return password;} public void setpassword (string password) {This. password = password;} public list <string> getposition () {return position;} public void setposition (list <string> position) {This. position = position;} public user (string ID, string username, string password, list <string> position) {super (); this. id = ID; this. username = username; this. password = password; this. position = position;} public user (string username, string password, list <string> position) {super (); this. username = username; this. password = password; this. position = position ;}}

Test class:

Package run; import Java. util. arraylist; import Java. util. iterator; import Java. util. list; import Java. util. set; import Org. JUnit. test; import Org. springframework. context. applicationcontext; import Org. springframework. context. annotation. annotationconfigapplicationcontext; import org.springframework.data.doc ument. mongoDB. mongooperations; import org.springframework.data.doc ument. mongoDB. query. criteria; import org.springframework.data.doc ument. mongoDB. query. query; import org.springframework.data.doc ument. mongoDB. query. update; import COM. config. springbench config; import entity. user; public class app {private applicationcontext CTX = new annotationconfigapplicationcontext (springmongoconfig. class); Private extends operations = (extends operations) CTX. getbean ("jsontemplate"); @ test public void testsave () {list <string> List = new arraylist <string> (); For (INT I = 0; I <100; I ++) {list. add ("development" + I); list. add ("manager" + I); User user = new user ("John" + I, "123" + I, list); // The first parameter: the name of the table to be saved. By default, it is a set (table) with the same name as the Instance name user. // If the table does not exist, it is automatically created. If the table name user is a keyword, do not use operations. save ("userinfo", user); list. clear () ;}@ test public void testupdate () {operations. updatefirst ("userinfo", new query (criteria. where ("username "). is ("john0"), update. update ("password", "newpassword") ;}@ test public void testdelete () {operations. remove ("userinfo", new query (criteria. where ("ID "). is ("4ffe3486b41f8ed41269a729"), user. class) ;}@ test public void testquery () {// output all table names set <string> set = operation operations. getcollectionnames (); iterator <string> it = set. iterator (); While (it. hasnext () {system. out. println (it. next ();} // User: List of table names to be queried <user> Users = operation operations. getcollection ("userinfo", user. class); system. out. println (users. size (); For (User U: Users) {system. out. println (U. GETID ());}}}

Source code download

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.