1.环境
Jdk:1.6.0_10-rc2
Spring3.1.2 下载
依赖jar文件:
2.相关配置
①.spring配置文件
<?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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 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"> <mongo:mongo id="mongo" host="127.0.0.1" port="27017" /> <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials"> <constructor-arg name="username" value="userName"/> <constructor-arg name="password" value="password"/> </bean> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg value="test-mongo" /> <constructor-arg ref="userCredentials" /> </bean> <bean id="userDao" class="com.x.mongodb.dao.impl.UserDao" /> </beans>
②.MongoTemplate Api
package com.x.mongodb.dao.support; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.data.mongodb.core.MongoTemplate; public abstract class AbstractBaseMongoTemplete implements ApplicationContextAware { protected MongoTemplate mongoTemplate; /** * 设置mongoTemplate * @param mongoTemplate the mongoTemplate to set */ public void setMongoTemplate(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { MongoTemplate mongoTemplate = applicationContext.getBean("mongoTemplate", MongoTemplate.class); setMongoTemplate(mongoTemplate); } } package com.x.mongodb.dao.impl; 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 com.x.mongodb.dao.IUserDao; import com.x.mongodb.dao.support.AbstractBaseMongoTemplete; import com.x.mongodb.entity.User; public class UserDao extends AbstractBaseMongoTemplete implements IUserDao { /** * 新增 * <br>------------------------------<br> * @param user */ public void insert(User user) { mongoTemplate.insert(user); } /** * 批量新增 * <br>------------------------------<br> * @param users */ public void insertAll(List<User> users) { mongoTemplate.insertAll(users); } /** * 删除,按主键id, 如果主键的值为null,删除会失败 * <br>------------------------------<br> * @param id */ public void deleteById(String id) { User user = new User(id, null, 0); mongoTemplate.remove(user); } /** * 按条件删除 * <br>------------------------------<br> * @param criteriaUser */ public void delete(User criteriaUser) { Criteria criteria = Criteria.where("age").gt(criteriaUser.getAge());; Query query = new Query(criteria); mongoTemplate.remove(query, User.class); } /** * 删除全部 * <br>------------------------------<br> */ public void deleteAll() { mongoTemplate.dropCollection(User.class); } /** * 按主键修改, * 如果文档中没有相关key 会新增 使用$set修改器 * <br>------------------------------<br> * @param user */ public void updateById(User user) { Criteria criteria = Criteria.where("id").is(user.getId()); Query query = new Query(criteria); Update update = Update.update("age", user.getAge()).set("name", user.getName()); mongoTemplate.updateFirst(query, update, User.class); } /** * 修改多条 * <br>------------------------------<br> * @param criteriaUser * @param user */ public void update(User criteriaUser, User user) { Criteria criteria = Criteria.where("age").gt(criteriaUser.getAge());; Query query = new Query(criteria); Update update = Update.update("name", user.getName()).set("age", user.getAge()); mongoTemplate.updateMulti(query, update, User.class); } /** * 根据主键查询 * <br>------------------------------<br> * @param id * @return */ public User findById(String id) { return mongoTemplate.findById(id, User.class); } /** * 查询全部 * <br>------------------------------<br> * @return */ public List<User> findAll() { return mongoTemplate.findAll(User.class); } /** * 按条件查询, 分页 * <br>------------------------------<br> * @param criteriaUser * @param skip * @param limit * @return */ public List<User> find(User criteriaUser, int skip, int limit) { Query query = getQuery(criteriaUser); query.skip(skip); query.limit(limit); return mongoTemplate.find(query, User.class); } /** * 根据条件查询出来后 再去修改 * <br>------------------------------<br> * @param criteriaUser 查询条件 * @param updateUser 修改的值对象 * @return */ public User findAndModify(User criteriaUser, User updateUser) { Query query = getQuery(criteriaUser); Update update = Update.update("age", updateUser.getAge()).set("name", updateUser.getName()); return mongoTemplate.findAndModify(query, update, User.class); } /** * 查询出来后 删除 * <br>------------------------------<br> * @param criteriaUser * @return */ public User findAndRemove(User criteriaUser) { Query query = getQuery(criteriaUser); return mongoTemplate.findAndRemove(query, User.class); } /** * count * <br>------------------------------<br> * @param criteriaUser * @return */ public long count(User criteriaUser) { Query query = getQuery(criteriaUser); return mongoTemplate.count(query, User.class); } /** * * <br>------------------------------<br> * @param criteriaUser * @return */ private Query getQuery(User criteriaUser) { if (criteriaUser == null) { criteriaUser = new User(); } Query query = new Query(); if (criteriaUser.getId() != null) { Criteria criteria = Criteria.where("id").is(criteriaUser.getId()); query.addCriteria(criteria); } if (criteriaUser.getAge() > 0) { Criteria criteria = Criteria.where("age").gt(criteriaUser.getAge()); query.addCriteria(criteria); } if (criteriaUser.getName() != null) { Criteria criteria = Criteria.where("name").regex("^" + criteriaUser.getName()); query.addCriteria(criteria); } return query; } } package com.x.mongodb.dao; import java.util.List; import com.x.mongodb.entity.User; public interface IUserDao { /** * 新增 * <br>------------------------------<br> * @param user */ void insert(User user); /** * 新增 * <br>------------------------------<br> * @param users */ void insertAll(List<User> users); /** * 删除,主键id, 如果主键的值为null,删除会失败 * <br>------------------------------<br> * @param id */ void deleteById(String id); /** * 按条件删除 * <br>------------------------------<br> * @param criteriaUser */ void delete(User criteriaUser); /** * 删除全部 * <br>------------------------------<br> */ void deleteAll(); /** * 修改 * <br>------------------------------<br> * @param user */ void updateById(User user); /** * 更新多条 * <br>------------------------------<br> * @param criteriaUser * @param user */ void update(User criteriaUser, User user); /** * 根据主键查询 * <br>------------------------------<br> * @param id * @return */ User findById(String id); /** * 查询全部 * <br>------------------------------<br> * @return */ List<User> findAll(); /** * 按条件查询 * <br>------------------------------<br> * @param criteriaUser * @param skip * @param limit * @return */ List<User> find(User criteriaUser, int skip, int limit); /** * 根据条件查询出来后 在去修改 * <br>------------------------------<br> * @param criteriaUser 查询条件 * @param updateUser 修改的值对象 * @return */ User findAndModify(User criteriaUser, User updateUser); /** * 查询出来后 删除 * <br>------------------------------<br> * @param criteriaUser * @return */ User findAndRemove(User criteriaUser); /** * count * <br>------------------------------<br> * @param criteriaUser * @return */ long count(User criteriaUser); } [java] view plain copy print? package com.x.mongodb.entity; import java.io.Serializable; import java.util.HashMap; import java.util.Map; public class User implements Serializable { private static final long serialVersionUID = -5785857960597910259L; private String id; private String name; private int age; /** * <br>------------------------------<br> */ public User() { } /** * * <br>------------------------------<br> * @param id * @param name * @param age */ public User(String id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } /** * 获得id * @return the id */ public String getId() { return id; } /** * 设置id * @param id the id to set */ public void setId(String id) { this.id = id; } /** * 获得name * @return the name */ public String getName() { return name; } /** * 设置name * @param name the name to set */ public void setName(String name) { this.name = name; } /** * 获得age * @return the age */ public int getAge() { return age; } /** * 设置age * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * toString */ public String toString() { Map<String, String> map = new HashMap<String, String>(); map.put("id", id); map.put("name", name); map.put("age", String.valueOf(age)); return map.toString(); } } import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.x.mongodb.dao.IUserDao; import com.x.mongodb.entity.User; public class UserDaoTest { /** * 新增 * <br>------------------------------<br> */ @Test public void testInsert() { getUserDao().insert(new User(null, "testUser", 21)); } /** * 批量新增 * <br>------------------------------<br> */ @Test public void testInsertAll() { List<User> list = new ArrayList<User>(); for (int i = 0; i < 10; i++) { list.add(new User(null, "testUser" + i, 21 + i)); } getUserDao().insertAll(list); } /** * 根据主键删除 * <br>------------------------------<br> */ @Test public void testDeleteById() { String id = "5058184ec85607e42c4bfad8"; getUserDao().deleteById(id); } /** * 条件删除 * <br>------------------------------<br> */ @Test public void testDelete() { //删除年龄大于25的 getUserDao().delete(new User(null, null, 25)); } /** * 删除全部 * <br>------------------------------<br> */ @Test public void testDeleteAll() { getUserDao().deleteAll(); } /** * 修改 根据id修改 * <br>------------------------------<br> */ @Test public void testUpdateById() { getUserDao().updateById(new User("50581c08c856346f02e9842c", "张三", 100)); } /** * 修改多个 * <br>------------------------------<br> */ @Test public void update() { //修改年龄大于29岁的 姓名为“王五“ User criteriaUser = new User(null, null, 29); User user = new User(null, "王五", 39); getUserDao().update(criteriaUser, user); } /** * 按主键查询, 如果不存在 返回null * <br>------------------------------<br> */ @Test public void testFindById() { User user = getUserDao().findById("50581c08c856346f02e98425"); print(user); } /** * 查询全部 * <br>------------------------------<br> */ @Test public void testFindAll() { List<User> list = getUserDao().findAll(); print(list); } /** * 按条件查询 * <br>------------------------------<br> */ @Test public void testFind() { //查询25岁以上的, 分页 User criteriaUser = new User(null, null, 20); List<User> list = getUserDao().find(criteriaUser, 1, 10); print(list); } /** * 查询出来后 修改 * <br>------------------------------<br> */ @Test public void testFindAndModify() { User criteriaUser = new User("50581c08c856346f02e9842d", null, 0); User updateUser = new User(null, "张三", 100); updateUser = getUserDao().findAndModify(criteriaUser, updateUser); print(updateUser); } /** * 查询出来后 删除 * <br>------------------------------<br> */ @Test public void testFindAndRemove() { User criteriaUser = new User("50581c08c856346f02e9842d", null, 0); criteriaUser = getUserDao().findAndRemove(criteriaUser); print(criteriaUser); } /** * count * <br>------------------------------<br> */ @Test public void testCount() { User criteriaUser = new User(null, "test", 0); long count = getUserDao().count(criteriaUser); print(count); } public void print(Object object) { if (object == null || !(object instanceof Collection)) { System.out.println(object); return; } List<?> list = (List<?>) object; for (Object obj : list) { System.out.println(obj); } } public IUserDao getUserDao() { String configLocations = "applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configLocations); IUserDao userDao = applicationContext.getBean("userDao", IUserDao.class); return userDao; } }