스프링

* 테이블 스키마 초기화
* UserDao 구현

---

Done 
* 데이터베스 설치
* Connection Pooling 설정
* 테이블 설계

 

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.0.5.RELEASE</version>
		</dependency>

 

class UserDao

 

package net.slipp.dao.users;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.PostConstruct;

import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

import net.slipp.domain.users.User;

public class UserDao extends JdbcDaoSupport {

	
	@PostConstruct
	public void initialize(){
		ResourceDatabasePopulator populator=new ResourceDatabasePopulator();
		populator.addScript(new ClassPathResource("slipp.sql"));
		DatabasePopulatorUtils.execute(populator, getDataSource());
	}

	public User findById(String userId) {
		String sql =" select * from USERS where userId =?";
		
		RowMapper<User> rowMapper =new RowMapper<User>() {
			@Override
			public User mapRow(ResultSet rs, int rowNum) throws SQLException {
				
				return new User(
						rs.getString("userId"),
						rs.getString("password"),
						rs.getString("name"),
						rs.getString("email")
						);
			}
		};
		return getJdbcTemplate().queryForObject(sql, rowMapper, userId);
	}

	
	public void create(User user) {
		String sql =" insert into USERS values(?, ? , ? , ?) ";
		getJdbcTemplate().update(sql, user.getUserId(), user.getPassword(), user.getName(), user.getEmail());
	}
	
	
	
}





 

 

class User

 

package net.slipp.domain.users;

public class User {

	private String userId;
	private String password;
	private String name;
	private String email;
	
	public User() {
	
	}
	public User(String userId, String password, String name, String email) {
		super();
		this.userId = userId;
		this.password = password;
		this.name = name;
		this.email = email;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	@Override
	public String toString() {
		return "User [userId=" + userId + ", password=" + password + ", name=" + name + ", email=" + email + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((userId == null) ? 0 : userId.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (userId == null) {
			if (other.userId != null)
				return false;
		} else if (!userId.equals(other.userId))
			return false;
		return true;
	}
	
	
	
	
}

 

class 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);
	}
	
}

 

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>
	
	<bean id="userDao"  class="net.slipp.dao.users.UserDao">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	
	
</beans>

 

 class 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);
	}
	
}

 

class UserDaoTest

package net.slipp.dao.users;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import net.slipp.domain.users.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/applicationContext.xml")
public class UserDaoTest {

	private static final Logger logger = LoggerFactory.getLogger(UserDaoTest.class);
	
	@Autowired
	private UserDao userDao;
	
	@Test
	public void test() {
		User user =userDao.findById("javajigi");		
		logger.debug("User :   {} ", user.toString());
	}

	@Test
	public void create() throws Exception{
		User user=new User("sangigi", "password", "산지기", "sanjigi@gmail.com");
		userDao.create(user);
		User actual=userDao.findById(user.getUserId());	
		logger.debug(" create User : {} " , actual);	
		assertThat(actual, is(user));
	}
	

	
}

 

23:15 DEBUG net.slipp.dao.users.UserDaoTest -  create User : User [userId=sangigi, password=password,

name=산지기, email=sanjigi@gmail.com] 
 

23:15 DEBUG net.slipp.dao.users.UserDaoTest - User :   User [userId=javajigi, password=password, name=자

바지기, email=javajigi@slipp.net] 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

탕약에 감초 빠질까 , 어떤 일에나 빠짐없이 끼어드는 사람을 두고 빗대는 말.

댓글 ( 4)

댓글 남기기

작성