이안의 평일코딩

국비 mongoDB - Spring 연동 본문

Back-end/mongoDB

국비 mongoDB - Spring 연동

이안92 2020. 11. 18. 19:04
반응형

2020.11.18(수)

C:\Users\SIST>cd "c:\Program Files"

c:\Program Files>cd MongoDB

c:\Program Files\MongoDB>cd Server

c:\Program Files\MongoDB\Server>cd 4.4

c:\Program Files\MongoDB\Server\4.4>cd bin

c:\Program Files\MongoDB\Server\4.4\bin>mongod --dbpath c:\data

 

SpringSubqueryProject

src/main/java

com.sist.temp

MongoDAO

package com.sist.temp;

import org.springframework.stereotype.Repository;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.sist.dao.EmpVO;

@Repository
public class MongoDAO {
    private MongoClient mc; //Connection
    private DB db; //데이터베이스 (XE)
    private DBCollection dbc; //테이블
    public MongoDAO(){
        try{
            mc=new MongoClient("localhost",27017);
            db=mc.getDB("mydb");
            dbc=db.getCollection("emp"); //create table emp
        }catch(Exception ex){}
    }
    /* json형식
     * {},
     * {}
     */
    public void empInsert(EmpVO vo){
        BasicDBObject obj=new BasicDBObject();
        obj.put("empno", vo.getEmpno());
        obj.put("emame", vo.getEname());
        obj.put("job", vo.getJob());
        obj.put("hiredate", vo.getHiredate().toString());
        obj.put("dname", vo.getDname());
        obj.put("loc", vo.getLoc());
        dbc.insert(obj);
    }
}

 

MainClass

package com.sist.temp;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.sist.dao.*;

@Component
public class MainClass {
    @Autowired
    private EmpDAO dao;
    @Autowired
    private MongoDAO mdao;
    public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("application-context.xml");
        MainClass mc=(MainClass)app.getBean("mainClass"); //getbean ui찾는것, MainClass의 앞자 소문자로
        List<EmpVO> list=mc.dao.empListData();
        for(EmpVO vo:list){
            mc.mdao.empInsert(vo);
        }
        System.out.println("완료!!");
    }
}

 

com.sist.dao

EmpMapper

package com.sist.dao;

import org.apache.ibatis.annotations.Select;

import java.util.*;

public interface EmpMapper {
    
    @Select("SELECT empno,ename,job,hiredate,sal,e.deptno,"
            +"(SELECT dname FROM dept d WHERE e.deptno=d.deptno) as dname,"
            +"(SELECT loc FROM dept d WHERE e.deptno=d.deptno) as loc "
            +"FROM emp e")
    public List<EmpVO> empListData();
    
    @Select("SELECT empno,ename,job,hiredate,sal,deptno, "
            +"(SELECT dname FROM dept d WHERE e.deptno=d.deptno) as dname,"
            +"(SELECT loc FROM dept d WHERE e.deptno=d.deptno) as loc "
            +"FROM emp e "
            +"WHERE deptno=(SELECT deptno FROM emp WHERE ename=#{ename})")
    public List<EmpVO> empGroupData(String ename);
    
}

 

src/main/java에 application-context.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    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-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
    <context:component-scan base-package="com.sist.dao"/>
    <context:component-scan base-package="com.sist.temp"/>
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"
        p:driverClassName="oracle.jdbc.driver.OracleDriver"
        p:url="jdbc:oracle:thin:@localhost:1521:XE"
        p:username="hr"
        p:password="happy"
    />
     <bean id="ssf"
         class="org.mybatis.spring.SqlSessionFactoryBean"
         p:dataSource-ref="ds"
     />
     <bean id="mapper"
         class="org.mybatis.spring.mapper.MapperFactoryBean"
         p:mapperInterface="com.sist.dao.EmpMapper"
         p:sqlSessionFactory-ref="ssf"
     />
</beans>

 

MainClass.java에서 Run As - Java Application으로 실행하면 EmpMapper.java 쿼리가

 

mongoDB에 들어오게된다~!

 


mongoDB는 임시로 거쳐서 비정형화 데이터를 저장해서 오라클에 집어넣어줌

(오라클은 필터링해야되기 때문에 정형화된 데이터만 넣을 수 있음)

 

find (sql의 SELECT문)

 

MongoDAO

package com.sist.temp;

import org.springframework.stereotype.Repository;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.sist.dao.EmpVO;
import java.util.*;

@Repository
public class MongoDAO {
    private MongoClient mc; //Connection
    private DB db; //데이터베이스 (XE)
    private DBCollection dbc; //테이블
    public MongoDAO(){
        try{
            mc=new MongoClient("localhost",27017);
            db=mc.getDB("mydb");
            dbc=db.getCollection("emp"); //create table emp
        }catch(Exception ex){}
    }
    /* json형식
     * {},
     * {}
     */
    public void empInsert(EmpVO vo){
        BasicDBObject obj=new BasicDBObject();
        obj.put("empno", vo.getEmpno());
        obj.put("ename", vo.getEname());
        obj.put("job", vo.getJob());
        obj.put("hiredate", vo.getHiredate().toString());
        obj.put("dname", vo.getDname());
        obj.put("loc", vo.getLoc());
        dbc.insert(obj);
    }
    /*
     * 0
     * 1
     * 
     * 10
     * 11
     * 12
     * 
     * 20
     */
    public List<EmpVO> empListData(int page){
        List<EmpVO> list=new ArrayList<EmpVO>();
        int rowSize=10;
        int skip=(rowSize*page)-rowSize;
        DBCursor c=dbc.find().skip(skip).limit(rowSize);
        // find => select
        while(c.hasNext()){
            /*
             {
                "_id" : ObjectId("5fb4ead67c437949a3501680"),
                "empno" : 7369,
                "emame" : "SMITH",
                "job" : "CLERK",
                "hiredate" : "Wed Dec 17 00:00:00 KST 1980",
                "dname" : "RESEARCH",
                "loc" : "DALLAS"
            }
            
            BasicDBObject가 한줄씩 가져옴
             */
            BasicDBObject obj=(BasicDBObject)c.next();
            EmpVO vo=new EmpVO();
            vo.setEmpno(obj.getInt("empno"));
            vo.setEname(obj.getString("ename"));
            vo.setJob(obj.getString("job"));
            vo.setDname(obj.getString("dname"));
            vo.setLoc(obj.getString("loc"));
            list.add(vo);
        }
        c.close();
        return list;
    }
}

 

ename값 안들어갔나..?

MainClass

package com.sist.temp;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.sist.dao.*;

@Component
public class MainClass {
    @Autowired
    private EmpDAO dao;
    @Autowired
    private MongoDAO mdao;
    public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("application-context.xml");
        MainClass mc=(MainClass)app.getBean("mainClass"); //getbean ui찾는것, MainClass의 앞자 소문자로
        /*List<EmpVO> list=mc.dao.empListData();
        for(EmpVO vo:list){
            mc.mdao.empInsert(vo);
        }
        System.out.println("완료!!");*/
        List<EmpVO> list=mc.mdao.empListData(1);
        for(EmpVO vo:list){
            System.out.println(vo.getEmpno()+" "+vo.getEname()+" "
                    +vo.getJob()+" "+vo.getDname()+" "+vo.getLoc());
        }
    }
}
반응형

'Back-end > mongoDB' 카테고리의 다른 글

[몽고DB] 다른 아이피에서 접속하기  (2) 2021.02.10
Comments