In the previous article, we recorded that mongodb databases are directly connected and operated without spring in java code. Here we will record the simple operations on mongodb in java when spring is used. Maven import package configuration: Because sping and springmvc are involved, you also need to import their related packages: projectxmlnsmaven
In the previous article, we recorded that mongodb databases are directly connected and operated without spring in java code. Here we will record the simple operations on mongodb in java when spring is used. Maven import package configuration: Because sping and springmvc are involved, you also need to import their related packages: project xmlns = http: // maven
In the previous article, we recorded that mongodb databases are directly connected and operated without spring in java code. Here we will record the simple operations on mongodb in java when spring is used.
Maven import package Configuration:Because sping and springmvc are involved, you also need to import their related packages:
4.0.0
spring_mongo
spring_mongo
war
0.0.1-SNAPSHOT
spring_mongo Maven Webapp
http://maven.apache.org
org.springframework.data
spring-data-mongodb
1.8.0.RELEASE
org.mongodb
mongo-java-driver
3.0.3
commons-logging
commons-logging
1.2
org.springframework
spring-test
4.1.6.RELEASE
junit
junit
4.11
org.springframework
spring-context
4.1.7.RELEASE
org.springframework
spring-context-support
4.0.9.RELEASE
maven-compiler-plugin
2.3.2
1.7
1.7
UTF-8
${java.home}/lib/rt.jar;${java.home}/lib/jce.jar
spring_mongo
Basic spring Configuration:It mainly enables annotation scanning and so on:
Spring connects to mongodb and configures related factories:
Entity class corresponding to the database:Note that you need to implement the serialization interface and set the uid attribute here. Otherwise, you cannot directly convert the database return result to the object attribute in the operation:
package spring_mongo.models;import java.io.Serializable;public class UserModel implements Serializable { private static final long serialVersionUID = 1L; private String userName; private String password; public UserModel(String userName, String password) { super(); this.userName = userName; this.password = password; } 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; }}
You need to implement the ApplicationContextAware interface to obtain the mongodb-operated templete according to spring Configuration:
Package spring_mogo.dao.daoImp; import org. springframework. beans. beansException; import org. springframework. context. applicationContext; import org. springframework. context. applicationContextAware; import org. springframework. data. mongodb. core. optional template; public abstract class AbstractBaseMongoTemplete implements ApplicationContextAware {protected custom template;/*** @ Description Sets the Custom template * @ param custom template */public void setcustom template (custom template) {this. extends template = extends template;} @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {extends template = applicationContext. getBean ("jsontemplate", jsontemplate. class); setmediatemplate (jsontemplate );}}
Database Operation interfaces and corresponding implementation classes:Demonstrate the most basic addition, deletion, modification, and query. Note that the parameter declaration and conversion between the received and returned data are as follows: (1) interface:
Package spring_mogo.dao; import java. util. list; import spring_mongo.models.UserModel; public interface UserDao {/*** query data ** @ author: tuzongxun * @ Title: findAll * @ param @ return * @ return List
* @ Date May 13,201 6 3:07:39 * @ throws */public List
FindAll ();/***** add data ** @ author: tuzongxun * @ Title: insertUser * @ param user * @ return void * @ date May 13,201 6 3:09:45 * @ throws */public void insertUser (UserModel user ); /*** delete data ** @ author: tuzongxun * @ Title: removeUser * @ param userName * @ return void * @ date May 13,201 6 3:09:55 * @ throws */public void removeUser (String userName ); /*** modify data ** @ author: tuzongxun * @ Title: updateUser * @ param user * @ return void * @ date May 13,201 6 3:10:06 * @ throws */public void updateUser (UserModel user ); /*** query by condition ** @ author: tuzongxun * @ Title: findForRequery * @ param * @ return void * @ date May 13,201 6 3:23:37 * @ throws */public List
FindForRequery (String userName );}
(2) Implementation class. here we need to inherit the AbstractBaseMongoTemplete class to obtain the mongoTemplete for various operations:
Package spring_mogo.dao.daoImp; import java. util. list; 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 org. springframework. stereotype. component; import spring_mogo.dao.UserDao; import spring_assist.models.usermodel; import com. mongodb. basicDBObject; import com. mongodb. DBObject; @ Component ("UserDaoImp") public class UserDaoImp extends AbstractBaseMongoTemplete implements UserDao {/*** query all data ** @ author: tuzongxun * @ Title: findAll * @ Description: TODO * @ param @ return * @ date May 13,201 6 3:10:29 * @ throws */@ Override public List
FindAll () {// you need to set the corresponding corpse class of the Set and the corresponding collection name, so that the query result is directly mapped to the List
UserList = Invalid template. findAll (UserModel. class, "user"); return userList;}/*** add data ** @ author: tuzongxun * @ Title: insertUser * @ Description: TODO * @ param user * @ date May 13,201 6 3:10:45 * @ throws */@ Override public void insertUser (UserModel user) {// set the Document object DBObject object to be inserted to the database = new BasicDBObject (); object. put ("userName", user. getUserName (); object. put ("password", user. getPassword (); login template. insert (object, "user");}/*** delete data by condition ** @ author: tuzongxun * @ Title: removeUser * @ Description: TODO * @ param userName * @ date May 13,201 6 3:11:01 * @ throws */@ Override public void removeUser (String userName) {// sets the deletion condition, if the condition content is null, delete all Query queries = new query (); Criteria criteria = new Criteria ("userName"); criteria. is (userName); query. addCriteria (criteria); your template. remove (query, "user");}/*** modify data ** @ author: tuzongxun * @ Title: updateUser * @ Description: TODO * @ param user * @ date May 13,201 6 3:11:12 * @ throws */@ Override public void updateUser (UserModel user) {// set the condition for modifying Query query = new Query (); Criteria criteria = new Criteria ("userName"); criteria. is (user. getUserName (); query. addCriteria (criteria); // Set Update update = Update. update ("password", user. getPassword (); // parameter: Query condition, change result, Set Name: Invalid template. updateFirst (query, update, "user");}/*** query by condition ** @ author: tuzongxun * @ Title: findForRequery * @ Description: TODO * @ param userName * @ date May 13,201 6 4:08:15 * @ throws */@ Override public List
FindForRequery (String userName) {Query query = new Query (); Criteria criteria = new Criteria ("userName"); criteria. is (userName); query. addCriteria (criteria); // query condition, object class corresponding to the set, Set Name List
UserList = login template. find (query, UserModel. class, "user"); return userList ;}}
Test class:To verify the correctness of the above Code and configuration, the test class code is as follows:
Package spring_cmd.test; import java. util. list; import org. junit. test; import org. junit. runner. runWith; import org. springframework. beans. factory. annotation. autowired; import org. springframework. test. context. contextConfiguration; import org. springframework. test. context. junit4.SpringJUnit4ClassRunner; import spring_mogo.dao.UserDao; import spring_assist.models.usermodel; @ RunWith (SpringJUnit4ClassRunner. class) @ ContextConfiguration (locations = {"classpath: spring. xml "}) public class login test {@ Autowired private UserDao userDao;/*** query test ** @ author: tuzongxun * @ Title: monFindTest * @ param * @ return void * @ date May 13,201 6 3:27:51 PM * @ throws */@ Test public void monFindTest () {List
UserModels = userDao. findAll (); if (userModels! = Null & userModels. size ()> 0) {for (UserModel user: userModels) {System. out. println (user. getUserName () + ":" + user. getPassword () ;}}/ *** test data insertion ** @ author: tuzongxun * @ Title: monInsertTest * @ param * @ return void * @ date May 13,201 6 3:27:38 * @ throws */@ Test public void monInsertTest () {UserModel user = new UserModel ("test111 ", & quot; 123456 & quot;); userDao. insertUser (user); this. monFindTest ();}/*** delete test *** @ author: tuzongxun * @ Title: monRemoveTest * @ param * @ return void * @ date May 13,201 6 3:28:06 * @ throws */@ Test public void monRemoveTest () {String userName = "test111"; userDao. removeUser (userName); this. monFindTest ();}/*** test modification *** @ author: tuzongxun * @ Title: monUpdateTest * @ param * @ return void * @ date May 13,201 6 3:50:08 PM * @ throws */@ Test public void monUpdateTest () {UserModel user = new UserModel ("test111 ", "test"); userDao. updateUser (user); this. monFindTest ();}/*** query by condition ** @ author: tuzongxun * @ Title: monFindForRuq * @ param * @ return void * @ date May 13,201 6 4:10:53 PM * @ throws */@ Test public void monFindForRuq () {String userName = "test111"; List
UserModels = userDao. findForRequery (userName); if (userModels! = Null & userModels. size ()> 0) {for (UserModel user: userModels) {System. out. println (user. getUserName () + ":" + user. getPassword ());}}}}