-
[SPRING] SPRING 게시판 (3) - DB 연동2JAVA/SPRING 2020. 3. 11. 17:16728x90
테이블 생성
패키지 생성
VO 생성
// com.ms.board.Vo/boardVo public class boardVo { private int bno; private String title; private String content; private int count; private String writer; private LocalDate createTime; private LocalDate editTIme; ... //Getter Setter 생략 }
SQL 쿼리 작성
<!-- resources/mappers/BoardMapper.xml --> <?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="com.ms.board.Dao.BoardDao"> <select id="boardList" resultType="com.ms.board.Vo.BoardVo"> SELECT * FROM board </select> </mapper>
DAO 생성
// Dao/BoardDao.java @Repository("BoardDao") public class BoardDao { @Inject private SqlSession sqlSession; private String Namespace = "com.ms.board.Dao.BoardDao"; public List<BoardVo> getBoardList() throws Exception{ return sqlSession.selectList(Namespace + ".boardList"); } }
테스트 코드 생성
// test/java/boardDaoTest.java @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/root-context.xml", "classpath:spring/dataSource-context.xml" }) public class boardDaoTest { private static final Logger logger = LoggerFactory.getLogger(boardDaoTest.class); @Inject private BoardDao boardDao; @Test public void testBoardList() throws Exception{ List<BoardVo> boardList = boardDao.getBoardList(); logger.info("\nBoard List\n"); if(boardList.size() > 0) { for(BoardVo list:boardList) { logger.info(list.getTitle()); } }else { logger.info("데이터가 없습니다."); } } }
728x90'JAVA > SPRING' 카테고리의 다른 글
[SPRING] SPRING 게시판 (5) - CRUD [게시물 작성] (0) 2020.03.14 [SPRING] SPRING 게시판 (4) - CRUD [게시물 리스트] (0) 2020.03.12 [SPRING] Resource specification not allowed here for source level below 1.7 Error (0) 2020.03.11 [SPRING] SPRING게시판 (2) - DB 연동 (0) 2020.03.10 [SPRING] SPRING 게시판 (1) - 프로젝트 생성 (0) 2020.03.10