일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 기초
- 오라클
- 알고리즘
- 자바
- 필기
- 스프링부트
- 데이터프로그래밍
- SQL
- 토이프로젝트
- 해킹실습
- 프로그래밍
- 프로그래머스
- MySQL
- 파이썬
- 문제풀이
- 빅데이터
- 엘라스틱서치
- SQL 문법
- 스프링
- 이클립스
- 코딩테스트
- 백준
- 데이터베이스
- 문법
- 스파크
- 모의해킹
- c언어
- SQL 정리
- 리눅스마스터 2급 2차
- 위클리챌린지
- Today
- Total
개발일기
[프로젝트 개발] 2. 스프링 - 오라클 연동하기 본문
# JDBC : 자바에서 데이터베이스에 접속할 수 있도록 하는 자바 API. JDBC는 데이터베이스에서 자료를 쿼리하거나 업데이트 하는 방법을 제공한다.
# Mybatis : 자바 퍼시스턴스 프레임워크의 하나로 XML 서술자나 어노테이션을 사용하여 저장 프로시저나 SQL 문으로 객체들을 연결한다.
1. 스프링 마이바티스와 오라클 DB를 연동하기 전에 라이브러리를 추가해야한다.
라이브러리는 프로젝트의 pom.xml 파일에 DBCP 관련 <depencency>를 추가해주어야 한다.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- Oracle -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
2. <dependency> 뿐만 아니라 ojdbc를 설치하기 위해 별도로 <repository> 도 설정해주어야 한다.
<!-- ojdbc 다운받기 위한 옵션 -->
<repositories>
<repository>
<id>oracle</id>
<name>ORACLE JDBC Repository</name>
<url>http://mesir.googlecode.com/svn/trunk/mavenrepo</url>
</repository>
</repositories>
이렇게 repository를 설정 해 주어야 MAVEN repository가 참고해서 ojdbc를 설치한다.
3. Mybatis 설정은 mybatis-config.xml 파일을 생성한 후 작성한다.
경로 : src - main - resources - mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
4. Oracle DB와 연동하기 위해 root-context.xml 파일에 아래와 같이 작성한다.
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property value="oracle.jdbc.driver.OracleDriver" name="driverClassName" />
<property value="jdbc:oracle:thin:@localhost:1521:xe" name="url" />
<!-- 오라클 사용자 이름 -->
<property value="scott" name="username" />
<!-- 오라클 사용자 비밀번호 -->
<property value="tiger" name="password" />
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="SqlSessionFactory">
<property name="dataSource" ref="dataSource" />
<property value="classpath:mybatis-config.xml" name="configLocation" />
<property value="classpath:/sqlmap/**/*sql.xml" name="mapperLocations" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="simple.spring.exa"/>
</bean>
5. 오라클 연동 테스트
@RequestMapping(value="/create", method = RequestMethod.GET)
public ModelAndView create() {
Integer result = svc.selectNow();
System.out.println("오라클 테스트 : " + result);
먼저 사전에 만들었던 컨트롤러에 TestSvc 의 selectNow 함수를 호출하여 print 한다.
6. TestSvc
@Autowired
private TestDao dao;
public Integer selectNow() {
return dao.selectNow();
}
서비스 단에서는 다오의 selectNow() 함수를 호출하여 리턴한다.
7. TestDao
@Repository
public interface TestDao {
Integer selectNow();
}
8. Testsql
테스트 sql 파일은 src/main/resources/sqlmap 에 작성해주어야 한다. 경로를 바꾸고 싶다면 root-context.xml 에서 경로를 바꿔주어야 한다. 아니면 mapper를 인식하지 못한다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "simple.spring.exa.dao.TestDao">
<select id="selectNow" resultType="Integer">
SELECT COUNT(SYSDATE)
FROM EMP
</select>
</mapper>
9. 테스트 완료
쿼리가 접속한 EMP 테이블의 총 데이터 갯수가 출력되는 것을 확인할 수 있다.
'미니 프로젝트' 카테고리의 다른 글
[프로젝트 개발] 1. 책 입력화면 만들기 (2) | 2022.06.04 |
---|---|
[프로젝트 세팅] 2. GIT과 연동하기 (0) | 2022.06.01 |
[프로젝트 세팅] 1. 프로젝트 초기 세팅하기 (1) | 2022.05.29 |
[프로젝트 준비] 3. 필요한 프로그램 설치 (0) | 2022.05.21 |
[프로젝트 준비] 2. Oracle 11g Express Edition 설치 (0) | 2022.05.14 |