본문 바로가기
Develop/Spring

[2-1등록,수정,삭제,조회 기능의 구현]1.개발전준비-데이터베이스 관련

by jaekk 2018. 8. 18.

1. DataSource 등록


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package org.zerock.controller;
 
import java.sql.Connection;
import java.sql.SQLException;
 
import javax.inject.Inject;
import javax.sql.DataSource;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:/spring/root-context.xml"})
public class DataSourceTest {
 
    @Inject
    DataSource ds;
    
    
    @Test
    public void test() {
        try {
            Connection con = ds.getConnection();
            System.out.println(con);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
}
 
cs


2. DataSource 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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">
    
    <!-- Root Context: defines shared resources visible to all other web components -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
        <property name="url" value="jdbc:log4jdbc:mysql://127.0.0.1:3307/book_ex?useSSl=false"></property>
        <property name="username" value="zerock"></property>
        <property name="password" value="zerock"></property>
    </bean>
    
</beans>
 
cs



3. 개발 패키지 구성

org.zerock.domain: value Object

org.zeorck.service: 서비스 인터페이스와 구현 클래스 패키지

org.zerock.persistence: MyBatis의 DAO 패키지

org.zeock.controller: 스프링 MVC의 컨트롤러 패키지 

resource/mappers폴더: MyBatis의 XML Mapper 위치


4. 테이블의 생성작업


1
2
3
4
5
6
7
8
9
create table book_ex.tbl_board(
    bno INT NOT NULL AUTO_INCREMENT,
    title VARCHAR(200NOT NULL,
    content TEXT NULL,
    writer VARCHAR(50NOT NULL,
    regdate TIMESTAMP NOT NULL DEFAULT now(),
    viewcnt INT DEFAULT 0,
    PRIMARY KEY (bno)
);
cs


5. 테스트 데이터 등록





















※ 본 게시글은 코드로 배우는 스프링 웹 프로젝트 책을 참고하여 작성하였습니다.



댓글