sql>tbl_user 만들기
create table tbl_user(
uid varchar(20) not null,
upw varchar(200) not null,
uname varchar(100) not null,
primary key(uid)
);
UserVO(class)
package com.example.domain;
public class UserVO {
private String uid;
private String upw;
private String uname;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUpw() {
return upw;
}
public void setUpw(String upw) {
this.upw = upw;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
@Override
public String toString() {
return "UserVO [uid=" + uid + ", upw=" + upw + ", uname=" + uname + "]";
}
}
UserDAO(interface)
package com.example.persistence;
import java.util.List;
import com.example.domain.SearchCriteria;
import com.example.domain.UserVO;
public interface UserDAO {
public List<UserVO> list(SearchCriteria cri) throws Exception;
public int total(SearchCriteria cri) throws Exception;
public void insert(UserVO vo) throws Exception;
public UserVO read(String uid) throws Exception;
public void update(UserVO vo) throws Exception;
public void delete(String uid) throws Exception;
}
UserMapper(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="UserMapper">
<select id="list" resultType="com.example.domain.UserVO">
select * from tbl_user
<if test="searchType=='uname'">
where uname like concat('%',#{keyword},'%')
</if>
limit #{pageStart},#{perPageNum}
</select>
<select id="total" resultType="int">
select count(*) from tbl_user
<if test="searchType=='uname'">
where uname like concat('%',#{keyword},'%')
</if>
</select>
<insert id="insert">
insert into tbl_user(uid,upw,uname)
values (#{uid},#{upw},#{uname})
</insert>
<select id="read" resultType="com.example.domain.UserVO">
select * from tbl_user
where uid=#{uid}
</select>
<update id="update">
update tbl_user set
upw=#{upw},
uname=#{uname}
where uid=#{uid}
</update>
<delete id="delete">
delete from tbl_user
where uid=#{uid}
</delete>
</mapper>
DBTest3(class)
package com.example.web;
import javax.inject.Inject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.example.domain.Criteria;
import com.example.persistence.ReplyDAO;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/**/*.xml"})
public class DBTest3 {
@Inject
ReplyDAO dao;
@Test
public void list() throws Exception{
Criteria cri=new Criteria();
cri.setPage(2);
cri.setPerPageNum(10);
dao.list(cri,99);
}
}
UserDAOImpl(class)
package com.example.persistence;
import java.util.List;
import javax.inject.Inject;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import com.example.domain.SearchCriteria;
import com.example.domain.UserVO;
@Repository
public class UserDAOImpl implements UserDAO{
@Inject
SqlSession session;
private static final String namespace="UserMapper";
@Override
public List<UserVO> list(SearchCriteria cri) throws Exception {
// TODO Auto-generated method stub
return session.selectList(namespace+".list",cri);
}
@Override
public int total(SearchCriteria cri) throws Exception {
// TODO Auto-generated method stub
return session.selectOne(namespace+".total",cri);
}
@Override
public void insert(UserVO vo) throws Exception {
// TODO Auto-generated method stub
session.insert(namespace+".insert",vo);
}
@Override
public UserVO read(String uid) throws Exception {
// TODO Auto-generated method stub
return session.selectOne(namespace+".read",uid);
}
@Override
public void update(UserVO vo) throws Exception {
// TODO Auto-generated method stub
session.update(namespace+".update");
}
@Override
public void delete(String uid) throws Exception {
// TODO Auto-generated method stub
session.delete(namespace+".delete",uid);
}
}
UserController(class)
package com.example.web;
import javax.inject.Inject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.example.domain.PageMaker;
import com.example.domain.SearchCriteria;
import com.example.domain.UserVO;
import com.example.persistence.UserDAO;
@RequestMapping("user")
@Controller
public class UserController {
@Inject
UserDAO dao;
@RequestMapping("list")
public String list(Model model,SearchCriteria cri) throws Exception{
cri.setPerPageNum(10);
PageMaker pm=new PageMaker();
pm.setCri(cri);
pm.setTotalCount(dao.total(cri));
model.addAttribute("list",dao.list(cri));
model.addAttribute("pm",pm);
return "user/list";
}
@RequestMapping("insert")
public String insertjsp(){
return "user/insert";
}
@RequestMapping(value="insert", method=RequestMethod.POST)
public String insert(UserVO vo) throws Exception{
dao.insert(vo);
return "redirect:list";
}
@RequestMapping("read")
public String read(Model model,String uid,SearchCriteria cri) throws Exception{
model.addAttribute("vo",dao.read(uid));
model.addAttribute("cri",cri);
return "user/read";
}
@RequestMapping(value="update", method=RequestMethod.POST)
public String update(UserVO vo,SearchCriteria cri,RedirectAttributes rttr) throws Exception{
dao.update(vo);
rttr.addAttribute("keyword",cri.getKeyword());
rttr.addAttribute("searchType",cri.getSearchType());
rttr.addAttribute("page",cri.getPage());
rttr.addAttribute("uid",vo.getUid());
return "redirect:list";
}
@RequestMapping(value="delete", method=RequestMethod.POST)
public String delete(String uid) throws Exception{
dao.delete(uid);
return "redirect:list";
}
}
user>insert(jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="${pageContext.request.contextPath}/resources/main.css" rel="stylesheet">
</head>
<body>
<h1 style="margin:0px auto; margin-top:10px;margin-bottom:10px;">[글쓰기 화면]</h1>
<form name="frm" method="post" action="insert">
<table border=1 width=400 style="margin-bottom:20px;">
<tr>
<td>아이디</td>
<td><input type="text" name="uid"></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="text" name="upw"></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="uname"></td>
</tr>
</table>
<input type="submit" value="SAVE">
<input type="reset" value="CANCEL">
<input type="button" value="MAIN" onClick="location.href='list'">
</form>
</body>
</html>
user>list(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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="${pageContext.request.contextPath}/resources/main.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<h1 style="margin:20px;">[사용자 목록]</h1>
<form name="frm" style="margin:10px;" action="list" method="get">
<select name="searchType">
<option value="uname"<c:out value="${pm.cri.searchType=='title'?'selected':'' }"/>>이름</option>
</select>
<input type="text" name="keyword" value="${pm.cri.keyword}">
<input type="submit" value="검색">
<input type="text" name="page" value="${pm.cri.page}">
<input type="text" name="uid">
</form>
<div style="width:488px; border:solid 2px; margin:0px auto; margin-top:10px; margin-bottom:10px;">
<button onClick="location.href='insert'">INSERT</button>
</div>
<table border=1 width=500 class="tbl-user">
<tr class="tbl-title">
<th width=100>아이디</th>
<th width=100>비밀번호</th>
<th width=300>이름</th>
</tr>
<c:forEach items="${list}" var="vo">
<tr class="tbl-row">
<td width=100><a href="#" onClick="funread('${vo.uid}')">${vo.uid}</a></td>
<td width=100>${vo.upw}</td>
<td width=300>${vo.uname}</td>
</tr>
</c:forEach>
</table>
<div id="pagination">
<!-- JStl -->
<c:if test="${pm.prev}">
<a href="list?page=${pm.startPage-1}">◀</a>
</c:if>
<c:forEach begin="${pm.startPage}" end="${pm.endPage}" var="i">
<c:if test="${i==pm.cri.page}">
[<a href="${i}" class="active">${i}</a>]
</c:if>
<c:if test="${i!=pm.cri.page}">
[<a href="${i}">${i}</a>]
</c:if>
</c:forEach>
<c:if test="${pm.next}">
<a href="list?page=${pm.endPage+1}">▶</a>
</c:if>
</div>
</body>
<script>
$("#pagination").on("click","a",function(event){
event.preventDefault();
var page=$(this).attr("href");
$(frm.page).val(page);
frm.action="list"
frm.submit();
});
function funread(uid){
frm.uid.value=uid;
frm.method="get";
frm.action="read";
frm.submit();
}
</script>
</html>
user>read(jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="${pageContext.request.contextPath}/resources/main.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<h1 style="margin:0px auto; margin-top:10px;margin-bottom:10px;">[글읽기 화면]</h1>
<form name="frm" action="update" method="post">
<input type="text" name="page" value="${cri.page}">
<input type="text" name="searchType" value="${cri.searchType}">
<input type="text" name="keyword" value="${cri.keyword}">
<input type="text" name="uid" value="${vo.uid}">
<table border=1 width=400 style="margin-bottom:20px;">
<tr>
<td>아이디</td>
<td><input type="text" name="uid" value="${vo.uid}"></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="text" name="upw" value="${vo.upw}"></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="uname" value="${vo.uname}"></td>
</tr>
</table>
<input type="button" value="UPDATE" id="btnup">
<input type="button" value="DELETE" id="btndel">
<input type="reset" value="CANCEL">
<input type="button" value="MAIN" onClick="location.href='list'">
</form>
</body>
<script>
$("#btnup").on("click",function(){
if(!confirm("수정하시겠습니까?")) return;
frm.action="update";
frm.method="post";
frm.submit();
});
$("#btndel").on("click",function(){
if(!confirm("삭제하시겠습니까?")) return;
frm.action="delete";
frm.method="post";
frm.submit();
});
</script>
</html>
'spring' 카테고리의 다른 글
맛집목록 만들기 (0) | 2019.11.26 |
---|---|
Transaction 처리 (0) | 2019.11.26 |
게시판에 댓글만들기 (0) | 2019.11.22 |
Jstl 게시판 만들기 (0) | 2019.11.22 |
네이버 API와 CURD작업 연결 (0) | 2019.11.22 |