implementation 'org.mybatis:mybatis:3.5.6'
해당 위치에 생성
mybatis-config에 db.properties 이름으로 파일 생성
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ai
username=root
password=12345
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- resource = "??"는 src/main/resouices 폴더를 찾아감 -->
<properties resource="mybatis-config/db.properties" />
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 추가한 맵퍼는 여기에 추가 -->
<mapper resource="mybatis-config/mapper/BlogMapper.xml"/>
</mappers>
</configuration>
<?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="org.mybatis.example.BlogMapper">
<select id="boardList" resultType="org.example.entity.Board">
select * from board
</select>
</mapper>
repersitoty 폴더 생성
repersitoty 폴더에 BoardDAO 생성
package org.example.repersitoty;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.entity.Board;
import java.io.InputStream;
import java.util.List;
public class BoardDAO {
// SqlSessionFactory
private static SqlSessionFactory sqlSessionFactory;
// sqlSeeion, SqlSession, SqlSession(5~8)
static {
try{
String resource = "mybatis-config/config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}catch (Exception e){
e.printStackTrace();
}
}
// 전체 게시물을 가져오는 기능 (TDD)
public List<Board> boardList(){
SqlSession session = sqlSessionFactory.openSession();
List<Board> list = session.selectList( "boardList");
session.close();
return list;
}
}
package org.example.controller;
import org.example.entity.Board;
import org.example.repersitoty.BoardDAO;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/List") // Servlet Mapping -> web.xml(DD)
public class BoardListController extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BoardDAO dao = new BoardDAO();
List<Board> list = dao.boardList();
// forword 방식
req.setAttribute("list", list);
RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/views/boardList.jsp");
rd.forward(req, resp);
}
}
views 폴더 생성 > boardList.jsp 파일 생성하기
부트 스크랩으로 화면단 불러올 수 있음
create table board(
num int not null auto_increment,
title varchar(100) not null,
content varchar(2000) not null,
writer varchar(50) not null,
cnt int default 0,
indate datetime default now(),
primary key(num)
);
insert into board (title, content, writer)
values ("스프링 게시판", "스프링게시판", "관리자");
insert into board (title, content, writer)
values ("스프링 게시판", "어려워요 잠와요 배고파요 찡찡", "전송민");
jsp에 붙여넣기
<c:forEach var="board" items="${list}">
<tr>
<td>${board.num}</td>
<td>${board.title}</td>
<td>${board.writer}</td>
<td><fmt:formatDate value="${board.indate}" pattern="yyyy-MM-dd" /></td>
<td>${board.cnt}</td>
</tr>
</c:forEach>