Spring-data-mongo 설정 예제

참고:http://docs.spring.io/spring-data/data-mongo/docs/1.8.0.M1/reference/html/#mongo.mongo-xml-config
1.applicationContext-mongodb.properties
\#만약,mongodb 에 클 러 스 터 url 간 설정 이 있 으 면 사용 할 수 있 습 니 다","분리
mongo.url=192.168.1.156:27017
mongo.dbname=autocomplete
mongo.username=easy
mongo.password=easy
mongo.connectTimeout=3000
mongo.maxWaitTime=60000
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.socketTimeout=0
mongo.slaveOk=true
\#연결 풀 연결 수
mongo.connectionsPerHost=20
2.applicationContext-mongodb.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd"
default-lazy-init="true">

threads-allowed-to-block-for-connection-multiplier="15"
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-ref="mongo" username="${mongo.username}" password="${mongo.password}" />






3.테스트 사례
package cn.com.easy.mongodb.entry;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
 *       
 * 
 * @author nibili 2015 6 19 
 * 
 */
@Document
public class Word {
public static final String ID_FIELD = "id";
public static final String TEXT_FIELD = "text";
public static final String USECOUNT_FIELD = "useCount";
public static final String RESULTCOUNT_FIELD = "resultCount";
@Id
private String id;
/**    */
private String text;
/**    */
private String pinyin;
/**         */
private String initials;
/**      */
private Integer useCount;
/**       */
private Integer resultCount;
/**
 * get id
 * 
 * @return
 * @auth nibili 2015 6 19 
 */
public String getId() {
return id;
}
/**
 * set id
 * 
 * @param id
 * @auth nibili 2015 6 19 
 */
public void setId(String id) {
this.id = id;
}
/**
 * get   
 * 
 * @return
 * @auth nibili 2015 6 19 
 */
public String getText() {
return text;
}
/**
 * set   
 * 
 * @param text
 * @auth nibili 2015 6 19 
 */
public void setText(String text) {
this.text = text;
}
/**
 * get   
 * 
 * @return
 * @auth nibili 2015 6 19 
 */
public String getPinyin() {
return pinyin;
}
/**
 * set   
 * 
 * @param pinyin
 * @auth nibili 2015 6 19 
 */
public void setPinyin(String pinyin) {
this.pinyin = pinyin;
}
/**
 * get        
 * 
 * @return
 * @auth nibili 2015 6 19 
 */
public String getInitials() {
return initials;
}
/**
 * set        
 * 
 * @param initials
 * @auth nibili 2015 6 19 
 */
public void setInitials(String initials) {
this.initials = initials;
}
/**
 * get     
 * 
 * @return
 * @auth nibili 2015 6 19 
 */
public Integer getUseCount() {
return useCount;
}
/**
 * set     
 * 
 * @param useCount
 * @auth nibili 2015 6 19 
 */
public void setUseCount(Integer useCount) {
this.useCount = useCount;
}
/**
 * get      
 * 
 * @return
 * @auth nibili 2015 6 19 
 */
public Integer getResultCount() {
return resultCount;
}
/**
 * set      
 * 
 * @param resultCount
 * @auth nibili 2015 6 19 
 */
public void setResultCount(Integer resultCount) {
this.resultCount = resultCount;
}
}
import static org.springframework.data.mongodb.core.query.Criteria.where;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.com.easy.mongodb.entry.Word;
import cn.com.easy.utils.FastJSONUtils;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext-mongodb-demo.xml")
public class MongoTest {
private static final Logger log = LoggerFactory.getLogger(MongoTest.class);
@Autowired
private MongoTemplate mongoTemplate;
/**
 * mongo  
 * 
 * @auth nibili 2015 6 19 
 */
@Test
public void insert() {
try {
Word word = new Word();
word.setInitials("sj");
word.setPinyin("shouji");
word.setResultCount(10);
word.setText("  ");
word.setUseCount(100);
//
mongoTemplate.insert(word);
//
word = mongoTemplate.findOne(new Query(where(Word.TEXT_FIELD).is("  ")), Word.class);
log.info("  :" + FastJSONUtils.toJsonString(word));
List<Word> list = mongoTemplate.findAll(Word.class);
log.info("  List:" + FastJSONUtils.toJsonString(list));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

좋은 웹페이지 즐겨찾기