Spring + mongoDB

Database/MongoDB 2012.01.30 17:14 |

  1. Spring Data MongoDB – 1.0.0.M2
  2. Spring Core – 3.0.5.RELEASE
  3. Java Mongo Driver – 2.5.2
  4. STS
  5. JDK – 1.6
  6. Maven – 3.0.3


1. 프로젝트 구조

2012:01:30 17:05:00




2. 의존 라이브러리

 spring-data-mongodb
Currently, the “spring-data-mongodb” jar is only available in “http://maven.springframework.org/milestone“, so, you have to declare this repository also.  




3. Maven  pom.xml 설정

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.spring.mongo</groupId>
  <artifactId>SpringMongoDB</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringMongoDB</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

<repositories>
<repository>
<id>spring-milestone</id>
<name>Spring Maven MILESTONE Repository</name>
<url>http://maven.springframework.org/milestone</url>
</repository>
</repositories>
  <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
 
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
 
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
 
<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.5.2</version>
</dependency>
 
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.0.0.M2</version>
</dependency>
 
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
  </dependencies>
</project>




4.1 Spring 설정 annotation

 SpringMongoConfig.java 

package com.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.config.AbstractMongoConfiguration;

import com.mongodb.Mongo;

/**
 * Spring MongoDB configuration file
 * @author jeonguk.lee
 *
 */
@Configuration
public class SpringMongoConfig extends AbstractMongoConfiguration {
@Override
public @Bean Mongo mongo() throws Exception {
return new Mongo("localhost");
}
    
 
       // Auth 설정으로 서버를 스타트 했으므로...
@Override
public @Bean MongoTemplate mongoTemplate() throws Exception {
MongoTemplate mdb = new MongoTemplate(mongo(), "jeongukdb", "blog");
mdb.setUsername("jk");
mdb.setPassword("1111");
return mdb;
}
}




4.2 Spring 설정  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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
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">

<!-- Default bean name is 'mongo' -->
<mongo:mongo host="localhost" port="27017" />
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="jeongukdb" />
<constructor-arg name="defaultCollectionName" value="blog" />
<property name="username" value="jk" />
<property name="password" value="1111" />
</bean>
<!-- To translate any MongoException throws in @Repository annotated classes -->
<context:annotation-config />
</beans>



5. User Model

package com.spring.user;

public class User {
private String id;
private String firstname;
private String lastname;
private int age;
public User() {
}
public User(String id, String firstname, String lastname, int age) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return " id : " + this.id + " first name : " + this.firstname + " last name : " + this.lastname + " age : " + this.age;
}
}



6. CRUD TEST

MongoTemplate implemented MongoOperations interface. 


App.java 

package com.spring.mongo;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update;

import com.spring.config.SpringMongoConfig;
import com.spring.user.User;


public class App {

public static void main( String[] args ) {
//For Annotation
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
//For XML
//ApplicationContext ctx = new GenericXmlApplicationContext("mongo-config.xml");
MongoOperations mongoOperation =(MongoOperations)ctx.getBean("mongoTemplate");
User user = new User("1001", "Lee", "Jeonguk", 35);
//save
mongoOperation.save("userprofile",user);
//find
User savedUser = mongoOperation.findOne("userprofile", new Query(Criteria.where("id").is("1001")), User.class);
System.out.println("savedUser :" + savedUser);
//update
mongoOperation.updateFirst("userprofile", new Query(Criteria.where("firstname").is("Lee")), Update.update("lastname", "new lastname"));
//find
User updatedUser = mongoOperation.findOne("userprofile", new Query(Criteria.where("id").is("1001")),User.class);
System.out.println("updateUser :" + updatedUser);
//delete
mongoOperation.remove("userprofile", new Query(Criteria.where("id").is("1001")),User.class);
//List
List<User> listUser = mongoOperation.getCollection("userprofile", User.class);
System.out.println("Number of user = " + listUser.size());
    }
}


 
7. 결과

savedUser : id : 1001 first name : Lee last name : Jeonguk age : 35
updateUser : id : 1001 first name : Lee last name : new lastname age : 35
Number of user = 0

 
저작자 표시 비영리
신고
Posted by jeonguk