Integrate MongoDB with Spring

Source: Internet
Author: User
Tags findone log log

Integrate MongoDB with Spring
Opening

Spring provides integration of non-relational databases, and Spring's spring-data package provides a toolkit for integrating mongoDB and redis. This blog introduces Spring and MongoDB integration.
Import Toolkit
The required toolkit includes the spring jar package, spring-common jar package, and spring-data mongoDB jar package. Spring jar packages include aop, asm, aspects, beans, context, core, expression, jdbc, and transaction. Spring-common jar packages include commons-beanutils, commons-codec, commons-collections, commons-io, commons-lang, commons-logging, and commons-pool. The jar packages of spring-data mongoDB include spring-data-commons-aspects, spring-data-commons-core, spring-data-document-core, spring-data-mongodb, spring- data-mongodb-cross-store, spring-data-mongodb-log4j, etc. Another one is mongo. jar.
Toolkit

Configuration File

  
             
          
               
                
       
        
       
      
     
            
               
            
            
    
   
   
Code Writing

Entity class:

public class Person implements Serializable {    /**     *      */    private static final long serialVersionUID = 3617931430808763429L;    private String id;       private String name;       private int age;    public Person() {        super();    }    public Person(String id, String name, int age) {        super();        this.id = id;        this.name = name;        this.age = age;    }    /**     * @return the id     */    public String getId() {        return id;    }    /**     * @param id the id to set     */    public void setId(String id) {        this.id = id;    }    /**     * @return the name     */    public String getName() {        return name;    }    /**     * @param name the name to set     */    public void setName(String name) {        this.name = name;    }    /**     * @return the age     */    public int getAge() {        return age;    }    /**     * @param age the age to set     */    public void setAge(int age) {        this.age = age;    }    /**     *      * @param name     * @param age     */    public Person(String name, int age) {        super();        this.name = name;        this.age = age;    }        public String toString() {               return Person[id=+id+,name=+name+,age=+age+];           }   }

DAO layer interface

public interface AbstractRepository {    public void insert(Person person);     public Person findOne(String id);       public List
  
    findAll();       public List
   
     findByRegex(String regex);    public void removeOne(String id);       public void removeAll();       public void findAndModify(String id);   }
   
  

DAO layer interface implementation

Public class PersonRepository implements actrepository {private writable template; @ Override public List
  
   
FindAll () {return getasktemplate (). find (new Query (), Person. class) ;}@ Override public void findAndModify (String id) {getasktemplate (). updateFirst (new Query (Criteria. where (id ). is (id), new Update (). inc (age, 3) ;}@ Override public List
   
    
FindByRegex (String regex) {Pattern pattern = Pattern. compile (regex, Pattern. CASE_INSENSITIVE); Criteria criteria = new Criteria (name ). regex (pattern. toString (); return getasktemplate (). find (new Query (criteria), Person. class) ;}@ Override public Person findOne (String id) {return getasktemplate (). findOne (new Query (Criteria. where (id ). is (id), Person. class) ;}@ Override public void insert (Person person) {getasktemplate (). insert (person) ;}@ Override public void removeAll () {List
    
     
List = this. findAll (); if (list! = Null) {for (Person person: list) {getasktemplate (). remove (person) ;}}@ Override public void removeOne (String id) {Criteria criteria = Criteria. where (id ). in (id); if (criteria = null) {Query query = new Query (criteria); if (query! = Null & getasktemplate (). findOne (query, Person. class )! = Null) getasktemplate (). remove (getasktemplate (). findOne (query, Person. class);}/*** @ return the specified template */public writable template getasktemplate () {return another template ;} /*** @ param template the specified template to set */public void set0000template (specified template) {this. extends template = extends template;} test program public class extends test {private static Log log = LogFactory. getLog (upload test. class. getName (); private AbstractRepository pr = null; public void init () {ApplicationContext ctx = new ClassPathXmlApplicationContext (applicationContext. xml); pr = (PersonRepository) ctx. getBean (personRepository);} public void insert () {Person p = new Person (cuiran, 27); pr. insert (p);} public void findOne () {String id = 50c83cb552c2ceb0463177d6; Person p = pr. findOne (id); log. debug (p);} public void listAll () {List
     
      
List = pr. findAll (); for (Person p: list) {log. debug (p. toString () ;}} public void start () {init (); // insert (); // listAll (); findOne ();} public static void main (String [] args) {synchronized test t = new synchronized test (); t. start ();}}
     
    
   
  
End
Using Spring to integrate MongoDB's underlying code encapsulation makes it much easier to use non-relational data MongoDB storage in projects.

 

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.