티스토리 뷰
pom.xml
변경 부분
<properties>
<java-version>1.8</java-version>
<org.springframework-version>4.3.8.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<!-- Test --> 다음에 추가 부분
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
</dependencies>
<build>
src/test/java → com.itwillbs.sts → DbConTest.java 파일 추가 (class파일)
package com.itwillbs.sts;
import java.sql.Connection;
import java.sql.DriverManager;
import org.junit.Test;
public class DbConTest {
// 드라이버 연결
// 5버전 : com.mysql.jdbc.Driver
// 5버전 이후 : com.mysql.cj.jdbc.Driver
private static final String DRIVER = "com.mysql.jdbc.Driver";
// 디비 주소
// 5버전 : jdbc:mysql://127.0.0.1:3306/springdb?useSSL=false
// 5버전 이후 : jdbc:mysql://127.0.0.1:3306/springdb?useSSL=false&serverTimezone=Asia/Seoul
private static final String URL = "jdbc:mysql://127.0.0.1:3306/springdb?useSSL=false";
// 워크벤치 아이디 연결
private static final String ID = "jspid";
// 워크벤치 비밀번호 연결
private static final String PW = "jsppass";
// 연결이 됫는지 안 됫는지 알려줌
// @Test : 사용하는데 적용이 안되면 pom.xml에서 바꿔줘야 한다.
@Test
public void testConnection() throws Exception {
// 기존 try catch 안에 있었지만 throws Exception 적용해주어서 안 함
Class.forName(DRIVER);
// Connection con = DriverManager.getConnection(URL, ID, PW);
// try-with (1.7 이후부터 사용 가능) : finally 구문을 사용하지 않고 자동으로 close() 호출
// try(AutoClosable 인터페이스를 구현한 타입의 변수) {
//
// } catch (Exception e) {
//
// }
try(Connection con = DriverManager.getConnection(URL, ID, PW)) {
// 연결 정보 출력
System.out.println("디비 연결 정보 : " + con);
} catch (Exception e) {
e.printStackTrace();
}
// MyBatis(iBatis) : SQL 처리하는데 효율적으로 사용 가능하게 함
// 스프링 웹프로젝트 구성
// - Presentation Layer : UI 구성요소
// -> JSP(view), Controller 구성됨
// - Business (Logic) Layer : 서비스 계층
// - Data Access Layer : 데이터 처리(Persistence Layer)
// -> DAO, Mybatis
// jsp
// html <- controller <- Service <- DAO <- Mybatis <- DB
// css
// js(javascript)
// Mybatis : SQL Mapper 라이브러리, try~ catch ~finally 없이 쿼리 사용, pstmt, rs
// xml 파일, 어노테이션을 사용해서 sql 쿼리문을 사용 가능
// Mabatis-Spring
// pom.xml 수정
}
}
root-context.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- bean 태그 : 자바 bean처럼 생각하면 된다. 객체를 bean으로 사용. -->
<bean id="datasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!-- 버전 5 -->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/springdb?useSSL=false"></property>
<!-- 버전 5 이상 -->
<!-- <property name="url" value="jdbc:mysql://127.0.0.1:3306/springdb?useSSL=false&serverTimezone=Asia/Seoul"></property> -->
<property name="username" value="jspid"></property>
<property name="password" value="jsppass"></property>
</bean>
</beans>
src/test/java → com.itwillbs.sts → DataSourceTest.java 파일 추가 (class파일)
package com.itwillbs.sts;
import java.sql.Connection;
import javax.inject.Inject;
import javax.sql.DataSource;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//@RunWith, @ContextConfiguration
//-> 테스트 파일을 실행할 때 스프링로드되어서 실행시키기위한 어노테이션
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
locations = {"file:src/main/webapp/WEB-INF/spring/**/root-context.xml"}
)
public class DataSourceTest {
// @Inject : DataSource 객체는 스프링이 생성해서 주입해줌
@Inject
DataSource ds;
// => private DataSource ds2 = new DataSource();
public void testCon() throws Exception {
try(Connection con = ds.getConnection()) {
System.out.println("DataSource 사용 연결 정보 con : " + con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
시작하기
매핑된 SQL 구문 살펴보기
<?xml version="1.0" encoding="UTF-8" ?>
xml 문서 표시
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
dtd 데이터 타입 결정
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
mapper 매핑을 통해서 select 쿼리에 담는다.
우리가 필요한건 dtd인데
mapper 가 아니라 configuration
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
configuration 태그가 자동으로 생겨난다.
root-context.xml 추가
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:/mybatis-config.xml"></property>
</bean>
src/test/java → com.itwillbs.sts → MybatisTest.java 파일 추가 (class파일)
'부산 ITWILL 학원 실습 > Spring' 카테고리의 다른 글
[Spring] 스프링 특징 (0) | 2018.10.22 |
---|---|
[Spring] 설치 & 적용 (0) | 2018.10.22 |