mongodb集成spring
1:首先需要下载mongodb的java驱动包
https://github.com/mongodb/mongo-java-driver/downloads
2:需要下载spring集成mongodb的包
下载途径:
a:去csdn下
b:去http://projects.spring.io/spring-data-mongodb/ 使用Maven方式下载
c:网上其他地方找
要下载的包如下
spring-data-commons-core-1.2.0.M2.jar
spring-data-mongodb-1.0.0.M4.jar
spring-data-mongodb-cross-store-1.0.0.M4.jar
spring-data-mongodb-log4j-1.0.0.M4.jar
为什么选择很老的1.0.0版本,是因为我的spring框架是3.0.5的,不知道是不是spring版本低了的原因,选择高点的如1.4.0反正会报错
3:ok 包包都⑦了,开始编码了,先说说配置文件
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <!-- spring配置文件目录-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring/*.xml</param-value>
- </context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- </web-app>
application-global.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:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/data/mongo
- http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
- <!-- 获取配置文件属性 -->
- <bean id="placeholderConfig"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
- <value>classpath:db.properties</value>
- </property>
- </bean>
- <!-- Default bean name is 'mongo' -->
- <!-- 连接池配置详细查看http://api.mongodb.org/java/2.7.2/com/mongodb/MongoOptions.html#connectionsPerHost
- 或者http://www.cnblogs.com/huangfox/archive/2012/04/01/2428947.html
- -->
- <mongo:mongo host="${datasource.host}" port="${datasource.port}">
- <mongo:options connections-per-host="10"
- threads-allowed-to-block-for-connection-multiplier="10"
- auto-connect-retry="true" />
- </mongo:mongo>
- <!-- Spring提供的mongodb操作模板-->
- <bean id="mongoTemplate"
- class="org.springframework.data.mongodb.core.MongoTemplate">
- <constructor-arg ref="mongo" />
- <constructor-arg name="databaseName" value="${datasource.dbname}" />
- </bean>
</beans>
- <strong><span style="color:#ff0000;">db.properties</span></strong>
#datasource.host=localhost
datasource.host=12.35.174.48
#datasource.port=27017
datasource.port=9933
datasource.dbname=logDB
datasource.username=test
datasource.passwd=test
4:spring的db操作模板MongoTemplate
具体使用参考百度或者官网http://docs.spring.io/spring-data/data-mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoTemplate.html#getDb()
程序启动时,注入MongoTemplate模板
- package dao.impl;
- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- import org.springframework.data.mongodb.core.MongoTemplate;
- import com.mongodb.DB;
- public class AbstractBaseMongoTemplete implements ApplicationContextAware {
- protected MongoTemplate mongoTemplate;
- protected DB db;
- /**
- * 设置mongoTemplate
- * @param mongoTemplate the mongoTemplate to set
- */
- public void setMongoTemplate(MongoTemplate mongoTemplate) {
- this.mongoTemplate = mongoTemplate;
- }
- public void setApplicationContext(ApplicationContext applicationContext)
- throws BeansException {
- //System.out.println("ApplicationContextAware开始执行setApplicationContext方法");
- MongoTemplate mongoTemplate = applicationContext.getBean("mongoTemplate", MongoTemplate.class);
- setMongoTemplate(mongoTemplate);
- db=mongoTemplate.getDb();
- }
- }
5:具体操作类接口
- public interface IDbDao {
- /**
- * 插入单个
- * @param obj
- * @return
- */
- public abstract DBObject insert(DBObject obj);
- /**
- * 插入json格式字符串
- * @param jsonStr
- * @return
- */
- public abstract WriteResult insert(String jsonStr);
- /**
- * 批量插入
- * @param list
- */
- public abstract void insertBatch(List<DBObject> list);
- /**
- * 插入json文件
- * @param list
- */
- public abstract void insertFile(String fileName);
- /**
- * 删除单个
- * @param obj
- */
- public abstract void delete(DBObject obj);
- /**
- * 批量删除
- * @param list
- */
- public abstract void deleteBatch(List<DBObject> list);
- /**
- * 修改
- * @param searchFields 要修改的查询条件
- * @param updateFields 修改的值
- */
- public abstract void update(DBObject searchFields,DBObject updateFields);
- /**
- * 按条件查询
- * @return
- */
- public abstract List<BasicDBObject> find();
- /**
- * 按条件查询
- * @param query 查询条件
- * @return
- */
- public abstract List<BasicDBObject> find(DBObject query);
- /**
- * 排序查询
- * @param query
- * @param sort new BasicDBObject("age", true) 则对age字段降序 注意1是升序,-1是降序
- * @param _class 要返回的实体bean类型
- * @return
- */
- public abstract List<BasicDBObject> find(DBObject query,DBObject sort);
- /**
- * 分页查询
- * @param query 查询条件
- * @param start 起始
- * @param limit 每页多少数据
- * @return
- */
- public abstract List<BasicDBObject> find(DBObject query,int start,int limit);
- /**
- * 分页查询
- * @param query 查询条件
- * @param sort 排序
- * @param start 起始
- * @param limit 每页多少数据
- * @return
- */
- public abstract List<BasicDBObject> find(DBObject query,DBObject sort,int start,int limit);
- /**
- * 按条件查询
- * @param _class 要返回的实体bean类型
- * @return
- */
- public abstract List<Object> find(Class _class);
- /**
- * 按条件查询
- * @param query 查询条件
- * @param _class 要返回的实体bean类型
- * @return
- */
- public abstract List<Object> find(DBObject query,Class _class);
- /**
- * 排序查询
- * @param query 查询条件
- * @param sort new BasicDBObject("age", true) 则对age字段降序 注意1是升序,-1是降序
- * @param _class 要返回的实体bean类型
- * @return
- */
- public abstract List<Object> find(DBObject query,DBObject sort,Class _class);
- /**
- * 分页查询
- * @param query 查询条件
- * @param start 起始
- * @param limit 每页多少数据
- * @param _class 要返回的实体bean类型
- * @return
- */
- public abstract List<Object> find(DBObject query,int start,int limit,Class _class);
- /**
- * 分页查询
- * @param query 查询条件
- * @param sort 排序
- * @param start 起始
- * @param limit 每页多少数据
- * @param _class 要返回的实体bean类型
- * @return
- */
- public abstract List<Object> find(DBObject query,DBObject sort,int start,int limit,Class _class);
- /**
- * 按条件查询,返回查询个数
- * @param query 查询条件
- * @return
- */
- public abstract int findCount(DBObject query);
- /**
- * 排序查询,返回查询个数
- * @param query
- * @param sort new BasicDBObject("age", true) 则对age字段降序 注意1是升序,-1是降序
- * @param _class 要返回的实体bean类型
- * @return
- */
- public abstract int findCount(DBObject query,DBObject sort);
- /**
- * 获取总数
- * @param obj
- * @return
- */
- public abstract long getCount(DBObject obj);
- /**
- * 获取总数
- * @param obj
- * @return
- */
- public abstract long getCount();
- /**
- * 利用java驱动自带函数分组查询
- * @param key 用来分组文档的字段
- * @param cond 执行过滤的条件
- * @param initial reduce中使用变量的初始化
- * @param reduce reduce(参数:当前文档和累加器文档.)
- * @param fn
- * @return
- */
- public abstract DBObject group(DBObject key,DBObject cond,DBObject initial,String reduce,String fn );
- /**
- * 使用聚合框架(参考http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/)
- * @param firstDb
- * @param optionDb
- * @return
- */
- public abstract AggregationOutput aggregation(DBObject firstDb,DBObject ...optionDb);
- public abstract DBCollection getCollection();
- /**
- * 创建索引
- * @param obj new BasicDBObject("name", 1)); // 1代表升序 -1代表降序
- */