MyBatis mapper.xml에 대해

Structure of Knowledge·2021년 1월 21일
0

Spring MVC Exercise in BIT

목록 보기
14/19

MyBatis: SQL문을 매핑하여 DB에서 자동으로 데이터를 뽑아준다

  1. mapper.xml은 SQL문을 매핑하는 소스이고, DAO의 구현클래스에 해당한다고 볼 수 있다.

  2. SQL 태그의 반환타입(resultType) 속성이 DTO이고, mapper 인터페이스에 매핑된 메소드의 반환타입이 List<DTO> 일 경우에, MyBatis 가 자동으로 List를 채워준다.

  3. mapper.xml 의 parameterType 에 여러 개의 변수를 넘겨줄 때, hashMap<이름,객체>을 이용해 보내주거나, VO를 설계하여 VO타입으로 보내는 것이 확장성이 좋다.
    (cf)내가 생각하는 VO의 개념은?)

  4. mapper.xml에서 ${ }, #{ } 둘다 쓰인다. 차이점은 다음과 같다.
    ex) name = "Sin";
    ${name} == Sin
    #{name} == 'Sin'

  5. 여러개의 mapper.xml을 한 Mapper 인터페이스에 매핑시킬 수 있다.

<!-- mybatis-config.xml -->
<mapper resource="bit/sdo/mapper/board-mapper.xml"/>
<mapper resource="bit/sdo/mapper/bdfile-mapper.xml"/>
<!-- board-mapper.xml -->
<mapper namespace="bit.sdo.mapper.BoardMapper">
<!-- bdfile-mapper.xml -->
<mapper namespace="bit.sdo.mapper.BoardMapper">
  1. ......

3-1. hashMap을 이용하는 방법

mapper.xml

<select id="list" parameterType="hashMap" resultType="Board" >
		<![CDATA[ ]]> <!-- 이 안에 넣어주면 부등호를 써도 상관없음 파싱하지 않음 -->
		select * from (select ROWNUM rnum, aa.* from (select * from BOARD order by SEQ desc) aa) where rnum &gt; #{initRow} and rnum &lt;= #{lastRow}
</select>


mapper.java

public interface BoardMapper {
	List<Board> list(Map<String,Integer> map);
	long getTotalCount();
	void insert(Board board);
	Board showContent(long seq);
	void delete(long seq);
	void update(Board board);
}

serviceImpl.java
public BoardVo listS(int cp, int ps) {
	Map<String, Integer> map = new HashMap<String,Integer>();
	int initRow = (cp-1)*ps;
	int lastRow = cp*ps;
	map.put("initRow",initRow);
	map.put("lastRow", lastRow);
		
	List<Board> list = boardDao.list(map);
	long totalCount = boardDao.getTotalCount();
	return new BoardVo(cp, totalCount, ps, list);
}


3-2. VO 클래스를 이용하는 방법

profile
객체와 제어, 비전공자 개발자 되기

0개의 댓글