前边有一篇记录过不使用spring,直接在java代码中连接和操作mongodb数据库,这里就紧随其后记录一下使用spring的情况下,在java中简单操作mongodb。
maven导包配置:
因为涉及了sping以及springmvc,因此也需要导入它们相关的包:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ponent("UserDaoImp") public class UserDaoImp extends AbstractBaseMongoTemplete implements UserDao { /** * 查询所有数据 * * @author:tuzongxun * @Title: findAll * @Description: TODO * @param @return * @date May 13, 2016 3:10:29 PM * @throws */ @Override public List<UserModel> findAll() { // 需要设置集合对应的尸体类和相应的集合名,从而查询结果直接映射 List<UserModel> userList = mongoTemplate.findAll(UserModel.class, "user"); return userList; } /** * 新增数据 * * @author:tuzongxun * @Title: insertUser * @Description: TODO * @param @param user * @date May 13, 2016 3:10:45 PM * @throws */ @Override public void insertUser(UserModel user) { // 设置需要插入到数据库的文档对象 DBObject object = new BasicDBObject(); object.put("userName", user.getUserName()); object.put("password", user.getPassword()); mongoTemplate.insert(object, "user"); } /** * 按条件删除数据 * * @author:tuzongxun * @Title: removeUser * @Description: TODO * @param @param userName * @date May 13, 2016 3:11:01 PM * @throws */ @Override public void removeUser(String userName) { // 设置删除条件,如果条件内容为空则删除所有 Query query = new Query(); Criteria criteria = new Criteria("userName"); criteria.is(userName); query.addCriteria(criteria); mongoTemplate.remove(query, "user"); } /** * 修改数据 * * @author:tuzongxun * @Title: updateUser * @Description: TODO * @param @param user * @date May 13, 2016 3:11:12 PM * @throws */ @Override public void updateUser(UserModel user) { // 设置修改条件 Query query = new Query(); Criteria criteria = new Criteria("userName"); criteria.is(user.getUserName()); query.addCriteria(criteria); // 设置修改内容 Update update = Update.update("password", user.getPassword()); // 参数:查询条件,更改结果,集合名 mongoTemplate.updateFirst(query, update, "user"); } /** * 根据条件查询 * * @author:tuzongxun * @Title: findForRequery * @Description: TODO * @param @param userName * @date May 13, 2016 4:08:15 PM * @throws */ @Override public List<UserModel> findForRequery(String userName) { Query query = new Query(); Criteria criteria = new Criteria("userName"); criteria.is(userName); query.addCriteria(criteria); // 查询条件,集合对应的实体类,集合名 List<UserModel> userList = mongoTemplate.find(query, UserModel.class, "user"); return userList; } }
测试类:
为了验证以上代码和配置的正确性,测试类代码如下:
package spring_mongo.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_mongo.models.UserModel; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring.xml" }) public class mongoTest { @Autowired private UserDao userDao; /** * 查询测试 * * @author:tuzongxun * @Title: monFindTest * @param * @return void * @date May 13, 2016 3:27:51 PM * @throws */ @Test public void monFindTest() { List<UserModel> userModels = userDao.findAll(); if (userModels != null && userModels.size() > 0) { for (UserModel user : userModels) { System.out.println(user.getUserName() + ":" + user.getPassword()); } } } /** * 插入数据测试 * * @author:tuzongxun * @Title: monInsertTest * @param * @return void * @date May 13, 2016 3:27:38 PM * @throws */ @Test public void monInsertTest() { UserModel user = new UserModel("test111", "123456"); userDao.insertUser(user); this.monFindTest(); } /** * 删除测试 * * @author:tuzongxun * @Title: monRemoveTest * @param * @return void * @date May 13, 2016 3:28:06 PM * @throws */ @Test public void monRemoveTest() { String userName = "test111"; userDao.removeUser(userName); this.monFindTest(); } /** * 测试修改 * * @author:tuzongxun * @Title: monUpdateTest * @param * @return void * @date May 13, 2016 3:50:08 PM * @throws */ @Test public void monUpdateTest() { UserModel user = new UserModel("test111", "test"); userDao.updateUser(user); this.monFindTest(); } /** * 按条件查询 * * @author:tuzongxun * @Title: monFindForRuq * @param * @return void * @date May 13, 2016 4:10:53 PM * @throws */ @Test public void monFindForRuq() { String userName = "test111"; List<UserModel> userModels = userDao.findForRequery(userName); if (userModels != null && userModels.size() > 0) { for (UserModel user : userModels) { System.out.println(user.getUserName() + ":" + user.getPassword()); } } } }
后期补上demo下载地址:demo
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。