본 블로깅의 목적은 카산드라에 (2011/11/10 - [NoSQL/cassandra] - Injecting dependencies Cassandra higher-level client Hector for Spring framework.)이어서 Springframework(이하 스프링) 에서 몽고디비를 사용하기위한 스프링에서 제공하는 http://www.springsource.org/spring-data/mongodb 라이브러리에서 xml 설정을 이용한 org.springframework.data.mongodb.core.MongoTemplate 객체 생성방법과 DAO단에서 @Autowired로 불러와 바로 사용하도록하는 방법을 알아보기 위함이다.
본문에 앞서 필요한 라이브러리가 몇가지가 있다.
1) Spring Core – 3.0.5.RELEASE (어노테이션을 통한 객체주입을 하려면 3. 이후 버젼 부터 사용해야한다.) http://www.springsource.org/node/2880
2) Spring Data MongoDB (mongoTemplate 라이브러리 ) http://www.springsource.org/spring-data/mongodb
3) 그리고 마지막으로 중요한 Spring Data Commons http://www.springsource.org/spring-data/commons 이 라이브러리를 추가하지 않으면 객체주입이 안되서 xml에서 자동적으로 객체생성이 불가능해진다.
4) MongoDB Driver http://www.mongodb.org/display/DOCS/Java+Language+Center#JavaLanguageCenter-JavaDriver 자바 클라이언트 라이브러리
spring3-jdbc.xml 설정시 반드시 추가해야할 파서
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="...(생략)
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd" >
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> </bean> <mongo:mongo host="${mongodb.host}" port="${mongodb.port}"></mongo:mongo> <mongo:db-factory dbname="${mongodb.dbname}" username="${mongodb.username}" password="${mongodb.password}" mongo-ref="mongo" /> <!-- org.springframework.data.mongodb.core.MongoTemplate -->
<context:annotation-config /> <context:component-scan base-package="com.application.controller" /> <context:component-scan base-package="com.application.filter" />
<context:component-scan base-package="com.application.dao.mongodb" />
</beans>
저렇게 mongoTemplate를 등록한후 사용될 객체를 spring pool에 등록을한다.
package com.application.dao.mongodb; import org.springframework.beans.factory.annotation.Autowired; 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; import org.springframework.stereotype.Repository; import com.mongodb.WriteResult; @Repository public class UsersMongodbDAOImpl implements UsersMongodbDAO{ @Autowired MongoTemplate mongoTemplate; @Override public Users insert(String account, String name, int age) { // Users users = new Users(); users.setAccount(account); users.setName(name); users.setAge(age); mongoTemplate.insert(users); return users; } @Override public Users save(String account, String name, int age) { // Users users = new Users(); users.setAccount(account); users.setName(name); users.setAge(age); mongoTemplate.save(users); return users; } @Override public String update(String account, String name, int age) { // WriteResult wr = mongoTemplate.updateMulti(Query.query((Criteria.where("account").is(account))), Update.update("age", "asdf"), Users.class); return wr.getLastError().toString(); } @Override public Users get(String account) { // return mongoTemplate.findById(account, Users.class); } }
'NoSQL > MongoDB' 카테고리의 다른 글
insert와 save의 차이(?) 오류(?) 버그(?) (0) | 2012.06.13 |
---|---|
Mongdb DB 보안셋팅 (0) | 2012.05.14 |
[spring-data/mongodb] 클라이언트 콘솔에서 DBRef로 등록된 객체를 통해 find()하는 방법. (1) | 2011.12.26 |