IT/BigData와 MongoDB

Spring-Data-MongoDB 연동

바바옄 2015. 7. 7. 18:00
반응형

1. MongoDB 사용자 권한 설정하기

 

첫번재 cmd 창 - mongod 접속

 

 

두 번째 cmd창 띄운 후 - mongo 접속

 

 

두 번째 cmd 창에 계정 만들기


# 참고 : 밑에 소스코드 예제는 user : "admin" , pwd : "admin" , db: test 이다 

 

 

 

2. spring project 만들기

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3. MongoDB에 데이터 들어갔는지 확인하기

 


- cmd 창 권한 변경하기


첫번째 cmd창 나갔다가(ctrl + c) -> mongod.exe -auth 입력


- 두번째 cmd창 나갔다가 -> mongo 입력


- use admin   // admin 계정 사용하기

  db.auth("userName","pwd");  // userName: 사용자 , pwd: 비밀번호 

  use test // test : DB 이름

  db.person.find(); // person : collection




소스코드 


Main.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.ktds.dao;
 
import org.apache.catalina.core.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class Main {
    public static void main(String[] args){
        
        String url = "mongoContext.xml";
        
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(url);
        
        MongoDAO dao = ctx.getBean("mongoDAO", MongoDAO.class);
        if(dao == null){
            System.out.println("NULL!!");
        }
        else{
            System.out.println("Not NULL!!");
            
            dao.insertTestVO();
        }    
    }
}
 
 
cs



MongoDAO.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.ktds.dao;
 
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.MongoTemplate;
 
public class MongoDAO {
    
    private MongoTemplate mongoTemplate;
 
    public void setMongoTemplate(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }
    
    public void insertTestVO(){
        TestVO testVO = new TestVO();
        testVO.setName("전성규");
        testVO.setJob("회사원");
        
        // person : collection 명
        mongoTemplate.insert(testVO, "person");
    }
    
    public class TestVO{
 
        @Id
        private String id;
        private String job;
        private String name;
        
        public String getJob() {
            return job;
        }
        public void setJob(String job) {
            this.job = job;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
 
}
cs


pom.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<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>TestForMongo</groupId>
  <artifactId>TestForMongo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
   <dependencies>
       <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>4.1.6.RELEASE</version>
      </dependency>
       <dependency>
           <groupId>org.mongodb</groupId>
           <artifactId>mongo-java-driver</artifactId>
           <version>3.0.2</version>
       </dependency>
       <dependency>
           <groupId>org.springframework.data</groupId>
           <artifactId>spring-data-mongodb</artifactId>
           <version>1.7.0.RELEASE</version>
       </dependency>
       
  </dependencies>
</project>
cs


mongoContext.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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: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/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd">
 
    <mongo:mongo-client
        host="localhost"
        port="27017"
        credentials="admin:admin@admin"><!-- id:pwd @ admin -->
        <mongo:client-options
            connections-per-host="8"
            threads-allowed-to-block-for-connection-multiplier="4"
            connect-timeout="1000"
            max-wait-time="1500"
            socket-keep-alive="true"
            socket-timeout="1500"
        />    
    </mongo:mongo-client>
 
    <bean    id="mongoTemplate"
            class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg ref="mongo"/>
            <constructor-arg name="databaseName" value="test"/>    
    </bean>
    
    <bean    id="mongoDAO"
            class="com.ktds.dao.MongoDAO">
            <property name="mongoTemplate" ref="mongoTemplate"/>    
    </bean>
</beans>
 
cs


반응형

'IT > BigData와 MongoDB' 카테고리의 다른 글

INSERT / UPDATE / DELETE / FIND/ MapReduce  (0) 2015.07.08
MapReduce  (0) 2015.07.07
Count, Distinct, Group, Map Reduce  (0) 2015.07.07
MongoDB 설치  (0) 2015.07.07