Spring Quick Start(์ฑ )์ Day1 class06~07 ์ค์ต์ ์งํํ๋ฉด์ ๊ธฐ๋กํ ๋ด์ฉ์ ๋๋ค.
์ค์ต ์ฝ๋๋ ์ฌ๊ธฐ์ ํ์ธํ ์ ์์ต๋๋ค.
MySQL๋ก db๋ฅผ ๋ฐ๊พธ๋ฉด์ ๊ธฐ์กด ์ฑ ์ ์ค์ต์์ ๋ณ๊ฒฝํ ๋ถ๋ถ
-
dependency
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency>
-
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 );
-
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;
์ง์ ์คํํ ํ๋ฉด
'Spring > Spring Quick Start' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
AOP๋? (0) | 2021.01.07 |
---|---|
[์๋ฌ] "useSSL" ์ํฐํฐ์ ๋ํ ์ฐธ์กฐ๋ ';' ๊ตฌ๋ถ์๋ก ๋๋์ผ ํฉ๋๋ค. (0) | 2021.01.07 |
IntelliJ ๋ค์์คํ์ด์ค(xmlns) ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ (0) | 2020.12.30 |
@Service, @Repository, @Controller์ ๊ฐ๋ตํ ์ค๋ช (0) | 2020.12.29 |
DI ์ด๋ ธํ ์ด์ ; @Autowired, @Inject, @Qualifier, @Resource (0) | 2020.12.29 |
@Component ์ฌ์ฉ๋ฒ (0) | 2020.12.29 |