pom.xml
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.194</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
application-properties.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>application-properties</comment>
<!-- Database -->
<entry key="database.driverClassName">org.h2.Driver</entry>
<entry key="database.url">jdbc:h2:~/slipp</entry>
<entry key="database.username">sa</entry>
<entry key="database.password"></entry>
</properties>
applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
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-4.3.xsd">
<context:property-placeholder location="classpath*:application-properties.xml"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
</beans>
ApplicationContextTest
package net.slipp.support;
import static org.junit.Assert.assertNotNull;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/applicationContext.xml")
public class ApplicationContextTest {
@Autowired
private DataSource dataSource;
@Test
public void dataSource(){
assertNotNull(dataSource);
}
}
SQL
DROP TABLE IF EXISTS USERS;
CREATE TABLE USERS (
userId varchar(12) NOT NULL,
password varchar(12) NOT NULL,
name varchar(20) NOT null,
email varchar(50),
primary key (userId)
);
INSERT INTO USERS VALUES('javajigi', 'password', '자바지기', 'javajigi@slipp.net');
댓글 ( 4)
댓글 남기기