관리 메뉴

거니의 velog

230921_MyBatis 과제_myBatisBoardTest 본문

대덕인재개발원/대덕인재개발원_자바기반 애플리케이션

230921_MyBatis 과제_myBatisBoardTest

Unlimited00 2023. 9. 21. 19:39

[mybatis-config.xml]

<?xml version="1.0" encoding="UTF-8"?>
<!-- 이 문서는 MyBatis의 환경 설정을 구성하는 문서입니다. -->
<!DOCTYPE configuration 
	PUBLIC "-//mybatis.org/DTD Config 3.0//EN" 
	"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<properties resource="kr/or/ddit/mybatis/config/dbinfo.properties" />
	
	<settings>
		<setting name="jdbcTypeForNull" value="NULL" />
	</settings>
	
	<typeAliases>
		<typeAlias type="kr.or.ddit.vo.JdbcBoardVO" alias="boardVo" />
	</typeAliases>
	
	<environments default="oracleDev">
		<environment id="oracleDev">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${user}" />
				<property name="password" value="${pass}" />
			</dataSource>
		</environment>
	</environments>
	
	<mappers>
		<mapper resource="kr/or/ddit/mybatis/mappers/board-mapper.xml" />
	</mappers>
</configuration>

[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="boardVo">
		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>
	
	<delete id="deleteBoard" parameterType="int">
		delete from jdbc_board where board_no = #{board_no}
	</delete>
	
	<update id="updateBoard" parameterType="boardVo">
		update jdbc_board set 
		board_title = #{board_title}, 
		board_content = #{board_content}, 
		board_date = sysdate 
		where board_no = #{board_no}
	</update>
	
	<select id="getAllBoard" resultType="boardVo">
		select * from jdbc_board order by board_no desc
	</select>
	
	<select id="getBoard" parameterType="int" resultType="boardVo">
		select board_no, board_title, board_writer, 
		to_char(board_date, 'YYYY-MM-DD') as board_date, 
		board_cnt, board_content 
		from jdbc_board 
		where board_no = #{board_no}
	</select>
	
	<select id="getSearchBoard" parameterType="String" resultType="boardVo">
		select * from jdbc_board 
		where board_title like '%' || #{board_title} || '%' 
		order by board_no desc
	</select>
	
	<update id="setCountIncrement" parameterType="int">
		update jdbc_board set 
		board_cnt = board_cnt + 1 
		where board_no = #{board_no}
	</update>
	
</mapper>

[JdbcBoardDaoImpl.java]

package kr.or.ddit.board.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import kr.or.ddit.util.MyBatisUtil;
import kr.or.ddit.vo.JdbcBoardVO;

public class JdbcBoardDaoImpl implements IJdbcBoardDao {
	
	// 싱글톤 패턴
	private static JdbcBoardDaoImpl dao;
	private JdbcBoardDaoImpl() {}
	public static JdbcBoardDaoImpl getInstance() {
		if(dao == null) dao = new JdbcBoardDaoImpl();
		return dao;
	}

	@Override
	public int insertBoard(JdbcBoardVO boardVo) {
		SqlSession session = null;
		int cnt = 0; // 반환값이 저장될 변수
		
		try {
			session = MyBatisUtil.getSqlSession();
			
			cnt = session.insert("board.insertBoard", boardVo);
			
			if(cnt > 0) session.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(session != null) session.close();
		}
		
		return cnt;
	}

	@Override
	public int deleteBoard(int boardNo) {
		SqlSession session = null;
		int cnt = 0; // 반환값이 저장될 변수
		
		try {
			session = MyBatisUtil.getSqlSession();
			
			cnt = session.delete("board.deleteBoard", boardNo);
			
			if(cnt > 0) session.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(session != null) session.close();
		}
		
		return cnt;
	}

	@Override
	public int updateBoard(JdbcBoardVO boardVo) {
		SqlSession session = null;
		int cnt = 0; // 반환값이 저장될 변수
		
		try {
			session = MyBatisUtil.getSqlSession();
			
			cnt = session.update("board.updateBoard", boardVo);
			
			if(cnt > 0) session.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(session != null) session.close();
		}
		
		return cnt;
	}

	@Override
	public List<JdbcBoardVO> getAllBoard() {
		SqlSession session = null;
		List<JdbcBoardVO> boardList = null; // 반환값이 저장될 변수
		
		try {
			session = MyBatisUtil.getSqlSession();
			
			boardList = session.selectList("board.getAllBoard");
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(session != null) session.close();
		}
		
		return boardList;
	}

	@Override
	public JdbcBoardVO getBoard(int boardNo) {
		SqlSession session = null;
		JdbcBoardVO boardVo = null; // 반환값이 저장될 변수
		
		try {
			session = MyBatisUtil.getSqlSession();
			
			boardVo = session.selectOne("board.getBoard", boardNo);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(session != null) session.close();
		}
		
		return boardVo;
	}

	@Override
	public List<JdbcBoardVO> getSearchBoard(String title) {
		SqlSession session = null;
		List<JdbcBoardVO> boardList = null; // 반환값이 저장될 변수
		
		try {
			session = MyBatisUtil.getSqlSession();
			
			boardList = session.selectList("board.getSearchBoard", title);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(session != null) session.close();
		}
		
		return boardList;
	}

	@Override
	public int setCountIncrement(int boardNo) {
		SqlSession session = null;
		int cnt = 0; // 반환값이 저장될 변수
		
		try {
			session = MyBatisUtil.getSqlSession();
			
			cnt = session.update("board.setCountIncrement", boardNo);
			
			if(cnt > 0) session.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(session != null) session.close();
		}
		
		return cnt;
	}

}

[JdbcBoardController.java 를 실행...]

'대덕인재개발원 > 대덕인재개발원_자바기반 애플리케이션' 카테고리의 다른 글

230925_Servlet 1  (0) 2023.09.25
230922_Log4J  (0) 2023.09.22
230921_MyBatis 2  (0) 2023.09.21
230920_MyBatis 1  (0) 2023.09.20
230919_MVC 2  (0) 2023.09.18