一、前言
- MongoDB 是一个可扩展的、高性能的、开源的NoSQL数据库,跟传统的数据库不一样,MongoDB并不是将数据存储在表中,他将数据结构化为一个类似于JSON的文档中。这篇文章就是展示如何使用Java基于MongoDB和Spring Data创建一个CRUD应用。
- Spring Data for MongoDB Spring Data for MongoDB提供了一个类似于基于Sping编程模型的NoSQL数据存储。Spring Data for MongoDB提供了很多特性,它使很多MongoDB的Java开发者解放了很多。MongoTemplate helper类支持通用的Mongo操作。它整合了文档和POJO之间的对象映射。通常,他会转换数据库访问异常到Spring中的异常结构。使用起来非常的方便。
MongoDB的API提供了DBObject接口来实现BSONObject的操作方法,BasicDBObject是具体实现。但是并没有提供DBObject与BeanObject的转换。
代码要处理Bean中的各种类型,并且要控制持久化时的深度:
- 基础类型(int, float, boolean...)
- 基础扩展类型(Integer, Float, Boolean)
- 枚举
- ObjectId
- 普通对象 extends Object
- 业务对象(拥有ObjectId的对象)
- 容器List, Map, Set(处理元素时要把上面的类型再处理一遍)
以上这些东西,spring-data-mongo 都可以实现
参考质料
- http://projects.spring.io/spring-data-mongodb/
- http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/
- http://docs.mongodb.org/manual/core/introduction/
- spring官方提供的整合mongodb spring-data-mongodb-reference.pdf
本项目是亲测过的
二、项目
-
所依赖的jar包
-
spring的配置文件 applicationContext.xml如下:
<?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/spring-beans-3.0.xsd"> <context:property-placeholder location="classpath:mongodb.properties" /> <!-- 对mongodb的一些参数连接的设置 --> <mongo:mongo host="${mongo.host}" port="${mongo.port}"> <mongo:options connections-per-host="${mongo.connectionsPerHost}" threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}" auto-connect-retry="${mongo.autoConnectRetry}" socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}" slave-ok="${mongo.slaveOk}" write-number="1" write-timeout="0" write-fsync="true" /> </mongo:mongo> <!-- 配置一个MongoDbFactory,u数据库名叫mydb,mongo-ref指向mongo --> <!-- 这个东西就是上面那个mongo:mongo配置的bean --> <mongo:db-factory dbname="${mongo.database}" mongo-ref="mongo" /> <!-- 这个bean的默认名称是mongoTemplate --> <!-- 类似于spring里的jdbcTemplate,需要注入MongoDbFactory --> <!-- mongoConverter不是必须的 --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> </bean> <!-- 注入spring提供的模板 --> <bean id="personRepository" class="com.spring.mongo.dao.impl.PersonRepository"> <property name="mongoTemplate" ref="mongoTemplate"></property> </bean> <context:annotation-config /> <!-- Scan components for annotations within the configured package --> <context:component-scan base-package="com"> <context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration" /> </context:component-scan> </beans>
mongodb.properties文件如下:mongo.host=192.168.0.19 mongo.port=27017 mongo.database=xlk mongo.connectionsPerHost=8 mongo.threadsAllowedToBlockForConnectionMultiplier=4 mongo.connectTimeout=1500 mongo.maxWaitTime=1500 mongo.autoConnectRetry=true mongo.socketKeepAlive=true mongo.socketTimeout=1500 mongo.slaveOk=true
注:<mongo:mongo host="${mongo.host}" port="${mongo.port}" replica-set="">
在mongo里也可以配置副本集分片模式,引用官方的解释:
- 使用@Document注解指明一个领域对象将被持久化到MongoDB中。@Id注解identifies。
package com.spring.mongo.domain; import java.io.Serializable; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document; /** * * person实体类 </br> * 类名: Person </br> * 日期: 2014-4-15 上午11:43:08 </br> * @author 许立亢 * @version 1.0 */ @Document public class Person implements Serializable { /** * */ private static final long serialVersionUID = 3617931430808763429L; @Id private String id; @Indexed private String name; private int age; public Person() { super(); } public Person(String id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } /** * @return the id */ public String getId() { return id; } /** * @param id the id to set */ public void setId(String id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * * @param name * @param age */ public Person(String name, int age) { super(); this.name = name; this.age = age; } public String toString() { return "Person[id="+id+",name="+name+",age="+age+"]"; } }
- 创建一个简单的接口,这个接口带有CRUD方法。
package com.spring.mongo.dao; import java.util.List; import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions; import com.mongodb.CommandResult; import com.spring.mongo.domain.Person; /** * * person接口操作 </br> * 类名: AbstractRepository </br> * 日期: 2014-4-16 上午09:24:17 </br> * @author 许立亢 * @version 1.0 */ public interface AbstractRepository { public void insert(Person person); public Person findOne(String id); public List<Person> findAll(); public List<Person> findByRegex(String regex); public void removeOne(String id); public void removeAll(); public void updateInc(String id,String key,Number inc); public void updateSet(String id,String key,Object value); public void mapReduce(String inputCollectionName, String mapFunction,String reduceFunction,MapReduceOptions mapReduceOptions,Class<Person> entityClass); public CommandResult executeCommand(String jsonCommand); public void createCollection(); public void dropCollection(); }
实现刚才的接口, 程序启动时,注入MongoTemplate模板 ,/** * */ package com.spring.mongo.dao.impl; import java.util.List; import java.util.regex.Pattern; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions; import org.springframework.data.mongodb.core.mapreduce.MapReduceResults; 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.mongodb.BasicDBList; import com.mongodb.BasicDBObject; import com.mongodb.CommandResult; import com.spring.mongo.dao.AbstractRepository; import com.spring.mongo.domain.Person; /** * * person持久化类 </br> * 类名: PersonRepository </br> * 日期: 2014-4-16 上午09:50:14 </br> * @author 许立亢 * @version 1.0 */ public class PersonRepository implements AbstractRepository { private MongoTemplate mongoTemplate; /** * * 查找所以的document</br> * 日期:2014-4-16 上午09:49:47 * @return * @see com.spring.mongo.dao.AbstractRepository#findAll() */ @Override public List<Person> findAll() { return getMongoTemplate().find(new Query(), Person.class); } /** * * 根据id查找一个document</br> * 日期:2014-4-16 上午09:48:38 * @param id * @return * @see com.spring.mongo.dao.AbstractRepository#findOne(java.lang.String) */ @Override public Person findOne(String id) { return getMongoTemplate().findOne( new Query(Criteria.where("id").is(id)), Person.class); } /** * * 修改一条记录,增量修改</br> * 日期:2014-4-16 上午09:49:22 * @param id * @see com.spring.mongo.dao.AbstractRepository#findAndModify(java.lang.String) */ @Override public void updateInc(String id,String key,Number inc) { getMongoTemplate().updateFirst(new Query(Criteria.where("id").is(id)), new Update().inc(key, inc), Person.class); } /** * * 修改一条记录</br> * 日期:2014-4-16 上午09:59:01 * @param id * @param key * @param value * @see com.spring.mongo.dao.AbstractRepository#updateSet(java.lang.String, java.lang.String, java.lang.Object) */ @Override public void updateSet(String id,String key,Object value) { getMongoTemplate().updateFirst(new Query(Criteria.where("id").is(id)), new Update().set(key, value), Person.class); } /* * (non-Javadoc) * * @see com.mongo.dao.AbstractRepository#findByRegex(java.lang.String) */ @Override public List<Person> findByRegex(String regex) { Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Criteria criteria = new Criteria("name").regex(pattern.toString()); return getMongoTemplate().find(new Query(criteria), Person.class); } /** * * 插入一个记录document</br> * 日期:2014-4-16 上午09:48:17 * @param person * @see com.spring.mongo.dao.AbstractRepository#insert(com.spring.mongo.domain.Person) */ @Override public void insert(Person person) { getMongoTemplate().insert(person); } /** * * 删除全部的记录</br> * 日期:2014-4-16 上午09:47:57 * @see com.spring.mongo.dao.AbstractRepository#removeAll() */ @Override public void removeAll() { List<Person> list = this.findAll(); if (list != null) { for (Person person : list) { getMongoTemplate().remove(person); } } } /** * * 根据id删除一个document</br> * 日期:2014-4-16 上午09:47:26 * @param id * @see com.spring.mongo.dao.AbstractRepository#removeOne(java.lang.String) */ @Override public void removeOne(String id) { Criteria criteria = Criteria.where("id").in(id); if (criteria != null) { Query query = new Query(criteria); if (query != null && getMongoTemplate().findOne(query, Person.class) != null) getMongoTemplate().remove( getMongoTemplate().findOne(query, Person.class)); } } /** * 创建一个表collection * </br> * 日期:2014-4-16 上午10:01:28 * @see com.spring.mongo.dao.AbstractRepository#createCollection() */ public void createCollection() { if (!mongoTemplate.collectionExists(Process.class)) { mongoTemplate.createCollection(Process.class); } } /** * * 删除一个表collection</br> * 日期:2014-4-16 上午10:03:16 * @see com.spring.mongo.dao.AbstractRepository#dropCollection() */ public void dropCollection() { if (mongoTemplate.collectionExists(Process.class)) { mongoTemplate.dropCollection(Process.class); } } public void mapReduce(String inputCollectionName, String mapFunction,String reduceFunction,MapReduceOptions mapReduceOptions,Class<Person> entityClass){ MapReduceResults<Person> mapReduce = mongoTemplate.mapReduce(inputCollectionName, mapFunction, reduceFunction,mapReduceOptions, Person.class); BasicDBList list = (BasicDBList)mapReduce.getRawResults().get("results"); for (int i = 0; i < list.size(); i ++) { BasicDBObject obj = (BasicDBObject)list.get(i); System.out.println(obj.toString()); } } public CommandResult executeCommand(String jsonCommand){ // String CommandResult commandResult = mongoTemplate.executeCommand(jsonCommand); return commandResult; } /** * @return the mongoTemplate */ public MongoTemplate getMongoTemplate() { return mongoTemplate; } /** * @param mongoTemplate * the mongoTemplate to set */ public void setMongoTemplate(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } }
- 后写出我们的测试类开始进行测试
-
package spring; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions; import com.spring.mongo.dao.AbstractRepository; import com.spring.mongo.dao.impl.PersonRepository; import com.spring.mongo.domain.Person; /** * * 创建测试用例 </br> * 类名: MongoUserTest </br> * 日期: 2014-4-16 上午10:24:56 </br> * @author 许立亢 * @version 1.0 */ public class MongoUserTest { private static Log log = LogFactory.getLog(MongoUserTest.class.getName()); private AbstractRepository pr=null; @Before public void init(){ log.debug("开始启动"); ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); pr= (PersonRepository)ctx.getBean("personRepository"); } @Test public void insert(){ Person p=new Person("许立亢",24); pr.insert(p); log.debug("添加成功"); } @Test public void findOne(){ String id="534ddf83aa17bf2cdb40af4a"; Person p= pr.findOne(id); log.info(p); } @Test public void listAll(){ List<Person> list=pr.findAll(); log.debug("查询结果如下:"); for (Person p:list){ log.debug(p.toString()); System.out.println(p.toString()); } } @Test public void removeOne(){ pr.removeOne("534ddf83aa17bf2cdb40af4a"); log.info("删除成功"); } @Test public void removeAll(){ pr.removeAll(); log.info("全部删除....."); } @Test public void updateInc(){ pr.updateInc("534ddfe4aa174e725653fd11","age",12); log.info("修改........"); } @Test public void updateSet(){ pr.updateSet("534ddfe4aa174e725653fd11","age",12); log.info("修改........"); } @Test public void mapReduce(){ String inputCollectionName = "person"; String mapFunction = "function(){" + "emit({age:this.age},{count:1,name:this.name});" + "};"; String reduceFunction = "function( key , values ){" + " var reduced = {count:0,name:''};" + "values.forEach(function(val) {" + "reduced.count += 1;" + "reduced.name = val.name;" + "});" + "return reduced;" + "};"; MapReduceOptions mapReduceOptions = new MapReduceOptions().outputCollection("ddd"); pr.mapReduce(inputCollectionName, mapFunction,reduceFunction, mapReduceOptions, Person.class); } public static void main(String[] args) { } }
附加:
一下是spring官方提供的使用java代码注解实现: