본문 바로가기
자바 풀스택 공부

Day 66. [Spring] ORM (Object Relational Mapping)

by seung_nari 2022. 4. 8.

기존의 JDBC를 이용하여 퍼시스턴스 계층 구현시 문제
connection, preparedstatement 등등
중복적으로 구현해야하는 소스 코드가 많다.
또한 하나의 sql 문을 만들기 위해
StringBuffet를 사용한 과정이 귀찮다

ORM이란
Object Relational Mapping
객체 - 관계 매핑의 줄임말
OOP에서 쓰이는 객체라는 개념을 구현한 클레스와
RDB에 쓰이는 데이터인 테이블을 자동으로 매핑(연결)

 

JDBCApp.java

package edu.biz.jdbc1;

import java.awt.List;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JDBCApp {
	public static void main(String[] args) throws SQLException {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("jdbc1.xml");
//		ctx.getBean("");
//		DataSource dataSource = ctx.getBean("dataSource", DataSource.class);
//		Connection connection = dataSource.getConnection();
		
		JdbcTemplate template = ctx.getBean("jdbcTemplate", JdbcTemplate.class);
		
		String result = template.queryForObject("SELECT SYSDATE FROM DUAL", String.class);
//		System.out.println(result);
		
//		List<?> list = template.queryForList("SELECT * FROM EMP");
		
		Object obj = template.queryForObject(
				"SELECT EMPNO FROM EMP WHERE ENAME = ? AND EMPNO = ?", new String[]{"SMITH", "7369"}, Object.class);
		System.out.println(obj);
	}
}

 

jdbc1.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<context:property-placeholder location="scott.properties"/>
	<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
		<property name="driverClassName" value="${jdbc.classname}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>		
	</bean>
	
	<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
</beans>

 

scott.properties

jdbc.classname=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521/xe
jdbc.username=scott
jdbc.password=TIGER

댓글