본문 바로가기

IT/Spring

Spring JDBC framework 개념 및 예제

 

 스프링 뭐있나 New 안하는거지

 

 

CRUD 완성된 프로젝트의 모습 

 

 

 

Pom (maven 설정) 

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

<modelVersion>4.0.0</modelVersion> 

<groupId>Intern</groupId> 

<artifactId>sample</artifactId> 

<version>0.0.1-SNAPSHOT</version> 

<packaging>war</packaging> 

 

<properties> 

<spring.version>3.2.9.RELEASE</spring.version> 

<junit.version>4.11</junit.version> 

<jdk.version>1.8</jdk.version> 

</properties> 

 

<dependencies> 

<!-- Spring 3 dependencies --> 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-context</artifactId> 

<version>${spring.version}</version> 

</dependency> 

<dependency> 

<groupId>cglib</groupId> 

<artifactId>cglib</artifactId> 

<version>3.0</version> 

</dependency> 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-aspects</artifactId> 

<version>${spring.version}</version> 

</dependency> 

 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-aop</artifactId> 

<version>${spring.version}</version> 

</dependency> 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-jdbc</artifactId> 

<version>${spring.version}</version> 

</dependency> 

<dependency> 

<groupId>mysql</groupId> 

<artifactId>mysql-connector-java</artifactId> 

<version>5.1.9</version> 

</dependency> 

 

</dependencies> 

 

<build> 

<finalName>CRUD</finalName> 

<plugins> 

<plugin> 

<groupId>org.apache.maven.plugins</groupId> 

<artifactId>maven-compiler-plugin</artifactId> 

<version>3.0</version> 

<configuration> 

<source>${jdk.version}</source> 

<target>${jdk.version}</target> 

</configuration> 

</plugin> 

</plugins> 

</build> 

 

</project> 

 

 

 

Resources/ 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" 

 

<bean id="dataSource" 

class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 

<property name="driverClassName" value="com.mysql.jdbc.Driver" /> 

<property name="url" value="jdbc:mysql://localhost:3306/test" /> 

<property name="username" value="root" /> 

<property name="password" value="intern" /> 

</bean> 

 

<bean id="studentJDBCTemplate" class="Hello.StudentJDBCTemplate"> 

<property name="dataSource" ref="dataSource"></property> 

</bean> 

</beans> 

 

 

 

Src/student.java (data설정) 

package Hello; 

 

public class Student { 

private Integer age; 

private String name; 

private Integer id; 

 

public Integer getAge() { 

return age; 

} 

 

public void setAge(Integer age) { 

this.age = age; 

} 

 

public String getName() { 

return name; 

} 

 

public void setName(String name) { 

this.name = name; 

} 

 

public Integer getId() { 

return id; 

} 

 

public void setId(Integer id) { 

this.id = id; 

} 

 

} 

 

 

Src/studentDAO.java  (interface이며, template에서 활용) 

 

package Hello; 

 

import java.util.List; 

import javax.sql.DataSource; 

public interface StudentDAO { 

/** 

 * This is the method to be used to initialize database resources ie. 

 * connection. 

 */ 

public void setDataSource(DataSource ds); 

 

/** 

 * This is the method to be used to create a record in the Student table. 

 */ 

public void create(String name, Integer age); 

 

/** 

 * This is the method to be used to list down a record from the Student 

 * table corresponding to a passed student id. 

 */ 

public Student getStudent(Integer id); 

 

/** 

 * This is the method to be used to list down all the records from the 

 * Student table. 

 */ 

public List<Student> listStudents(); 

 

/** 

 * This is the method to be used to delete a record from the Student table 

 * corresponding to a passed student id. 

 */ 

public void delete(Integer id); 

 

/** 

 * This is the method to be used to update a record into the Student table. 

 */ 

public void update(Integer id, Integer age); 

} 

 

 

 

Src.JDBCTemplate.java 

 

package Hello; 

 

import java.util.List; 

 

import javax.sql.DataSource; 

 

import org.springframework.jdbc.core.JdbcTemplate; 

 

public class StudentJDBCTemplate implements StudentDAO { 

private DataSource dataSource; 

private JdbcTemplate jdbcTemplateObject; 

 

public void setDataSource(DataSource dataSource) { 

this.dataSource = dataSource; 

this.jdbcTemplateObject = new JdbcTemplate(dataSource); 

} 

 

public void create(String name, Integer age) { 

String SQL = "insert into Student (name, age) values (?, ?)"; 

 

jdbcTemplateObject.update(SQL, name, age); 

System.out.println("Created Record Name = " + name + " Age = " + age); 

return; 

} 

 

public Student getStudent(Integer id) { 

String SQL = "select * from Student where id = ?"; 

Student student = jdbcTemplateObject.queryForObject(SQL, 

new Object[] { id }, new StudentMapper()); 

return student; 

} 

 

public List<Student> listStudents() { 

String SQL = "select * from Student"; 

List<Student> students = jdbcTemplateObject.query(SQL, 

new StudentMapper()); 

return students; 

} 

 

public void delete(Integer id) { 

String SQL = "delete from Student where id = ?"; 

jdbcTemplateObject.update(SQL, id); 

System.out.println("Deleted Record with ID = " + id); 

return; 

} 

 

public void update(Integer id, Integer age) { 

String SQL = "update Student set age = ? where id = ?"; 

jdbcTemplateObject.update(SQL, age, id); 

System.out.println("Updated Record with ID = " + id); 

return; 

} 

} 

 

 

 

 

'IT > Spring' 카테고리의 다른 글

Spring Annotation 설명 및 예제  (0) 2015.01.31
Spring MVC 예제  (0) 2015.01.30
Spring에서 excel 사용하기 JXL & POI 개념 및 예제  (2) 2015.01.23
Spring-IOC (DI) 개념 및 예제  (2) 2015.01.21
Spring 이론  (0) 2015.01.20