@MapperpublicinterfaceBoardMapper{
// 모든 글 조회List<Board> selectAllBoards();
// 게시글 개수 반환 (메인 게시글, 글 관리, 휴지통)intselectBoardTotalCount(Pagination pagination);
// 삭제된 글 제외 모두 조회 (메인 게시글, 글 관리, 휴지통)List<Board> selectBoardList(Pagination pagination);
// 게시글 수정voidupdateBoard(Board board);
// 아이디로 글 찾기Board findById(Long id);
// 휴지통으로 이동 (임시삭제)voidtemporaryDeleteById(Long id);
// 글 작성voidinsertBoard(Board board);
// 휴지통 비우기voidpermanentlyDeleteById(Long boardId);
}
5. resources/mapper 에 mapper.xml 추가
구조
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="CommonMapper"><sqlid="paging">
LIMIT
#{startList}, #{listSize}
</sql><sqlid="search">
delete_yn = 'N'
<iftest="searchText != null and searchText !=''">
AND
(
title LIKE CONCAT('%', #{searchText}, '%')
OR content LIKE CONCAT('%', #{searchText}, '%')
)
</if></sql><sqlid="myPost">
delete_yn = 'N'
<iftest="writer != null and writer !=''">
AND writer = #{writer}
</if></sql><sqlid="trash">
delete_yn = 'Y'
<iftest="writer != null and writer !=''">
AND writer = #{writer}
</if></sql></mapper>
<sql> : <include>를 통해 다른 쿼리에 활용 가능
refid : 참조
(select, update, insert, delete의) id : Mapper인터페이스 함수명과 동일
parameterType : #{}에 들어갈 파라미터 타입 (사용 권장하지 않음.)
resultType : 쿼리 결과 타입
#{} : 파리미터(get을 의미) -> DTO(모델)과 똑같은 필드명 작성
#{id} => getId
<if> : if문. test="조건"
참조 매퍼
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="CommonMapper"><sqlid="paging">
LIMIT
#{startList}, #{listSize}
</sql><sqlid="search">
delete_yn = 'N'
<iftest="searchText != null and searchText !=''">
AND
(
title LIKE CONCAT('%', #{searchText}, '%')
OR content LIKE CONCAT('%', #{searchText}, '%')
)
</if></sql><sqlid="myPost">
delete_yn = 'N'
<iftest="writer != null and writer !=''">
AND writer = #{writer}
</if></sql><sqlid="trash">
delete_yn = 'Y'
<iftest="writer != null and writer !=''">
AND writer = #{writer}
</if></sql></mapper>
6. Service에서 사용
privatefinal BoardMapper boardMapper;
@AutowiredpublicBoardService(BoardMapper boardMapper){
this.boardMapper = boardMapper;
}
// id를 이용해서 해당 글 수정public Board contentLoad(Long id){
Board board = boardMapper.findById(id);
return board;
}