본문 바로가기

Development/Web & Server

[Spring Framework] MongoDB연동하기~!

자세한 사항은 여기를 참고하세요~!


1. 라이브러리 추가~!


<dependency>

<groupId>org.mongodb</groupId>

<artifactId>mongo-java-driver</artifactId>

<version>2.11.0</version>

</dependency>

 

<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-mongodb</artifactId>

<version>1.2.0.RELEASE</version>


2. 설정파일 설정


<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">

 

<mongo:mongo host="127.0.0.1" port="27017" />

<mongo:db-factory dbname="WKAuthDB" />

 

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" autowire="byName">

<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

</bean>

 

</beans>


3. 모델 만들기


import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
 
@Document(collection = "users")
public class User {
 
	@Id
	private String id;
 
	String username;
 
	String password;
 
	//getter, setter, toString, Constructors
 
}

4. 사용하기~!

mongoTemplate을 사용하여 위의 모델을 넣고 빼고 하면된다.


@Inject private MongoTemplate mongoTemplate;


User user = new User("mkyong", "password123");   // save mongoTemplate.save(user);




'Development > Web & Server' 카테고리의 다른 글

[Play Framework] MongoJack 연동하기~!  (0) 2013.12.03
[Play Framework] JPA라이브러리가 보이지 않을때!!!  (0) 2013.12.02
[MongoDB] 기초 사용법 정리  (0) 2013.11.27
MariaDB 설치하기~!  (0) 2013.11.26
MariaDB란..  (0) 2013.11.26