board-mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 이 문서는 myBatis에서 처리할 SQL문을 작성하는 문서입니다 -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org/DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="board">
<insert id="insertBoard" parameterType="bVo">
insert into jdbc_board(board_no, board_title, board_writer,
board_date, board_cnt, board_content)
values(board_seq.nextVal, #{board_title}, #{board_writer}, SYSDATE, 0, #{board_content})
</insert>
<select id="getAllBoard" resultType="bVo">
select * from jdbc_board
</select>
<select id="detail" parameterType="int" resultType="bVo">
select board_no, board_title, board_writer,
to_char(board_date,'yyyy-mm-dd') board_date, board_cnt, board_content
from jdbc_board where board_no = #{board_no}
</select>
<select id="searchBoard" resultType="bVo" parameterType="map">
select * from jdbc_board where ${FIELD} like '%'||#{VALUE}||'%'
</select>
<update id="updateBoard" parameterType="bVo">
update jdbc_board set board_title=#{board_title}, board_content=#{board_content} where board_no=#{board_no}
</update>
<delete id="deleteBoard" parameterType="int">
delete from jdbc_board where board_no=#{board_no}
</delete>
<update id="cbtUpdate" parameterType="map">
update jdbc_board
set board_cnt = (select board_cnt from jdbc_board where board_no =#{board_no})+1
where board_no=#{board_no}
</update>
</mapper>
BoardDaoImpl.java
package kr.or.ddit.board.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import kr.or.ddit.util.MyBatisUtil;
import kr.or.ddit.vo.BoardVo;
public class BoardDaoImpl implements IBoardDao {
private static BoardDaoImpl dao;
private BoardDaoImpl() {
}
public static BoardDaoImpl getInstance() {
if(dao==null) dao = new BoardDaoImpl();
return dao;
}
@Override
public int write(BoardVo vo) {
SqlSession session = null;
int cnt = 0;
try {
session = MyBatisUtil.getSqlSession();
cnt = session.insert("board.insertBoard", vo);
if(cnt>0) {
session.commit();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if(session!=null) try {session.close();} catch(Exception e) {}
}
return cnt;
}
@Override
public List<BoardVo> Allprint() {
SqlSession session = null;
List<BoardVo> list = null;
try {
session = MyBatisUtil.getSqlSession();
list = session.selectList("board.getAllBoard");
} catch (Exception e) {
// TODO: handle exception
} finally {
if(session!=null) try {session.close();} catch(Exception e) {}
}
return list;
}
@Override
public BoardVo detail(int board_no) {
SqlSession session = null;
BoardVo vo = null;
try {
session = MyBatisUtil.getSqlSession();
vo = session.selectOne("board.detail", board_no);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if(session!=null) try {session.close();} catch(Exception e) {}
}
return vo;
}
@Override
public List<BoardVo> search(Map<String, String> paramMap) {
SqlSession session = null;
List<BoardVo> list = null;
try {
session = MyBatisUtil.getSqlSession();
list = session.selectList("board.searchBoard", paramMap);
} catch (Exception e) {
// TODO: handle exception
} finally {
if(session!=null) try {session.close();} catch(Exception e) {}
}
return list;
}
@Override
public int edit(BoardVo vo) {
SqlSession session = null;
int cnt = 0;
try {
session = MyBatisUtil.getSqlSession();
cnt = session.update("board.updateBoard", vo);
if(cnt>0) {
session.commit();
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if(session!=null) try {session.close();} catch(Exception e) {}
}
return cnt;
}
@Override
public int delete(int board_no) {
SqlSession session = null;
int cnt = 0;
try {
session = MyBatisUtil.getSqlSession();
cnt = session.delete("board.deleteBoard", board_no);
if(cnt>0) {
session.commit();
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if(session!=null) try {session.close();} catch(Exception e) {}
}
return cnt;
}
@Override
public int cbtadd(int board_no) {
SqlSession session = null;
Map<String, Integer> map = new HashMap<String, Integer>();
int cnt = 0;
try {
session = MyBatisUtil.getSqlSession();
map.put("board_no", board_no);
cnt = session.update("board.cbtUpdate", map);
if(cnt>0) {
session.commit();
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if(session!=null) try {session.close();} catch(Exception e) {}
}
return cnt;
}
}