MongoDB Learning (4) Integrate springdata with MongoDB

Source: Internet
Author: User
Tags findone

1, configuration file mongo-config.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/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org /Schema/data/parse http: // www.springframework.org/schema/data/#/spring-developer-1.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <! -- Define the Mongo object and related IP addresses and ports --> <Mongo: Mongo host = "localhost" Port = "27017"/> <! -- Mongo factory, which is used to obtain the Mongo instance --> <Mongo: DB-factory dbname = "mongodemo"/> <bean id = "mappingcontext" class = "org. springframework. data. mongoDB. core. mapping. mongomappingcontext "> </bean> <bean id =" defaultmongopolictypemapper "class =" org. springframework. data. mongoDB. core. convert. defaultpolictypemapper "> <constructor-Arg name =" typekey "value =" null "/> </bean> <! -- The mongomappingconverter class serves as the object ing function. The Java class is converted to a MongoDB document through annotations. --> <Bean id = "mappingw.converter" class = "org. springframework. data. mongoDB. core. convert. mappingmongoconverter "> <constructor-Arg name =" mongodbfactory "ref =" mongodbfactory "/> <constructor-Arg name =" mappingcontext "ref =" mappingcontext "/> <property name =" typemapper "ref =" defaultmongotypemapper "/> </bean> <! -- MongoDB's main operation object. All operations for adding, deleting, modifying, and querying MongoDB are completed through it --> <bean id = "mongotemplate" class = "org. springframework. data. mongoDB. core. extends template "> <constructor-Arg name =" mongodbfactory "ref =" mongodbfactory "/> <constructor-Arg name =" mongoconverter "ref =" mappingmongoconverter "/> </bean> </beans>

2. entity Object User. Java

View code

Public class user {private string uname; private string upwd; public user (string uname, string upwd) {This. uname = uname; this. upwd = upwd;} Public String getuname () {return uname;} public void setuname (string uname) {This. uname = uname;} Public String getupwd () {return upwd;} public void setupwd (string upwd) {This. upwd = upwd;} Public String tostring () {return "[user information: uname:" + uname + "\ tupwd:" + upwd + "]";}

3. Tool class mongoutil. Java

Public class extends util {static applicationcontext CTX = new genericxmlapplicationcontext ("mongo-config.xml"); Private Static extends operations = NULL;/*** get the extends operations object (responsible for crud work) * @ return */public static operation operations getoperation operation () {If (Operation operations = NULL) {operation operations = (Operation operations) CTX. getbean ("your template");} return your operations;}/*** variable length parameter, construct the query condition * @ Param criterias * @ return */public static query setquery (criteria... criterias) {query = new query (); For (INT I = 0; I <criterias. length; I ++) {query. addcriteria (criterias [I]);} return NULL ;}}

4. crudtest. Java

Package COM. archie. test; import Java. util. list; import Org. springframework. data. mongoDB. core. repeated operations; import Org. springframework. data. mongoDB. core. query. criteria; import Org. springframework. data. mongoDB. core. query. query; import Org. springframework. data. mongoDB. core. query. update; import COM. archie. util. mongoutil; import COM. archie. VO. user;/*** add, delete, query, modify, and test * @ author archie2010 ** since 04:43:39 */public class crudtest {public static void main (string [] ARGs) {operations operation = Specified util. getmongooperation ();
User user = new user ("Archie", "123"); // added operation. save (user, "users"); criteria criterianame = criteria. where ("uname "). is ("Tom"); criteria criteriapwd = criteria. where ("upwd "). is ("123"); query = new query (); query. addcriteria (criterianame); query. addcriteria (criteriapwd); User userlogin = operation. findone (query, user. class, "users"); system. out. println (userlogin); // query user saveduser = operation. findone (new query (criteria. where ("uname "). is ("Archie"), user. class, "users"); system. out. println ("saveduser:" + saveduser); // update operation. updatemulti (new query (criteria. where ("uname "). is ("Archie"), update. update ("upwd", "1111111111"), "users"); // query user updateduser = operation. findone (new query (criteria. where ("uname "). is ("Archie"), user. class, "users"); system. out. println ("updateduser:" + updateduser); // Delete the specified operation. remove (new query (criteria. where ("uname "). is ("Archie"), "users"); // query the list of sets <user> listuser = operation. findall (user. class, "users"); system. out. println ("number of user =" + listuser. size ());}}

Run:

 

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.