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> |
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 생략 가능
※ 본 게시글은 코드로 배우는 스프링 웹 프로젝트 책을 참고하여 작성하였습니다.
'Develop > Spring' 카테고리의 다른 글
[오류]Injection of autowired dependencies failed (0) | 2018.08.19 |
---|---|
[오류]경로깨짐 (0) | 2018.08.19 |
[2-2.영속계층, 비즈니스계층]1.BoardVO 작성 (0) | 2018.08.18 |
[오류]localhost 페이지를 찾을 수 없음 (0) | 2018.08.18 |
[2-1등록,수정,삭제,조회 기능의 구현]6.CSS,JavaScript 준비 (0) | 2018.08.18 |
댓글