본문 바로가기
Develop/Spring

[2-2.영속계층, 비즈니스계층]2.DAO생성과 XML Mapper 작업

by jaekk 2018. 8. 18.

1. XML 네임 스페이스 추가





2. SessionFactoy, SqlSessionTemplate 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    <!-- Root Context: defines shared resources visible to all other web components -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
        <property name="url" value="jdbc:log4jdbc:mysql://127.0.0.1:3307/book_ex?useSSl=false"></property>
        <property name="username" value="zerock"></property>
        <property name="password" value="zerock"></property>
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:mappers/**/*Mapper.xml"></property>
    </bean>
    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean>
    
 
    <!-- org.zerock.persistence 패키지 자동 인식 -->
    <context:component-scan base-package="org.zerock.persistence"></context:component-scan>
cs





3. BoardBAO 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package persistence;
 
import java.util.List;
 
import domain.BoardVO;
 
public interface BoardDAO {
    public void create(BoardVO vo) throws Exception;
    
    public BoardVO read(Integer bno) throws Exception;
    
    public void update(BoardVO vo) throws Exception;
    
    public void delet(Integer bno) throws Exception;
    
    public List<BoardVO> listAll() throws Exception; 
    
    
}
 
cs






4. XML Mapper에서의 SQL 처리

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.zerock.mapper.MemberMapper">
    <insert id="create">
        insert into tbl_board(title, content, writer)
        values
        (#{title},#{content},#{writer})
    </insert>
    <select id="read" resultType="org.zerock.domain.BoardVO">
        select
        *
        from
        tbl_board
        where bno = #{bno}
    </select>
    <update id="update">
        update tbl_board set title=#{title},
        content=#{content}
        where bno=#{bno}
    </update>
    <delete id="delete">
        delete tbl_board where bno=#{bno}
    </delete>
    <select id="listAll" resultType="org.zerock.domain.BoardVO">
        <![CDATA[]
        select 
            bon,title,content,writer,regdate,viewcnt
        from
            tbl_board
        where bno>0
        order by bno desc, regdate desc
        ]]>
    </select>
</mapper>

cs







5. BoardDAO의 구현 클래스 boardDAOImpl


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package persistence;
 
import java.util.List;
 
import javax.inject.Inject;
 
import org.apache.ibatis.session.SqlSession;
 
import domain.BoardVO;
 
public class BoardDAOImpl implements BoardDAO {
 
    @Inject
    private SqlSession session;
    
    private static String namespace = "org.zerock.mapper.BoardMapper";
 
    @Override
    public void create(BoardVO vo) throws Exception {
        session.insert(namespace+".create",vo);
    }
 
    @Override
    public BoardVO read(Integer bno) throws Exception {
        return session.selectOne(namespace+".update",bno);
    }
 
    @Override
    public void update(BoardVO vo) throws Exception {
        session.update(namespace+".update", vo);
    }
 
    @Override
    public void delete(Integer bno) throws Exception {
        session.delete(namespace+".delete",bno);
    }
 
    @Override
    public List<BoardVO> listAll() throws Exception {
        return session.selectList(namespace+".listAll");
    }
 
}
 
cs




6. BoardDAO 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
sqpackage org.zerock.test;
 
import javax.inject.Inject;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.zerock.domain.BoardVO;
import org.zerock.persistence.BoardDAO;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/root-context.xml" })
public class BoardDAOTest {
 
    @Inject
    private BoardDAO dao;
 
    private static Logger logger = LoggerFactory.getLogger(BoardDAOTest.class);
 
    @Test
    public void testCreate() throws Exception {
        BoardVO board = new BoardVO();
        board.setTitle("새로운 글을 넣습니다");
        board.setContent("새로운 글을 넣습니다.");
        board.setWriter("user00");
        dao.create(board);
    }
 
    @Test
    public void testRead() throws Exception {
        System.out.println(dao.read(12).toString());
        logger.info(dao.read(12).toString());
    }
 
    @Test
    public void testUpdate() throws Exception {
        BoardVO board = new BoardVO();
        board.setBno(12);
        board.setTitle("수정된 글입니다.");
        board.setContent("수정 테스트");
        dao.update(board);
    }
 
    @Test
    public void testDelete() throws Exception {
        dao.delete(12);
 
    }
}
 
cs





BoardDAOImpl 클래스 작성 후 스프링의 빈으로 등록되었는지 확인

root-context.xml 선택 후 Beans Graph 탭을 이용하여 확인





7. <typeAliases>

<typeAliases> : mybatis-config.xml에 작성

- parameterType , resultType의 번거로움 대체

1
2
3
    <typeAliases>
        <package name="org.zerock.domain"/>
    </typeAliases>
cs

-> parameterType, resultType에 org.zerock.domain 생략 가능


※ 본 게시글은 코드로 배우는 스프링 웹 프로젝트 책을 참고하여 작성하였습니다.



댓글