Use MyEclipse to add, delete, modify, and query MongoDB Databases
Use MyEclipse to add, delete, modify, and query MongoDB Databases
1. Import the JAR package
Spring3.0 is used. You can query "MongoDB JAR packages" online"
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 public Person () {} public 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 get/set Method of the attribute **/.......................... .......} /** the preceding Code contains three constructor methods for passing parameter data **/
3. Create a method Interface
1. Create an interface named AbstractRepository. The source code is as follows:
Public interface into actrepository () {// Add method public void insert (Person person); // query object public Person findOne (String ID) by id; // query all public lists
FindAll (); // Delete public void removeOne (String ID) by id; // delete all public void removeAll (); // query and modify public void findAndModfy (String id );}
4. Interface Method implementation class
1. Create a class named PersonRepository and use the actual javasactrepository Interface
The source code is as follows:
Public class PersonRepository implements actrepository {Private response Template Using template;/** response template's get/set Method **/public response template getresponse template () {return response template;} public void setresponse template (Response Template Using template) {this. required template = required template;} // query all public lists
FindAll () {return getasktemplate (). find (new Query, Person. class);} // query and modify public void findAndModify () {getmediatemplate (). updateFirst (new Query (Criteria. where ("id ")). is (id), new Update (). inc ("age", 3);} // query the public List by condition
FindAll () {Pattern pattern = Pattern. compile (regex, Pattern. CASE_INSENSITIVE); Criteria criteria = new Criteria ("name "). regex (pattern. toString (); return getasktemplate (). find (new Query (criteria), Person. class)} // query the object public Person findOne (String ID) {return getasktemplate () by id (). findOne (new Query (Criteria. where ("id "). is (id), Person. class);} // Add public void insert (Person person) {getasktemplate (). insert (person);} // delete all public void removeAll () {List
List = this. finAll (); If (list! = Null) {for (Person person: list) {getasktemplate (). remove (person) }}// Delete public void removeOne (String ID) {Criteria criteria = Criteria by id. where ("id "). in (id); if (criteria! = Null) {Query query = new Query (criteria); if (query! = Null & getasktemplate (). findOne (query, Person. class) {getasktemplate (). remoev (getasktemplate (). findOne (query, Person. class ));}}}}
5. Configure ApplicationContext. xml
When Spring is used, we need to configure ApplicationContext. Because the versions are different, the default system header code in ApplicationContext. xml should be copied to the following for use. Otherwise, the code will be incorrect:
ApplicationContext. xml Code, although the red X error message appears in the upper left corner. Prompt
The information is probably duplicated. Don't worry about him, as long as there is no red line in the code.
6. Write test class for testing
Create a Test class with the mian method: the source code of some methods is as follows:
Public class Test {private static log = logFactory. getlog (Test. class. getName (); private AbstractRepository pr = null; // initial method public void init () {log. debug ("START"); ApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext. xml "); pr = (PersonRepository) ac. getbean ("personRepository");} // Add method public void insert () {Person p = new Person ("id1", "cuiran", 27); pr. insert (p); log. debug ("add Added successfully !");} // Query the object public void finOne () {String ID = "id1"; Person p = pr. findOne (id); log. debug (p) ;}/// query all public void findAll () {List
List = pr. findAll (); log. debug ("query result:"); for (Person p: list) {log. debug (p. toString) ;}/// test method public void start () {init (); // execute initialization insert (); // execute the add method} // mian method public static void main (String args []) {Text text = new Text (); test. start ();}