SpringJDBC란 ?
Native JDBC(JAVA 표준 API)를 효율적으로 사용하기 위해 스프링 프레임워크에서 제공하는 기능 중 하나이다.
pom.xml 라이브러리 추가
web.xml 추가
<!-- Spring configuration start -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 루트 어플리케이션컨텍스트 영역 -->
<!-- 시작점을 의미 한다 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/context*.xml</param-value>
</context-param>
<!-- Spring configuration end -->
xml은 컴파일 할 것이 없기 때문에 클래스 패스로 간다.
context*.xml 파일 src/main/resources 위치에 저장
resources에 넣으면 클래스패스로 가기 때문에 가능하다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
</beans><?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-4.1.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:53306/forum?serverTimezone=Asia/Seoul" />
<property name="username" value="root"/>
<property name="password" value="admin_123"/>
</bean>
</beans>JoinService, JoinDao 작성
package com.portfolio.www.service;
import java.util.HashMap;
import com.portfolio.www.dao.JoinDao;
import at.favre.lib.crypto.bcrypt.BCrypt;
public class JoinService {
private JoinDao joinDao;
public void setJoinDao(JoinDao joinDao) {
this.joinDao = joinDao;
}
public int join(HashMap<String,String> params) {
String passwd = params.get("passwd");
String encPasswd = BCrypt.withDefaults().hashToString(12,passwd.toCharArray());
System.out.println("encPasswd >>>>>>>>> " + encPasswd);
BCrypt.Result result = BCrypt.verifyer().verify(passwd.toCharArray(), encPasswd);
System.out.println("result.verified >>>>>>> " + result.verified);
params.put("passwd", encPasswd);
return joinDao.join(params);
}
}
package com.portfolio.www.dao;
import java.util.HashMap;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
public class JoinDao extends JdbcTemplate {
public JoinDao() {}
public JoinDao(DataSource ds) {
super(ds);
}
public int join(HashMap<String,String> params) {
String sql = "INSERT INTO forum.`member` "
+ "( member_id, passwd, member_nm, email, auth_yn, pwd_chng_dtm, join_dtm) "
+ "VALUES( '" + params.get("memberId")
+ "', '" + params.get("passwd")
+ "', '', '" + params.get("email")
+ "', '', '', DATE_FORMAT(now(),'%Y%m%d%H%i%s'))";
return update(sql);
}
}
Spring Bean 수동 등록 방법
property(프로퍼티)
빈의 멤버 변수에 값을 주입할때 사용되고 이 방법은 기본생성자를 사용해서 빈을 생성 한 후에 프로퍼티 값을 설정할 때 사용한다.
constructor-arg(생성자 인자)
빈의 생성자를 통해 값을 주입할수 있다. 생성자를 호출 할때 사용, 이 방법은 빈을 생성할 때 필수적으로 주입되어야 하는 값이 있을 때 사용한다.
property 와 constructor-arg의 차이점은 값이 주입되는 시점이다. 빈이 생성된 후에 값을 주입하거나 빈이 생성될 때 생성자를 통해 값을 주입하느냐에 따라서 주입되는 값의 필수 여부와 주입 시점에 따라 결정된다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<bean id="joinService" class="com.portfolio.www.service.JoinService" >
<property name="joinDao" ref="joinDao" />
</bean>
<bean id="joinDao" class="com.portfolio.www.dao.JoinDao" >
<constructor-arg ref="dataSource" />
</bean>
</beans>login.jsp 수정
LoginController 수정
@Autowired // 현재 Autowired 한 행위를 스프링에서는 di라고 한다.
private JoinService joinservice;
@RequestMapping("/join.do")
public String join(@RequestParam HashMap<String, String> params) {
System.out.println(params);
return "join";
}
DI : 객체 간의 의존성을 관리하고 객체를 생성하고 조립하는 방법, 현재 bean을 정의 했고 @Autowired를 통해 의존성을 주입했다.
컨트롤러 까지 코드 수정 후 회원 가입 버튼을 누루면 아래와 같은 데이터가 넘어가는 것을 확인 할 수 있다.

jsp → controller → service → dao → db