โป src/main/java ํด๋์ xyz.itwill08.dao ํจํค์ง ์์ฑ
๐pom.xml
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc11 --> <!-- โ JDBC ๊ธฐ๋ฅ์ ์ ๊ณตํ๊ธฐ ์ํ OracleDriver ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ --> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc11</artifactId> <version>21.9.0.0</version> </dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <!-- โ Spring DAO ๊ด๋ จ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ - Spring DataSource ๊ด๋ จ ํด๋์ค ํฌํจ --> <!-- โ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์กด๊ด๊ณ์ ์ํด spring-tx ๋ผ์ด๋ธ๋ฌ๋ฆฌ(ํธ๋ ์ ์ ๊ด๋ฆฌ)๋ ๋น๋ ์ฒ๋ฆฌ --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework-version}</version> </dependency>
๐DataSourceApp.java
โป xyz.itwill08.dao ํจํค์ง์ DataSourceApp.java ํด๋์ค ์์ฑ
package xyz.itwill08.dao; // import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; // //DataSource ๊ฐ์ฒด : ๋ค์์ Connection ๊ฐ์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์์ฑํ์ฌ ์ ์ฅํ๊ธฐ ์ํ ๊ฐ์ฒด - DBCP(DataBase Connection Pool) //โ Spring Bean Configuration File์์ DataSource ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์ ์์ํด๋์ค๋ฅผ Spring Bean ๋ฑ๋กํ์ฌ ์ฌ์ฉ //DataSource ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์ ์์ํด๋์ค๋ Spring Framework์์ ์ ๊ณตํ๋ spring-jdbc ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋น๋ ์ฒ๋ฆฌํ์ฌ ์ฌ์ฉ - ๋ฉ์ด๋ธ : pom.xml //โ DataSource ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ์ Oracle Driver ๊ด๋ จ ojdbc ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ํ๋ก์ ํธ์ ๋น๋ ์ฒ๋ฆฌ public class DataSourceApp { public static void main(String[] args) throws SQLException { ApplicationContext context=new ClassPathXmlApplicationContext("08_dao.xml"); DataSource dataSource=context.getBean("dataSource", DataSource.class); System.out.println("================================================================"); System.out.println("dataSource = "+dataSource); Connection connection=dataSource.getConnection(); System.out.println("connection = "+connection); connection.close(); System.out.println("================================================================"); ((ClassPathXmlApplicationContext)context).close(); } }
๐08_dao.xml
โป src/main/resources ํด๋์ 08_dao.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- ================================================================================ --> <!-- Spring Framework์ spring-jdbc ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ DriverManagerDataSource ํด๋์ค๋ฅผ Spring Bean์ผ๋ก ๋ฑ๋ก --> <!-- โ DBCP(DataBase Connection Pool) ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ DataSource ๊ฐ์ฒด ์์ฑ --> <!-- โ DataSource ๊ฐ์ฒด ํ๋์ Connection ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ธฐ ์ํ ๊ฐ์ ์ ๋ฌํ์ฌ ์ ์ฅ - Setter Injection --> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/> <property name="username" value="scott"/> <property name="password" value="tiger"/> </bean> </beans>