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;
}
}
