xml配置(mongo集群方式):
<?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.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.xsd"> <!--credentials的配置形式是:用户名:密码@默认数据库--> <!-- credentials="${mongo.username}:${mongo.password}@${mongo.dbname}" --> <mongo:mongo-client id="mongoClient" replica-set="${mongo.replica.set.address}"> <mongo:client-options connections-per-host="${mongo.connections_per_host}" threads-allowed-to-block-for-connection-multiplier="${mongo.threads_allowed_to_block_for_connection_multiplier}" connect-timeout="${mongo.connect_timeout}" max-wait-time="${mongo.max_wait_time}" socket-timeout="${mongo.socket_timeout}" /> </mongo:mongo-client> <mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" mongo-ref="mongoClient" /> <!--首先列一下WriteConcern的几种抛出异常的级别参数: WriteConcern.NONE:没有异常抛出 WriteConcern.NORMAL:仅抛出网络错误异常,没有服务器错误异常 WriteConcern.SAFE:抛出网络错误异常、服务器错误异常;并等待服务器完成写操作。 WriteConcern.MAJORITY: 抛出网络错误异常、服务器错误异常;并等待一个主服务器完成写操作。 WriteConcern.FSYNC_SAFE: 抛出网络错误异常、服务器错误异常;写操作等待服务器将数据刷新到磁盘。 WriteConcern.JOURNAL_SAFE:抛出网络错误异常、服务器错误异常;写操作等待服务器提交到磁盘的日志文件。 WriteConcern.REPLICAS_SAFE:抛出网络错误异常、服务器错误异常;等待至少2台服务器完成写操作。 --> <mongo:template id="mongoTemplate" db-factory-ref="mongoDbFactory" write-concern="MAJORITY" /> </beans>
mongo.peoperties:
mongo.replica.set.address=192.168.10.145:60000,192.168.10.146:60000,192.168.10.147:60000 mongo.dbname=boshidun mongo.connections_per_host=100 mongo.threads_allowed_to_block_for_connection_multiplier=10 mongo.connect_timeout=10000 mongo.max_wait_time=120000 mongo.socket_timeout=0
BaseRepository
import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; public abstract class BaseRepository<T> { private Class<T> entityClass; @SuppressWarnings("unchecked") public BaseRepository() { Type genType = getClass().getGenericSuperclass(); Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); entityClass = (Class<T>) params[0]; } public void insert(T entity) { this.getMongoTemplate().insert(entity); } public void update(Query query, Update update) { this.getMongoTemplate().findAndModify(query, update, entityClass.getClass()); } public long count(Query query) { return this.getMongoTemplate().count(query, entityClass.getClass()); } @SuppressWarnings("unchecked") public T findById(String id) { Query query = new Query(); query.addCriteria(new Criteria("_id").is(id)); return (T) this.getMongoTemplate().findOne(query, entityClass.getClass()); } protected abstract MongoTemplate getMongoTemplate(); }
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; 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 org.springframework.stereotype.Repository; @Repository public class UserRepository extends BaseRepository<User>{ @Autowired private MongoTemplate mongoTemplate; public List<User> findList(int skip, int limit) { Query query = new Query(); query.with(new Sort(new Order(Direction.ASC, "id"))); query.skip(skip).limit(limit); return this.mongoTemplate.find(query, User.class); } public List<User> findListByApplyId(String applyId) { Query query = new Query(); query.addCriteria(new Criteria("apply_id").is(applyId)); return this.mongoTemplate.find(query, User.class); } @Override protected MongoTemplate getMongoTemplate() { return mongoTemplate; } }