SMALL
안녕하세요, 코린이의 코딩 학습기 채니 입니다.
개인 포스팅용으로 내용에 오류 및 잘못된 정보가 있을 수 있습니다.
총 학생 수 조회
url-command.properties
/student/selectOne.do = com.ce.app.student.controller.SelectOneController
Controller
SelectOneController
public class SelectOneController extends AbstractController {
static final Logger log = Logger.getLogger(SelectOneController.class);
private StudentService studentService;
public SelectOneController(StudentService studentService) {
super();
this.studentService = studentService;
}
@Override
public String doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int totalCount = studentService.getTotalCount();
request.setAttribute("totalCount", totalCount);
return "student/selectOne";
}
}
Service
StudentService interface 생략
StudentServiceImpl
@Override
public int getTotalCount() {
try(SqlSession sqlSession = getSqlSession()) {
return studentDao.getTotalCount(sqlSession);
}
}
Dao
StudentDao interface 생략
StudentDaoImpl
@Override
public int getTotalCount(SqlSession sqlSession) {
return sqlSession.selectOne("student.getTotalCount");
}
리턴 레코드가 1 또는 0이기 때문에 selectOne 메소드를 이용해줍니다.
student-mapping.xml
<select id="getTotalCount" resultType="_int">
select count(*) from student where deleted_at is null
</select>
select의 경우 반드시 resultType / resultMap 속성을 반드시 작성해야 하기 때문에 내장별칭인 "_int"를 입력해주었습니다.
(parameterType은 생략 가능!)
selectOne.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mybatis 실습</title>
<script src="${pageContext.request.contextPath}/js/jquery-3.6.0.js"></script>
<style>
div#student-container{text-align:center;}
table.tbl-student{margin:10px auto;border:1px solid; border-collapse:collapse;}
table.tbl-student th,table.tbl-student td{
border:1px solid;
padding:5px;
line-height: 2em; /*input태그로 인해 cell이 높이가 길어져 border 두줄현상방지 */
}
table.tbl-student th{text-align:right;}
table.tbl-student td{text-align:left;}
table.tbl-student tr:last-of-type td:first-child{text-align:center;}
</style>
</head>
<body>
<div id="student-container">
<h2>학생정보 검색</h2>
<p>등록된 학생 수는 <span id="totalCount">${totalCount}</span>명입니다.</p>
<form name="studentSearchFrm">
<table class="tbl-student">
<tr>
<th>학생번호</th>
<td>
<input type="number" name="no" value="" required/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="검색" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
학생 정보 한 건 조회
url-command.properties
/student/student.do = com.ce.app.student.controller.StudentController
Controller
StudentController
public class StudentController extends AbstractController {
static final Logger log = Logger.getLogger(StudentController.class);
private StudentService studentService;
public StudentController(StudentService studentService) {
super();
this.studentService = studentService;
}
@Override
public String doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int no = Integer.parseInt(request.getParameter("no"));
log.debug("no = " + no);
Student student = studentService.selectOneStudent(no);
response.setContentType("application/json; charset=utf-8");
new Gson().toJson(student, response.getWriter());
return null;
}
}
Service
StudentService interface 생략
StudentServiceImpl
@Override
public Student selectOneStudent(int no) {
try(SqlSession sqlSession = getSqlSession()) {
return studentDao.selectOneStudent(sqlSession, no);
}
}
Dao
StudentDao interface 생략
StudentDaoImpl
@Override
public Student selectOneStudent(SqlSession sqlSession, int no) {
return sqlSession.selectOne("student.selectOneStudent", no);
}
student-mapping.xml
<select id="selectOneStudent" resultType="student">
select * from student where deleted_at is null and no = #{no}
</select>
등록해둔 별칭으로 resultType을 지정해주었습니다.
selectOne.jsp
<body>
<div id="student-container">
<h2>학생정보 검색</h2>
<p>등록된 학생 수는 <span id="totalCount">${totalCount}</span>명입니다.</p>
<form name="studentSearchFrm">
<table class="tbl-student">
<tr>
<th>학생번호</th>
<td>
<input type="number" name="no" value="" required/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="검색" />
</td>
</tr>
</table>
</form>
<hr />
<h2>학생 정보 수정</h2>
<form name="studentUpdateFrm">
<table class="tbl-student">
<tr>
<th>학생번호</th>
<td>
<input type="number" name="no" required readonly/>
</td>
</tr>
<tr>
<th>학생이름</th>
<td>
<input type="text" name="name" required/>
</td>
</tr>
<tr>
<th>학생전화번호</th>
<td>
<input type="tel" name="tel" required/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="수정" onclick="updateStudent();"/>
<input type="button" value="삭제" onclick="deleteStudent();" />
</td>
</tr>
</table>
</form>
</div>
<script>
document.studentSearchFrm.addEventListener('submit', (e) => {
e.preventDefault();
const no = e.target.no.value;
$.ajax({
url : "${pageContext.request.contextPath}/student/student.do",
data : {no},
success(student) {
const frm = document.studentUpdateFrm;
if(student) {
const {no, name, tel} = student;
frm.no.value = no;
frm.name.value = name;
frm.tel.value = tel;
} else {
alert("조회한 학생이 존재하지 않습니다.");
frm.reset();
}
},
error : console.log
});
});
</script>
</body>
LIST
'Java > └ Mybatis' 카테고리의 다른 글
Mybatis) resultMap 사용하기 (0) | 2022.08.08 |
---|---|
Mybatis) 학생 정보 변경 및 삭제하기 (0) | 2022.08.08 |
Mybatis) 학생 정보 등록 - MAP (0) | 2022.08.05 |
Mybatis) 학생 정보 등록 - DTO (별칭 등록, Mybatis 내장별칭) (0) | 2022.08.05 |
Mybatis) Mybatis 사용법, 설정파일 (0) | 2022.08.04 |