SMALL
안녕하세요, 코린이의 코딩 학습기 채니 입니다.
개인 포스팅용으로 내용에 오류 및 잘못된 정보가 있을 수 있습니다.
학생 정보 변경하기
url-command.properties
/student/updateStudent.do = com.ce.app.student.controller.StudentUpdateController
Controller
StudentUpdateController
public class StudentUpdateController extends AbstractController {
static final Logger log = Logger.getLogger(StudentUpdateController.class);
private StudentService studentService;
public StudentUpdateController(StudentService studentService) {
super();
this.studentService = studentService;
}
@Override
public String doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int no = Integer.parseInt(request.getParameter("no"));
String name = request.getParameter("name");
String tel = request.getParameter("tel");
Map<String, Object> data = new HashMap<>();
data.put("no", no);
data.put("name", name);
data.put("tel", tel);
int result = studentService.updateStudent(data);
response.setContentType("application/json; charset=utf-8");
Map<String, Object> msg = new HashMap<>();
msg.put("msg", "학생 정보를 성공적으로 수정하였습니다.");
new Gson().toJson(msg, response.getWriter());
return null;
}
}
Service
StudentServiceImpl
(interface 생략)
@Override
public int updateStudent(Map<String, Object> data) {
SqlSession sqlSession = getSqlSession();
int result = 0;
try {
result = studentDao.updateStudent(sqlSession, data);
sqlSession.commit();
} catch (Exception e) {
sqlSession.rollback();
throw e;
} finally {
sqlSession.close();
}
return result;
}
Dao
StudentDaoImpl
(interface 생략)
@Override
public int updateStudent(SqlSession sqlSession, Map<String, Object> data) {
return sqlSession.update("student.updateStudent", data);
}
student-mapper.xml
<update id="updateStudent">
update student set name = #{name}, tel = #{tel}, updated_at = sysdate where no = #{no}
</update>
selectOne.jsp
<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>
const updateStudent = () => {
const frm = document.studentUpdateFrm;
const student = {
no : frm.no.value,
name : frm.name.value,
tel : frm.tel.value
};
$.ajax({
url : "${pageContext.request.contextPath}/student/updateStudent.do",
data : student,
method : "POST",
success(response) {
alert(response.msg);
frm.reset();
},
error : console.log
});
};
</script>
정보가 성공적으로 변경된 것을 확인할 수 있습니다.
학생 정보 삭제하기
url-command.properties
/student/deleteStudent.do = com.ce.app.student.controller.StudentDeleteController
Controller
StudentDeleteController
public class StudentDeleteController extends AbstractController{
static final Logger log = Logger.getLogger(StudentDeleteController.class);
private StudentService studentService;
public StudentDeleteController(StudentService studentService) {
super();
this.studentService = studentService;
}
@Override
public String doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int no = Integer.parseInt(request.getParameter("no"));
int result = studentService.deleteStudent(no);
Map<String, Object> msg = new HashMap<>();
msg.put("msg", "학생 정보를 성공적으로 삭제하였습니다.");
response.setContentType("application/json; charset=utf-8");
new Gson().toJson(msg, response.getWriter());
return null;
}
}
Service
StudentServiceImpl
(interface 생략)
@Override
public int deleteStudent(int no) {
SqlSession sqlSession = getSqlSession();
int result = 0;
try {
result = studentDao.deleteStudent(sqlSession, no);
sqlSession.commit();
} catch(Exception e) {
sqlSession.rollback();
throw e;
} finally {
sqlSession.close();
}
return result;
}
Dao
StudentDaoImpl
(interface 생략)
@Override
public int deleteStudent(SqlSession sqlSession, int no) {
return sqlSession.update("student.deletStudent", no);
}
student-mapper.xml
<update id="deletStudent">
update student set deleted_at = sysdate where no = #{no}
</update>
selectOne.jsp
<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>
const deleteStudent = () => {
const frm = document.studentUpdateFrm;
const no = frm.no.value;
if(confirm("학생 정보를 정말 삭제하시겠습니까?")) {
$.ajax({
url : "${pageContext.request.contextPath}/student/deleteStudent.do",
data : {no},
method : "POST",
success(response) {
alert(response.msg);
location.reload();
},
error : console.log
});
}
};
실제로 데이터를 삭제하는 것이 아닌 deleted_at으로 관리하는 것이기 때문에 update문을 사용했으며,
등록된 학생 수를 실시간으로 변경 처리하기 위해 page reload 처리를 하였습니다.
LIST
'Java > └ Mybatis' 카테고리의 다른 글
Mybatis) 여러 건 조회하기 (JSTL 이용하여 문자열을 Date형식으로 파싱하기) (0) | 2022.08.08 |
---|---|
Mybatis) resultMap 사용하기 (0) | 2022.08.08 |
Mybatis) 총 학생 수 조회, 학생 정보 한 건 조회 (0) | 2022.08.05 |
Mybatis) 학생 정보 등록 - MAP (0) | 2022.08.05 |
Mybatis) 학생 정보 등록 - DTO (별칭 등록, Mybatis 내장별칭) (0) | 2022.08.05 |