create table board {
bno int auto_increment primary key,
title varchar(45) not null,
content text not null,
writer varchar(30) not null,
view_cnt int default 0 null,
comment_cnt int default 0 null,
reg_date datetime null
};
<mapper namespace="com.fastcampus.ch4.dao.BoardMapper">
<select id="select" parameterType="int" resultType="BoardDto">
SELECT bno, title, content, writer
, view_cnt, commnet_cnt, reg_date
FROM board
WHERE bno = #{bno}
</select>
public interface BoardDao {
BoardDto select(Integer bno) throws Exception;
int delete(Integer bno) throws Exception;
int insert(BoardDto dto) throws Exception;
int update(BoardDto dto) throws Exception;
int increaseView(Integer bno) throws Exception;
}
@Repository
public class BoardDaoImpl implements BoardDao {
@Autowired private SqlSession session
private static String namespace
= "com.fastcampus.ch4.dao.BoardMapper.";
public BoardDto select(Integer bno) throws Exception {
return session.selectOne(namespace + "select", bno);
} //T selectOne(String statement, Object parameter)
계층간의 데이터를 주고 받기 위해 사용되는 객체
create table board {
bno int auto_increment primary key,
title varchar(45) not null,
content text not null,
writer varchar(30) not null,
view_cnt int default 0 null,
reg_date datetime null
};
//==>boardDto로
public class BoardDto {
//int가 아닌 Integer 타입으로 하면 null이 들어 왔을 때 에러X
private Integer bno; //게시글번호
private String title;
private String content;
private String writer;
private int view_cnt;
private int commnet_cnt;
private Date reg_date;
}
}
//DTO 사용
@Repository
public class BoardDaoImpl implements BoardDao {
@Autowired private SqlSession session;
private static String namespace
= "com.fastcampus.ch4.dao.BoardMapper.";
public BoardDto select(Integer bno) throws Exception {
return session.selectOne(namespace + "select", bno);
} //T selectOne(String statement, Object parameter)
@Controller
@Service
@Repository
DTO는 각 계층간에 데이터를 전달하는 역할.
//#{}
<insert id="insert" parameterType="BoardDto">
INSERT INTO board
(title, content, writer)
VALUES
(#{title}, #{content}, #{writer})
</insert>
//==>
String sql = "INSERT INTO board "
+ " (title, content, writer) "
+ "VALUES "
+ "(?, ?, ?)";
PreparedStatement pstmt = conn prepareStatement(sql);
int result = pstmt.executeUpdate();
//${}
<insert id="insert" parameterType="BoardDto">
INSERT INTO board
(title, content, writer)
VALUES
('${title}', '${content}', '${writer}')
</insert>
//==>
String sql = "INSERT INTO board "
+ " (title, content, writer) "
+ "VALUES "
+ " (''+title+"', '"+content+"', '"+writer+"')";
Statement stmt = conn.createStatement();
int result = stmt.executeUpdate(sql);
XML내의 특수 문자(<,>,&,...)는 < >로 변환 필요.
또는 특수문자가 포함된 쿼리를 <![CDATA[와]]>로 감싼다.