1. VO 객체
-> 데이터를 담을 객체를 생성
public class BoardVO {
private Integer bno;
private String title;
private String content;
private String writer;
private Date regdate;
private int viewcnt;
public Integer getBno() {
return bno;
}
public void setBno(Integer bno) {
this.bno = bno;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public Date getRegdate() {
return regdate;
}
public void setRegdate(Date regdate) {
this.regdate = regdate;
}
public int getViewcnt() {
return viewcnt;
}
public void setViewcnt(int viewcnt) {
this.viewcnt = viewcnt;
}
2. DAO 인터페이스
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 delete(Integer bno) throws Exception;
public List<BoardVO> listAll() throws Exception;
}
3. DAO 구현 클래스
@Repository
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+".read", 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");
}
}
4. 데이터베이스에 사용할 쿼리를 정리한 mapper xml파일
<mapper namespace="org.zerock.mapper.BoardMapper">
<insert id="create">
insert into tbl_board(title, content, writer)
values(#{title}, #{content}, #{writer})
</insert>
<select id="read" resultType="org.zerock.domain.BoardVO">
select
bno, title, content, writer,
regdate, viewcnt
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 from tbl_board where bno= #{bno}
</delete>
<select id="listAll" resultType="org.zerock.domain.BoardVO">
<![CDATA[
select
bno, title, content, writer, regdate, viewcnt
from
tbl_board
where bno > 0
order by bno desc, regdate desc
]]>
</select>
</mapper>
5. root-context.xml
-> Bean 관리
<!-- 스프링에 빈으로 등록하기, 패키지 자동인식 -->
<context:component-scan base-package="org.zerock.persistence"></context:component-scan>
<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://192.168.25.57:3306/book_ex"></property>
<property name="username" value="root"></property>
<property name="password" value="ddd00"></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:mapper/**/*Mapper.xml"></property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
</beans>
6. 테스트 클래스
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/**/*.xml"})
public class BoardDAOTest {
private static final Logger logger= LoggerFactory.getLogger(BoardDAOTest.class);
@Inject //BoardDAO에 대한 의존성 주입
private BoardDAO dao;
@Test // 글 생성 테스트
public void testCreate() throws Exception {
BoardVO board = new BoardVO();
board.setTitle("input the new Text");
board.setContent("input the new Text Content");
board.setWriter("user00");
dao.create(board);
}
@Test
public void testRead() throws Exception {
logger.info(dao.read(1).toString());
}
@Test
public void testUpdate() throws Exception {
BoardVO board = new BoardVO();
board.setBno(1);
board.setTitle("Update Title");
board.setContent("Update Content");
dao.update(board);
}
@Test
public void testDelete() throws Exception {
dao.delete(1);
}
출처
남가람북스
코드로 배우는 스프링 웹 프레임워크
'web > Spring' 카테고리의 다른 글
STS의 github 연동 (0) | 2016.12.21 |
---|---|
Mybatis의 #{} 문법 사용방법 (0) | 2016.12.21 |
Spring의 UTF-8 처리 필터 등록 (0) | 2016.12.21 |
Mybatis 관련 정리 (0) | 2016.12.21 |
typeAliases 사용방법 (0) | 2016.12.21 |