详解Spring整合MongoDB
先列举几个网站:操作MongoDB库的语句
主要参考也就是这两个网站。
这个官方教程网站,由于都是英文的,我跟大多数不喜欢英文的人一样,都看不下去。但是确实从这个网站学到的是最全面和专业的。
先看一下Spring对MongoDB的配置文件mongo.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.1.xsd "> <context:component-scan base-package="com.cher7.mongo" /> <!--replicaSetMongo 为配置多个mongos或 配置多个relicasetmongo,但是所配置的必须为同一类型--> <mongo:mongo id="replicaSetMongo" replica-set="${mongodb.host}"> <!-- 每个IP的连接数--> <mongo:options connections-per-host="100" threads-allowed-to-block-for-connection-multiplier="50" auto-connect-retry="true"/> </mongo:mongo> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongo" ref="replicaSetMongo" /> <!--mongo的数据库名称--> <constructor-arg name="databaseName" value="${mongodb.dataname}" /> </bean> <beans profile="test"> <context:property-placeholder location="classpath:config/mongodb.properties"/> </beans> </beans>
mongodb.properties代码为:
#mongodb.host=192.168.65.4:10000,192.168.65.5:10000 #此处均为mongos mongodb.dataname=cher7
很多同学用Spring封装的MongoDB包时都会对多个MongoDB的负载均衡疑惑,Spring到底是如何做负载均衡呢?由于本文中心不是解析此问题,只做稍微解释:Spring启动了多个线程,每个线程的工作为ping一下要连接的Mongo库,取连接时间最短的Mongo为客户端提供服务。有兴趣的同学可以自己查看一下源码,此处不再详述。
继续上述。mongo.xml文件中对外提供了mongoTemplate对象,该对象即可对Mongo库进行增删改查各项操作。
本人对此作了小小封装:
package com.cher7.mongo; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Order; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import com.cher7.util.Reflections; /** * @ClassName: BaseMongoManager * @Description: * @author zc * @date 2014-5-8 下午3:50:22 * */ public class BaseMongoManager<E> { /** 日志对象 */ protected static final Logger logger = Logger .getLogger(BaseMongoManager.class); @Autowired private MongoTemplate mongoTemplate; public void setMongoTemplate(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } /** * Entity的类型 */ protected Class<E> entityClass; public BaseMongoManager() { this.entityClass = Reflections.getSuperClassGenricType(getClass()); } /** * * @Title: findAll * @Description: 查询表格所有的数据 * @return List<E> 当前实体对应的表格的所有数据。 */ public List<E> findAll() { return this.findAll(null); } /** * @Title: findAll * @Description: 查询表格所有的数据 * @param clazz * 该表所对应的实体 * @param collcetionName * 表格名称 * @return List<E> */ public List<E> findAll(String collcetionName) { if (StringUtils.isBlank(collcetionName)) { collcetionName = mongoTemplate.getCollectionName(Reflections .getSuperClassGenricType(getClass())); if (StringUtils.isBlank(collcetionName)) { collcetionName = this.entityClass.getSimpleName().toLowerCase(); } logger.info("findAll's param collcetionName is null,so it default is " + collcetionName); } return mongoTemplate.findAll(entityClass, collcetionName); } /** * * @Title: findCount * @Description:查询当前实体对应表格的所有数据的条数 * @return long 总条数 * @throws */ public long findCount() { long count = 0; count = mongoTemplate.count(new Query(), entityClass); return count; } /*** * * @Title: insert * @Description: 入库 * @param e * 实体数据 * @param collectionName * 表名 */ public void insert(E e, String collectionName) { mongoTemplate.insert(e, collectionName); } /** * * @Title: insert * @Description: 入库 * @param e * 实体数据 */ public void insert(E e) { mongoTemplate.insert(e); } /** * 生产查询语句 * * @Title: createCriteria * @Description: 根据不同条件生产SQL * @param gtMap * @param ltMap * @param eqMap * @param gteMap * @param lteMap * @param regexMap * @param inMap * @param neMap * @return Criteria 查询的语句 * @throws */ @SuppressWarnings("rawtypes") public Criteria createCriteria(Map<String, Object> gtMap, Map<String, Object> ltMap, Map<String, Object> eqMap, Map<String, Object> gteMap, Map<String, Object> lteMap, Map<String, String> regexMap, Map<String, Collection> inMap, Map<String, Object> neMap) { Criteria c = new Criteria(); List<Criteria> listC= new ArrayList<Criteria>(); Set<String> _set = null; if (gtMap != null && gtMap.size() > 0) { _set = gtMap.keySet(); for (String _s : _set) { listC.add( Criteria.where(_s).gt(gtMap.get(_s))); } } if (ltMap != null && ltMap.size() > 0) { _set = ltMap.keySet(); for (String _s : _set) { listC.add( Criteria.where(_s).lt(ltMap.get(_s))); } } if (eqMap != null && eqMap.size() > 0) { _set = eqMap.keySet(); for (String _s : _set) { listC.add( Criteria.where(_s).is(eqMap.get(_s))); } } if (gteMap != null && gteMap.size() > 0) { _set = gteMap.keySet(); for (String _s : _set) { listC.add( Criteria.where(_s).gte(gteMap.get(_s))); } } if (lteMap != null && lteMap.size() > 0) { _set = lteMap.keySet(); for (String _s : _set) { listC.add( Criteria.where(_s).lte(lteMap.get(_s))); } } if (regexMap != null && regexMap.size() > 0) { _set = regexMap.keySet(); for (String _s : _set) { listC.add( Criteria.where(_s).regex(regexMap.get(_s))); } } if (inMap != null && inMap.size() > 0) { _set = inMap.keySet(); for (String _s : _set) { listC.add( Criteria.where(_s).in(inMap.get(_s))); } } if (neMap != null && neMap.size() > 0) { _set = neMap.keySet(); for (String _s : _set) { listC.add( Criteria.where(_s).ne(neMap.get(_s))); } } if(listC.size() > 0){ Criteria [] cs = new Criteria[listC.size()]; c.andOperator(listC.toArray(cs)); } return c; } public Criteria createCriteria(Map<String, Object> eqMap) { return this.createCriteria(null, null, eqMap, null, null, null, null, null); } public Criteria createCriteria(Map<String, Object> eqMap, Map<String, Object> neMap) { return this.createCriteria(null, null, eqMap, null, null, null, null, neMap); } /** * * @Title: findCount * @Description: 根据各种条件查询总数 * @param gtMap * @param ltMap * @param eqMap * @param gteMap * @param lteMap * @param regexMap * @param inMap * @param neMap * @return long 总数 * @throws */ @SuppressWarnings("rawtypes") public long findCount(Map<String, Object> gtMap, Map<String, Object> ltMap, Map<String, Object> eqMap, Map<String, Object> gteMap, Map<String, Object> lteMap, Map<String, String> regexMap, Map<String, Collection> inMap, Map<String, Object> neMap) { long count = 0; Criteria c = this.createCriteria(gtMap, ltMap, eqMap, gteMap, lteMap, regexMap, inMap, neMap); Query query = null; if (c == null) { query = new Query(); } else { query = new Query(c); } count = mongoTemplate.count(query, entityClass); return count; } /*** * * @Title: findCount * @Description: 根据创建的条件查询总数 * @param queryC * @return long 返回类型 * @throws */ public long findCount(Criteria queryC){ Query query = new Query(queryC); return mongoTemplate.count(query, entityClass); } /** * * @Title: findCount * @Description: 根据多个种条件 or 的方式查询 * @param orList * or的查询条件的集合 * @return long * @throws */ public long findCount(Criteria... orList) { long count = 0; Criteria c = new Criteria(); Query query = null; if (orList != null && orList.length > 0) { c.orOperator(orList); } query = new Query(c); count = mongoTemplate.count(query, entityClass); return count; } @SuppressWarnings("rawtypes") public long findCount(Map<String, Object> gtMap, Map<String, Object> ltMap, Map<String, Object> eqMap, Map<String, String> regexMap, Map<String, Collection> inMap) { return this.findCount(gtMap, ltMap, eqMap, null, null, regexMap, inMap, null); } public long findCountByContainRegex(Map<String, Object> gtMap, Map<String, Object> ltMap, Map<String, Object> eqMap, Map<String, String> regexMap) { return this.findCount(gtMap, ltMap, eqMap, regexMap, null); } /** * * @Title: findListByPage * @Description: 根据分页+条件获取对应的实体集合 * @param eqMap * @param gtMap * @param ltMap * @param gteMap * @param lteMap * @param regexMap * @param inMap * @param orders * 排序集合 * @param pageIndex * 页码 * @param pageSize * 每页条数 * @return List<E> 实体集合 * @throws */ @SuppressWarnings("rawtypes") public List<E> findListByPage(Map<String, Object> eqMap, Map<String, Object> gtMap, Map<String, Object> ltMap, Map<String, Object> gteMap, Map<String, Object> lteMap, Map<String, String> regexMap, Map<String, Collection> inMap, Map<String, Object> neMap, List<Order> orders, int pageIndex, int pageSize) { List<E> list = null; Criteria c = this.createCriteria(gtMap, ltMap, eqMap, gteMap, lteMap, regexMap, inMap, neMap); Sort sort = null; if (orders != null && orders.size() > 0) { sort = new Sort(orders); } Query query = null; if (c == null) { query = new Query(); } else { query = new Query(c); } if (sort != null) { query = query.with(sort); } if (pageSize > 0) { query.skip((pageIndex - 1) * pageSize); query.limit(pageSize); } list = mongoTemplate.find(query, entityClass); return list; } /** * @Title: findOneObject * @Description: 符合条件的某一条数据 * @param eqMap * @param gtMap * @param ltMap * @param gteMap * @param lteMap * @param regexMap * @param inMap * @return E 返回该数据对应的实体 */ @SuppressWarnings("rawtypes") public E findObject(Map<String, Object> eqMap, Map<String, Object> gtMap, Map<String, Object> ltMap, Map<String, Object> gteMap, Map<String, Object> lteMap, Map<String, String> regexMap, Map<String, Collection> inMap) { E e = null; List<E> list = this.findList(eqMap, gtMap, ltMap, gteMap, lteMap, regexMap, inMap, null, null); if (list != null && list.size() > 0) { e = list.get(0); } return e; } /** * * @Title: findList * @Description: 多个查询条件or方式组合查询 * @param orList * @return List<E> * @throws */ public List<E> findList(Criteria... orList) { return this.findListByPage(null, 0, 0, orList); } /** * * @Title: findListByOrder * @Description: 多个查询条件or方式组合查询 * @param orList * @param orders * @return List<E> * @throws */ public List<E> findListByOrder(List<Order> orders, Criteria... orList) { return this.findListByPage(orders, 0, 0, orList); } /** * * @Title: findListByPage * @Description: 根据查询条件直接查询 * @param c * @param orders * @param pageIndex * @param pageSize * @return List<E> * @throws */ public List<E> findListByPage(Criteria c, List<Order> orders, int pageIndex, int pageSize){ Query query = new Query(c); Sort sort = null; if (orders != null && orders.size() > 0) { sort = new Sort(orders); } if (sort != null) { query = query.with(sort); } if (pageSize > 0) { query.skip((pageIndex - 1) * pageSize); query.limit(pageSize); } return mongoTemplate.find(query, entityClass); } public List<E> findListByOrder(Criteria c, List<Order> orders){ return this.findListByPage(c, orders, 0, 0); } public List<E> findList(Criteria c){ return this.findListByPage(c, null, 0, 0); } /** * * @Title: findObject * @Description: 根据查询条件查询某一个object * @param c * @return E * @throws */ public E findObject(Criteria c){ List<E> list = this.findList(c); if(list!=null && list.size()>0){ return list.get(0); } return null; } /** * * @Title: findListByPage * @Description: 多个查询条件or方式组合查询 * @param orList * or的查询条件的集合 * @param orders排序规则 * @param pageIndex * 第几页 * @param pageSize每页多少条 * @return List<E> 符合条件的集合 * @throws */ public List<E> findListByPage(List<Order> orders, int pageIndex, int pageSize, Criteria... orList) { List<E> list = null; Criteria c = new Criteria(); Query query = null; if (orList != null && orList.length > 0) { c.orOperator(orList); } query = new Query(c); Sort sort = null; if (orders != null && orders.size() > 0) { sort = new Sort(orders); query = query.with(sort); } if (pageSize > 0) { query.skip((pageIndex - 1) * pageSize); query.limit(pageSize); } list = mongoTemplate.find(query, entityClass); return list; } @SuppressWarnings("rawtypes") public List<E> findListNotContainOrder(Map<String, Object> eqMap, Map<String, Object> gtMap, Map<String, Object> ltMap, Map<String, Object> gteMap, Map<String, Object> lteMap, Map<String, String> regexMap, Map<String, Collection> inMap, Map<String, Object> neMap) { return this.findList(eqMap, gtMap, ltMap, gteMap, lteMap, regexMap, inMap, neMap, null); } }来自:http://www.cher7.com/?id=1244
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!