본문으로 바로가기

Toby의 Spring3 따라하기-제4편 AnnotationContext를 이용한 Spring 사용





최종적인 파일들의 모습은 왼쪽과 같다.



중요한 것은 src/main/resources에 있는 applicationContext.xml이다.


아래 이전 포스트 에서

2015/09/08 - [배우는 것/Spring] - Toby의 Spring3 따라하기-제3편 connectionMaker interface 이용해서 분리하기


만들었던 Interface인 ConnectionMaker와 Interface의 구현 Class인 RConnectionMaker를 applicationContext.xml과 ApplicationContext를 이용해 대체하는 것이다.


Spring은 ApplicationContext라고 볼 수 있다.



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

<bean id="userDao" class="UserDao">
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/krkdb"/>
<property name="username" value="root" />
<property name="password" value="123456"/>
</bean>

</beans>


위 xml에서 ConnectionMaker를 dataSource로 대체 해주었다. DataSource는 Spring-jdbc Library를 import해야 한다.





UserDao.java

import org.springframework.jdbc.datasource.SimpleDriverDataSource;


import java.sql.*;

/**
* Created by me on 2015-04-18.
*/
public class UserDao {

private SimpleDriverDataSource dataSource;

public void setDataSource(SimpleDriverDataSource dataSource) {
this.dataSource = dataSource;
}

public User select(String id) throws ClassNotFoundException, SQLException {

Connection c = dataSource.getConnection();

PreparedStatement ps;
ResultSet rs;

ps = c.prepareStatement("select * from users where id=?");
ps.setString(1, id);

rs = ps.executeQuery();

rs.next();

User user = new User();
user.setId(rs.getString("id"));
user.setPassword(rs.getString("password"));
user.setName(rs.getString("name"));

rs.close();
ps.close();
c.close();

return user;

}

public int insert(User user) throws ClassNotFoundException, SQLException {

Connection c = dataSource.getConnection();
PreparedStatement ps;

ps = c.prepareStatement("insert into users (id, password, name) VALUES (?, ?, ?)");
ps.setString(1, user.getId());
ps.setString(2, user.getPassword());
ps.setString(3, user.getName());

int updated_rows = ps.executeUpdate();

ps.close();
c.close();

return updated_rows;
}

public void deleteAll() throws SQLException {
Connection c = dataSource.getConnection();
PreparedStatement ps;

ps = c.prepareStatement("delete from users");
ps.executeUpdate();

ps.close();
c.close();
}

public int countAll() throws SQLException {

Connection c = dataSource.getConnection();
PreparedStatement ps;
ResultSet rs;

ps = c.prepareStatement("select count(*) from users");
rs = ps.executeQuery();

rs.next();

int count = rs.getInt(1);

rs.close();
ps.close();
c.close();

return count;


}


}



UserDaoTest.java

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;

import static org.junit.Assert.assertEquals;

/**
* Created by me on 2015-04-18.
*/
public class UserDaoTest{
ApplicationContext context;
UserDao dao;

@Before
public void setup(){
this.context =
new ClassPathXmlApplicationContext("applicationContext.xml");
this.dao = context.getBean("userDao", UserDao.class);
}

@Test
public void test() throws SQLException, ClassNotFoundException {

dao.deleteAll();

assertEquals(0, dao.countAll());


User user1 = new User();
user1.setId("hello");
user1.setPassword("1234");
user1.setName("jkkim");

dao.insert(user1);

assertEquals(1, dao.countAll());

User user = dao.select("hello");

assertEquals(user1.getId(), user.getId());

}

}



end.





댓글을 달아 주세요