반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 평일코딩
- 코딩테스트
- typescript
- javascript
- 정보처리기사요약
- CSS
- 국비IT
- Java의정석
- 스프링
- 자바스크립트 코딩테스트
- 정보처리기사실기
- 이안의평일코딩
- 자스코테
- 정보처리기사
- 정보처리기사실기정리
- php
- 정보처리기사실기요약
- 오라클
- 국비코딩
- VUE
- 자바의정석
- ReactNative
- 자바스크립트
- 리액트네이티브
- Oracle
- spring
- react
- 리액트
- 타입스크립트
- 정보처리기사정리
Archives
- Today
- Total
이안의 평일코딩
국비 mongoDB - Spring 연동 본문
반응형
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;
}
}
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