2022.10.18

Jimin·2022년 10월 18일
0

비트캠프

목록 보기
58/60
post-thumbnail

SqlSession과 SQL Mapper

메서드를 통해 넘어온 값이 Wrapper 객체이거나 String 객체일 경우 이름은 아무거나 써도 상관없다.


Service와 DAO와 Table


085. Mybatis SQL 매퍼 사용하기: DAO 구현 자동화

  • Mybastis SQL 매퍼를 사용하여 DAO를 구현하는 방법

1단계 - Mybatis SQL Mapper 라이브러리를 프로젝트에 추가한다.

  • search.maven.org 에서 'mybatis' 검색한다.
    • mybatis 라이브러리 파일
  • search.maven.org 에서 'mybatis-spring' 검색한다.
    • mybatis를 spring과 연동할 때 사용되는 라이브러리 파일
  • 라이브러리를 빌드 스크립트 파일(build.gradle)에 설정한다.
  • 이클립스 IDE용 설정 파일을 갱신한다.
  • 프로젝트를 갱신한다.
  • 라이브러리가 추가된 것을 확인한다.

2단계 - SqlSessionFactory 객체를 준비한다.

  • com.bitcamp.board.config.MybatisConfig 클래스 생성
    • sqlSessionFactory() 메서드 추가
  • com.bitcamp.board.config.AppWebApplicationInitializer 클래스 변경
    • getRootConfigClasses() 메서드 리턴 값 변경

3단계 - Mybatis를 사용하는 DAO를 정의한다.

  • com.bitcamp.board.dao.MybatisMemberDao 클래스 생성

  • com/bitcamp/board/mapper/MemberDaoMapper.xml 파일 생성

  • com.bitcamp.board.dao.MybatisBoardDao 클래스 생성

  • com/bitcamp/board/mapper/BoardDaoMapper.xml 파일 생성

  • MybatisBoardDao class

@Repository // DAO 역할을 수행하는 객체에 붙이는 애노테이션
public class MybatisBoardDao implements BoardDao {

    @Autowired
    DataSource ds;

    @Autowired
    SqlSessionFactory sqlSessionFactory;

    @Override
    public int insert(Board board) throws Exception {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            return sqlSession.insert("BoardDao.insert", board);
        }
    }

    @Override
    public Board findByNo1(int no) throws Exception {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

            Board board = sqlSession.selectOne("BoardDao.findByNo", no);

            // 게시글 첨부파일 가져오기
            List<AttachedFile> attachedFiles =  sqlSession.selectList("BoardDao.findFilesByBoard", no);

            board.setAttachedFiles(attachedFiles);

            return board;
        }
    }

    @Override
    public Board findByNo2(int no) throws Exception {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            return sqlSession.selectOne("BoardDao.findByNo", no);
        }
    }

    @Override
    public Board findByNo3(int no) throws Exception {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            return sqlSession.selectOne("BoardDao.findByNo3", no);
        }
    }

    @Override
    public int update(Board board) throws Exception {
        try (PreparedStatement pstmt = ds.getConnection().prepareStatement(
                "update app_board set title=?, content=? where bno=?")) {

            pstmt.setString(1, board.getTitle());
            pstmt.setString(2, board.getContent());
            pstmt.setInt(3, board.getNo());

            return pstmt.executeUpdate();
        }
    }

    @Override
    public int delete(int no) throws Exception {
        try (PreparedStatement pstmt = ds.getConnection().prepareStatement("delete from app_board where bno=?")) {
            pstmt.setInt(1, no);
            return pstmt.executeUpdate();
        }
    }

    @Override
    public List<Board> findAll() throws Exception {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            return sqlSession.selectList("BoardDao.findAll");
        }
    }


    @Override
    public int insertFiles(Board board) throws Exception {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            return sqlSession.insert("BoardDao.insertFiles", board);
        }
    }

    @Override
    public AttachedFile findFileByNo(int fileNo) throws Exception {
        try (PreparedStatement pstmt = ds.getConnection().prepareStatement(
                "select bfno, filepath, bno from app_board_file where bfno = " + fileNo);
                ResultSet rs = pstmt.executeQuery()) {

            if (!rs.next()) {
                return null;
            }

            AttachedFile file = new AttachedFile();
            file.setNo(rs.getInt("bfno"));
            file.setFilepath(rs.getString("filepath"));
            file.setBoardNo(rs.getInt("bno"));

            return file;
        }
    }

    @Override
    public List<AttachedFile> findFilesByBoard(int boardNo) throws Exception {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            return sqlSession.selectList("BoardDao.findFilesByBoard", boardNo);
        }
    }

    @Override
    public int deleteFile(int fileNo) throws Exception {
        try (PreparedStatement pstmt = ds.getConnection().prepareStatement(
                "delete from app_board_file where bfno=?")) {

            pstmt.setInt(1, fileNo);
            return pstmt.executeUpdate();
        }
    }

    @Override
    public int deleteFiles(int boardNo) throws Exception {
        try (PreparedStatement pstmt = ds.getConnection().prepareStatement(
                "delete from app_board_file where bno=?")) {

            pstmt.setInt(1, boardNo);
            return pstmt.executeUpdate();
        }
    }

}
  • BoardDaoMapper.xml file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="BoardDao">
  <resultMap type="board" id="boardMap">
    <id column="bno" property="no"/>
    <result column="title" property="title"/>
    <result column="content" property="content"/>
    <result column="cdt" property="createdDate"/>
    <result column="vw_cnt" property="viewCount"/>

    <!-- 조인 결과에서 같은 게시글에 대해 한 개의 회원 데이터를 담을 경우 -->    
    <association property="writer" javaType="member">
        <id column="mno" property="no"/>
         <result column="name" property="name"/>
    </association>
    
    <!-- 조인 결과에서 같은 게시글에 대해 여러 개의 첨부 파일 데이터를 담을 경우 -->
    <collection property="attachedFiles" ofType="attachedFile">
        <id column="bfno" property="no"/>
         <result column="filepath" property="filepath"/>
    </collection>
  </resultMap>
  
  <resultMap type="attachedFile" id="attachedFileMap">
    <id column="bfno" property="no"/>
    <result column="filepath" property="filepath"/>
    <result column="bno" property="boardNo"/>
  </resultMap>

  <select id="findAll" resultMap="boardMap">
    select 
      b.bno,
      b.title,
      b.cdt,
      b.vw_cnt,
      m.mno,
      m.name
    from 
      app_board b
     join app_member m on b.mno = m.mno
    order by 
      bno desc
  </select>


<select id="findByNo" resultMap="boardMap">
    select 
      b.bno,
      b.title,
      b.content,
      b.cdt,
      b.vw_cnt,
      m.mno,
      m.name
    from 
      app_board b
     join app_member m on b.mno = m.mno
    where 
      b.bno = #{value}
  </select>
  
  <select id="findByNo3" resultMap="boardMap">
    select 
      b.bno,
      b.title,
      b.content,
      b.cdt,
      b.vw_cnt,
      m.mno,
      m.name,
      bf.bfno,
      bf.filepath
    from 
      app_board b
     join app_member m on b.mno = m.mno
     left outer join app_board_file bf on  b.bno=bf.bno
    where 
      b.bno = #{value}
  </select>
  
  <select id="findFilesByBoard" resultMap="attachedFileMap">
    select 
      bfno, 
      filepath, 
      bno 
    from 
      app_board_file
    where 
      bno = #{value}
  </select>
  
  <insert id="insert" parameterType="board"
                  useGeneratedKeys="true" keyColumn="bno"   keyProperty="no">
    insert into app_board(title,content,mno) 
    values(#{title},#{content},#{writer.no})
  </insert>


  <delete id="deleteByMember">
    delete from app_board 
    where mno=#{value}
  </delete>
  
  <insert id="insertFiles" parameterType="board">
    insert into app_board_file(filepath,bno) 
    values
    <foreach collection="attachedFiles" item="file" separator=",">
    (#{file.filepath},#{no})
    </foreach>
  </insert>

</mapper>

테이블 조인과 SQL Mapper

select
	b.bno,
    b.title,
    b.content,
    b.cdt,
    m.mno,
    m.name
from
	app_board b
    (inner) join app_member m
    on b.mno = m.mno
order by
	b.bno desc;

→ 질의 결과

bnotitlecontentcdtmnoname
1aaaok2022-10-18100hong

bno ~ cdt : Board 객체에 저장된다.
mno ~ name : Member 객체에 저장된다.
Board 객체가 Member 객체를 writer로서 포함한다.

SqlSession.selectList("BoardDao.findAll")Member 객체를 포함한 Board 객체 ListReturn한다. 
profile
https://github.com/Dingadung

0개의 댓글