在使用spring-data-mongodb中,需要配置spring文件,如下:
mongodb.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"> <!-- 加载mongodb的属性配置文件 --> <context:property-placeholder location="classpath:config/mongodb.properties" /> <!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 --> <mongo:mongo id="mongo" replica-set="${mongo.hostport}"> <!-- 一些连接属性的设置 --> <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> <mongo:db-factory dbname="database" mongo-ref="mongo" /> <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials"> <constructor-arg name="username" value="${mongo.username}" /> <constructor-arg name="password" value="${mongo.psw}" /> </bean> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg name="databaseName" value="test" /> <constructor-arg ref="userCredentials" /> </bean> </beans>
通过上面的配置,我们通过获取注入Template对mongo数据库操作。
这其中有些需要认识的类:
1. Mongo.class
这个类中需要知道在xml配置文件中使用的构造方法:
public Mongo( String host , MongoOptions options ) throws UnknownHostException { this( new ServerAddress( host ) , options ); }
2. MongoOptions.class
此类中配置一些Mongo对象的一些属性
public class MongoOptions { public MongoOptions(){ reset(); } public MongoOptions(final MongoClientOptions options) { connectionsPerHost = options.getConnectionsPerHost(); threadsAllowedToBlockForConnectionMultiplier = options.getThreadsAllowedToBlockForConnectionMultiplier(); maxWaitTime = options.getMaxWaitTime(); connectTimeout = options.getConnectTimeout(); socketTimeout = options.getSocketTimeout(); socketKeepAlive = options.isSocketKeepAlive(); autoConnectRetry = options.isAutoConnectRetry(); maxAutoConnectRetryTime = options.getMaxAutoConnectRetryTime(); readPreference = options.getReadPreference(); dbDecoderFactory = options.getDbDecoderFactory(); dbEncoderFactory = options.getDbEncoderFactory(); socketFactory = options.getSocketFactory(); description = options.getDescription(); cursorFinalizerEnabled = options.isCursorFinalizerEnabled(); writeConcern = options.getWriteConcern(); slaveOk = false; // default to false, as readPreference field will be responsible } // other codes ... }
3. UserCredentials.class
此类被用来提供账号密码的验证类
package org.springframework.data.authentication; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; /** * Class used to provide credentials for username/password authentication * * @author Thomas Risberg * @author Oliver Gierke */ public class UserCredentials { public static final UserCredentials NO_CREDENTIALS = new UserCredentials(null, null); private final String username; private final String password; /** * Creates a new {@link UserCredentials} instance from the given username and password. Empty {@link String}s provided * will be treated like no username or password set. * * @param username * @param password */ public UserCredentials(String username, String password) { this.username = StringUtils.hasText(username) ? username : null; this.password = StringUtils.hasText(password) ? password : null; } // other codes ... }
4. MongoTemplate.class
如下是使用它的构造方法:
/** * Constructor used for a template configuration with user credentials in the form of * {@link org.springframework.data.authentication.UserCredentials} * * @param mongo must not be {@literal null}. * @param databaseName must not be {@literal null} or empty. * @param userCredentials */ public MongoTemplate(Mongo mongo, String databaseName, UserCredentials userCredentials) { this(new SimpleMongoDbFactory(mongo, databaseName, userCredentials)); }
使用mongo主要利用其中的Template对数据库操作。