Use MyEclipse to make a deletion and check the MongoDB database

Source: Internet
Author: User
Tags findone log log mongodb download

In front of a mongodb download and installation, the next article is the MongoDB database in the Java program basic functions:

Use MyEclipse the MongoDB database for pruning and checking operations

1. Importing the jar package

Use of Spring3.0; This kind of package, can be online query "MongoDB related jar package"

2. Create a person entity class

The source code is as follows:

public class person () {///attribute private String ID;//idprivate string name;//nameprivate int age;//age//constructor method Public person () {}p Ublic person (String Name,int age) {this.name = Name;this.age  = age;} Public person (String id,string name,int age) {This.id  = Id;this.name = Name;this.age  = age;} Public String toString () {return "person[id =" +id+ "name=" +name+ "age=" +age + "]";} /** The following is the property of the Get/set method **/................... /** the above code in a total of three construction methods for passing parameter data **/  


3. Create a method interface

1. create an interface named abstractrepository , with the following source code:


4. Interface method Implementation Class

1. create a class named personrepository and the real abstractrepository interface

The source code is as follows:

public class Personrepository implements abstractrepository{Private mongotemplate mongotemplate;/**mongotemplate get  /set method **/public Mongotemplate getmongotemplate () {return mongotemplate;} public void Setmongotemplate (Mongotemplate mongotemplate) {this.mongotemplate = mongotemplate;}//Query all public list<   Person> FindAll () {return getmongotemplate (). Find (new Query, Person.class);} The query modifies public void Findandmodify () {getmongotemplate (). Updatefirst (The new query (Criteria.where ("id")). is (ID), new Update    (). Inc ("age", 3)); Query public list<person> findAll () {Pattern pattern = Pattern.compile (regex,pattern.case_insensitive) conditionally; criteria = new Criteria ("name"). Regex (Pattern.tostring ()); return Getmongotemplate (). Find (criteria ), Person.class)}//Query object public by ID FindOne (String ID) {return getmongotemplate (). FindOne (New query (   Criteria.where ("id"). is (ID), person.class));}    Add public void Insert (person person) {getmongotemplate (). Insert (person); Delete all public voID RemoveAll () {list<person>list = This.finall (); If (List! = null) {for (person person:list) {getmongotemplate (). Remove (Person)}}}//delete public void Removeone (String ID) by id {criteria = Criteria.where ("id"). in (ID); if (criteria ! = NULL) {query query = new Query (criteria); if (Query! = null && getmongotemplate (). FindOne (Query,person.class)) { Getmongotemplate (). Remoev (Getmongotemplate (). FindOne (Query,person.class));}}}

5. Configure Applicationcontext.xml

We use Spring, naturally we want to configure ApplicationContext,because each version is different, so applicationcontext.xml in the system default header code please copy my following to use, otherwise the code error:

<?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-3.0.xsd http ://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/ Spring-mongo-1.0.xsd Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-3.0.xsd "> <!--the following for spring configuration--><!--setting up MongoDB connection port--><mongo:mongo host=" localhost "port=" 27017 "></mongo:mongo><bean id=" mongotemplate "class=" Org.springframework.data.document.mongodb.MongoTemplate "><constructor-arg ref=" MONGO "/>< Constructor-arg name= "DatabaseName" value= "DB"/><constructor-arg name= "defaultcollectionname" value= "person"/></bean><!--config bean, Inject Personreposiory's implementation class into--><bean id= "personrepository" class= "Com.mongo.repository.PersonRepository" >< Property Name= "Mongotemplate" ref= "Mongotemplate"/></bean></beans>

Applicationcontext.xml code, although a red X error appears in the upper-left corner . Prompt for

The information is probably a duplicate comment. No need to worry about him, as long as there is no red line code inside the good.


6. Write test class to test

Create a test class with the Mian Method: Thesource code for some of the methods is as follows:

public class test{private static log log = Logfactory.getlog (Test.class.getName ()); private abstractrepository PR = null; Initial method public void init () {log.debug ("start start"); ApplicationContext ac = new Classpathxmlapplicationcontext ("Applicationcontext.xml");p r = (personrepository) Ac.getbean ("Personrepository");} Add method public void Insert () {man p = new Person ("Id1", "Cuiran",;p R.insert (p); Log.debug ("Add success! ");}  Query object by id public void Finone () {String id = "ID1"; Person p =  Pr.findone (ID); Log.debug (P);}}  Query all public void FindAll () {list<person> List = Pr.findall (); Log.debug ("Query result:"); for (person p:list) {Log.debug ( p.tostring);}} Test method public void Start () {init ();//Perform initialization of insert ();//Execute Add Method}  //mian method public static void main (String args []) {Text Text = new text (); Test.start ();}  

Use MyEclipse to make a deletion and check the MongoDB database

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.