๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Spring/Spring Quick Start

๋น„์ฆˆ๋‹ˆ์Šค ์ปดํฌ๋„ŒํŠธ ์‹ค์Šต

Spring Quick Start(์ฑ…)์˜ Day1 class06~07 ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๊ธฐ๋กํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

์‹ค์Šต ์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


MySQL๋กœ db๋ฅผ ๋ฐ”๊พธ๋ฉด์„œ ๊ธฐ์กด ์ฑ…์˜ ์‹ค์Šต์—์„œ ๋ณ€๊ฒฝํ•œ ๋ถ€๋ถ„

  1. dependency

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>

     

  2. table ์ƒ์„ฑ

    CREATE TABLE BOARD (
    SEQ INTEGER(5) PRIMARY KEY,
    TITLE VARCHAR(200),
    WRITER VARCHAR(20),
    CONTENT VARCHAR(2000),
    REGDATE datetime default CURRENT_TIMESTAMP,
    CNT INTEGER(5) default 0
    );
  3. SQL๋ฌธ

    private final String BOARD_INSERT1 = "set @mymax := (select max(seq) from board)";
    private final String BOARD_INSERT2 =
            "insert into board (seq, title, writer, content) values (ifnull(@mymax, 0)+1,?,?,?)";
    private final String BOARD_UPDATE = "update board set title=?, content=? where seq=?";
    private final String BOARD_DELETE = "delete from board where seq=?";
    private final String BOARD_GET = "select * from board where seq=?";
    private final String BOARD_LIST = "select * from board order by seq desc";

6.1 BoardService ์ปดํฌ๋„ŒํŠธ์˜ Class Diagram

6.2 Value Object ํด๋ž˜์Šค ์ž‘์„ฑ

๋ฐ์ดํ„ฐ ์ „๋‹ฌ์„ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด. ๊ฐ€๋ฐฉ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์•„์ฃผ ์‰ฝ๋‹ค.

๋‚ด ๊ฒฝ์šฐ์—๋Š” DTO(data transfer object)๊ฐ€ ํ›จ์”ฌ ์ต์ˆ™ํ•œ๋ฐ, VO๋กœ ์‹ค์Šตํ–ˆ๋‹ค.

public class BoardVO {
    private int seq;
    private String title;
    private String writer;
    private String content;
    private Date regDate;
    private int cnt;

    // getter, setter ๋ฉ”์†Œ๋“œ ์ƒ๋žต
    // toString ๋ฉ”์†Œ๋“œ ์ƒ๋žต
}

6.3 DAO ํด๋ž˜์Šค ์ž‘์„ฑ

Connection ํš๋“๊ณผ ํ•ด์ œ ์ž‘์—…์ด๋‹ค.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBCUtil {
    public static Connection getConnection() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver"); // ์ด ๋ถ€๋ถ„๋„ mysql์„ ์œ„ํ•ด ๋ณ€ํ™˜ํ–ˆ๋‹ค.
            return DriverManager.getConnection("jdbc:mysql://localhost:3306/springquickstart" +
+ "?serverTimezone=Asia/Seoul&useSSL=false&allowPublicKeyRetrieval=true", "jueun", "jueun");
                    // ์˜ค๋ฅ˜๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด ๊ธธ์–ด์กŒ๋‹ค.
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // close ๋ฉ”์„œ๋“œ ์ƒ๋žต
}

CRUD ๊ธฐ๋Šฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š” BoardDAO ํด๋ž˜์Šค์ด๋‹ค.

ํ•ต์‹ฌ์€ ์ตœ์ƒ๋‹จ์— ์ ์€ sql๋ฌธ์ด๋‹ค.

์ „๋ฐ˜์ ์ธ ๋Š๋‚Œ์„ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ์ด ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ, ๋Œ€ํ‘œ์ ์ธ ์ฝ”๋“œ๋งŒ ์จ๋‘๋ ค ํ•œ๋‹ค.

@Repository("boardDAO")
public class BoardDAO {

        // JDBC ๊ด€๋ จ ๋ณ€์ˆ˜
        private Connection conn = null;
        private PreparedStatement stmt = null;
        private ResultSet rs = null;

        // ์ƒ๋žต

        // ๊ธ€ ๋ชฉ๋ก ์กฐํšŒ
        public List<BoardVO> getBoardList(BoardVO vo) {
                System.out.println("===> JDBC๋กœ getBoardList() ๊ธฐ๋Šฅ ์ฒ˜๋ฆฌ");
                List<BoardVO> boardList = new ArrayList<BoardVO>();
                try {
                        conn = JDBCUtil.getConnection();
                        stmt = conn.prepareStatement(BOARD_LIST);
                        rs = stmt.executeQuery();
                        while (rs.next()) {
                            BoardVO board = new BoardVO();
                            board.setSeq(rs.getInt("SEQ"));
                            board.setTitle(rs.getString("TITLE"));
                            board.setWriter(rs.getString("WRITER"));
                            board.setContent(rs.getString("CONTENT"));
                            board.setRegDate(rs.getDate("REGDATE"));
                            board.setCnt(rs.getInt("CNT"));
                            boardList.add(board);
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        JDBCUtil.close(rs, stmt, conn);
                }
                return boardList;
        }
}

6.4 Service ์ธํ„ฐํŽ˜์ด์Šค ์ž‘์„ฑ

package org.jueun.biz.board;

import java.util.List;

public interface BoardService {
    // CRUD ๊ธฐ๋Šฅ์˜ ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„
    // ๊ธ€ ๋“ฑ๋ก
    void insertBoard(BoardVO vo);
    // ๊ธ€ ์ˆ˜์ •
    void updateBoard(BoardVO vo);
    // ๊ธ€ ์‚ญ์ œ
    void deleteBoard(BoardVO vo);
    // ๊ธ€ ์ƒ์„ธ ์กฐํšŒ
    BoardVO getBoard(BoardVO vo);
    // ๊ธ€ ๋ชฉ๋ก ์กฐํšŒ
    List<BoardVO> getBoardList(BoardVO vo);
}

6.5 Service ๊ตฌํ˜„ ํด๋ž˜์Šค ์ž‘์„ฑ

๊ฐ์ฒด ์ƒ์„ฑ์„ ์œ„ํ•œ @Service

BoardDAO ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ์˜์กด์„ฑ ์ฃผ์ž…ํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜ ์œ„์— @Autowired๋ฅผ ์„ค์ •ํ–ˆ๋‹ค.

import java.util.List;
import org.jueun.biz.board.BoardService;
import org.jueun.biz.board.BoardVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("boardService")
public class BoardServiceImpl implements BoardService {
    @Autowired
    private BoardDAO boardDAO;

    public void insertBoard(BoardVO vo) {
        boardDAO.insertBoard(vo);
    }

    public void updateBoard(BoardVO vo) {
        boardDAO.updateBoard(vo);
    }

    public void deleteBoard(BoardVO vo) {
        boardDAO.deleteBoard(vo);
    }

    public BoardVO getBoard(BoardVO vo) {
        return boardDAO.getBoard(vo);
    }

    public List<BoardVO> getBoardList(BoardVO vo) {
        return boardDAO.getBoardList(vo);
    }
}

6.6 BoardService ์‹คํ–‰

BoardService ์ปดํฌ๋„ŒํŠธ๋ฅผ ์Šคํ”„๋ง ๊ธฐ๋ฐ˜์œผ๋กœ ์‹คํ–‰ํ•˜๋ ค๋ฉด ์Šคํ”„๋ง ์„ค์ • ํŒŒ์ผ์ธ applicationContext.xml ํŒŒ์ผ์— context:component-scan์„ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์˜ ๋ฒ”์œ„๋ฅผ 'org.jueun.biz' ํŒจํ‚ค์ง€๋กœ ์ง€์ •ํ•ด๋†“์•˜์œผ๋‹ˆ, BoardServiceImpl ํด๋ž˜์Šค์™€ BoardDAO ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ์˜์กด์„ฑ ์ฃผ์ž…๋„ ์ ์ ˆํ•˜๊ฒŒ ์ฒ˜๋ฆฌ๋  ๊ฒƒ์ด๋‹ค.

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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.2.xsd">

    <context:component-scan base-package="org.jueun.biz">
    </context:component-scan>
</beans>

eclipse์˜ ๊ฒฝ์šฐ sts๋กœ ์‰ฝ๊ฒŒ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๊ฐ€ ์ถ”๊ฐ€๊ฐ€ ๊ฐ€๋Šฅํ•œ๋ฐ, ์ธํ…”๋ฆฌ์ œ์ด์—์„œ๋Š” ๋” ์‰ฝ๊ฒŒ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ƒฅ <com ์ •๋„๋งŒ ์“ฐ๋ฉด ์ถ”์ฒœ์„ ํ•ด์ฃผ๊ณ , ํ™”์‚ดํ‘œ ๋ฐ‘์œผ๋กœ ์ด๋™ํ•ด์„œ ์—”ํ„ฐ๋ฅผ ๋ˆ„๋ฅด๋ฉด ์Šคํ‚ค๋งˆ๊ฐ€ ์ถ”๊ฐ€๊ฐ€ ๋œ๋‹ค.

 


์ง์ ‘ ์‹คํ–‰ํ•œ ํ™”๋ฉด

 

 


7.1 UserService ์ปดํฌ๋„ŒํŠธ์˜ Class Diagram

 

 

7.2 Value Object ํด๋ž˜์Šค ์ž‘์„ฑ

7.3 DAO ํด๋ž˜์Šค ์ž‘์„ฑ

7.4 Service ์ธํ„ฐํŽ˜์ด์Šค ์ž‘์„ฑ

7.5 Service ๊ตฌํ˜„ ํด๋ž˜์Šค ์ž‘์„ฑ

์•ž๊ณผ ํ๋ฆ„์ด ๊ฐ™์œผ๋ฏ€๋กœ ๊ณผ๊ฐํžˆ ์ƒ๋žต

์ถ”๊ฐ€๋œ ํด๋ž˜์Šค

7.6 UserService ์‹คํ–‰

setter ์ธ์ ์…˜ ์„ค์ •์œผ๋กœ ์‹คํ–‰ํ•˜๊ธฐ

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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.2.xsd">

    <context:component-scan base-package="org.jueun.biz"/>
    
    <bean id="userService" class="org.jueun.biz.user.impl.UserServiceImpl">
        <property name="userDAO" ref="userDao" />
    </bean>
    
    <bean id="userDao" class="org.jueun.biz.user.impl.UserDAO" />
</beans>

UserDAO ๊ฐ์ฒด๋ฅผ ์˜์กด์„ฑ ์ฃผ์ž…ํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ • ์ถ”๊ฐ€

7.7 ์–ด๋…ธํ…Œ์ด์…˜ ์ ์šฉ

์–ด๋…ธํ…Œ์ด์…˜ ์ ์šฉ์œผ๋กœ ๋ฐ”๊ฟ”์„œ ์‹คํ–‰ํ•˜๊ธฐ

UserDAO ํด๋ž˜์Šค ์œ„์— @Repository("userDAO");

UserServiceImpl ํด๋ž˜์Šค ์œ„์— @Service("userService");

UserServiceImpl ํด๋ž˜์Šค ์•ˆ private UserDAO userDAO ์œ„์— @Autowired;


์ง์ ‘ ์‹คํ–‰ํ•œ ํ™”๋ฉด