Spring-data-mongo 설정 예제
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: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">
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" />
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();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.