기능설명
1.사용자가 다른 사용자에게 메시지를 남길 수 있다.
2.메시지를 남긴 사용자는 10포인트 추가
3.남겨진 메시지를 읽으면 5포인트 추가
mysql>tbl_user 테이블 생성
create table tbl_user(
uid varchar(20) not null,
upw varchar(200) not null,
uname varchar(100) not null,
point int default 0,
primary key(uid)
);
mysql>tbl_message 테이블 생성
create table tbl_message(
mid int auto_increment primary key,
target varchar(20) not null,/*누구에게 보냈는지 (uid)*/
sender varchar(20) not null,/*보낸 사람 (uid)*/
message text,
sdate datetime default now(),/*보낸날짜*/
odate datetime,/*열어본 날짜*/
foreign key(target) references tbl_user(uid),/*참조*/
foreign key(sender) references tbl_user(uid)/*참조*/
);
<vo생성>
UserVO
package com.example.domain;
public class UserVO {
private String uid;
private String upw;
private String uname;
private int point;
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;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
@Override
public String toString() {
return "UserVO [uid=" + uid + ", upw=" + upw + ", uname=" + uname + ", point=" + point + "]";
}
}
<dao 생성>
package com.example.persistence;
import java.util.List;
import com.example.domain.UserVO;
public interface UserDAO {
public List<UserVO> list();
public UserVO read(String uid);//한개를 읽어와서 리턴타입이 UserVO
public void pointupdate(String uid,int point);
}
<mapper생성>
<?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
</select>
<select id="read" resultType="com.example.domain.UserVO">
select * from tbl_user
where uid=#{uid}
</select>
<update id="pointupdate">
update tbl_user
set point=point + #{point}
where uid=#{uid}
</update>
</mapper>
<implement 생성>
package com.example.persistence;
import java.util.HashMap;
import java.util.List;
import javax.inject.Inject;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import com.example.domain.UserVO;
@Repository
public class UserDAOImpl implements UserDAO{
@Inject
SqlSession session;
String namespace="UserMapper";
@Override
public List<UserVO> list() {
// TODO Auto-generated method stub
return session.selectList(namespace +".list");
}
@Override
public UserVO read(String uid) {
// TODO Auto-generated method stub
return session.selectOne(namespace+".read",uid);
}
@Override
public void pointupdate(String uid,int point) {
// TODO Auto-generated method stub
HashMap<String,Object> map=new HashMap<String,Object>();
map.put("uid", uid);
map.put("point", point);
session.update(namespace+".pointupdate",map);
}
}
<controller 생성>
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.ResponseBody;
import com.example.domain.UserVO;
import com.example.persistence.UserDAO;
@Controller
public class UserController {
@Inject
UserDAO dao;
@ResponseBody
@RequestMapping("read.json")
public UserVO read(String uid){
return dao.read(uid) ;
}
@RequestMapping("list") //사용자 목록
public String list(Model model){
model.addAttribute("list",dao.list());
return "list";
}
@RequestMapping("slist") //보낸메시지
public String slist(Model model,String uid){
model.addAttribute("list",dao.list());//유저목록
model.addAttribute("vo",dao.read(uid));
return "slist";
}
@RequestMapping("tlist") //받은메시지
public String tlist(){
return "tlist";
}
}
<vo 생성2>
package com.example.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class MessageVO {
private int mid;
private String target;
private String sender;
private String message;
@JsonFormat(pattern="yyyy-MM-dd kk:mm:ss",timezone="Asia/Seoul")
private Date sdate;
@JsonFormat(pattern="yyyy-MM-dd kk:mm:ss",timezone="Asia/Seoul")
private Date odate;
private String uname;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getMid() {
return mid;
}
public void setMid(int mid) {
this.mid = mid;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getSdate() {
return sdate;
}
public void setSdate(Date sdate) {
this.sdate = sdate;
}
public Date getOdate() {
return odate;
}
public void setOdate(Date odate) {
this.odate = odate;
}
@Override
public String toString() {
return "MessageVO [mid=" + mid + ", target=" + target + ", sender=" + sender + ", message=" + message
+ ", sdate=" + sdate + ", odate=" + odate + "]";
}
}
<DAO 생성2>
package com.example.persistence;
import java.util.List;
import com.example.domain.MessageVO;
public interface MessageDAO {
public List<MessageVO> slist(String sender); //보낸 메시지 리스트
public List<MessageVO> tlist(String target); //받은 메시지 리스트
public void insert(MessageVO vo);
public MessageVO read(int mid);
public void odateupdate(int mid);
}
<MAPPER생성2>
<?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="MessageMapper">
<select id="tlist" resultType="com.example.domain.MessageVO">
select m.*,u.uname from tbl_message m,tbl_user u
where target=#{target} and u.uid=m.sender
</select>
<select id="slist" resultType="com.example.domain.MessageVO">
select m.* ,u.uname from tbl_message m,tbl_user u
where sender=#{sender} and u.uid=m.target
</select>
<insert id="insert">
insert into tbl_message(target,sender,message)
values(#{target},#{sender},#{message})
</insert>
<select id="read" resultType="com.example.domain.MessageVO">
select m.*,u.uname from tbl_message m,tbl_user u
where mid=#{mid} and u.uid=m.sender
</select>
<update id="odateupdate">
update tbl_message set odate=now()
where mid=#{mid}
</update>
</mapper>
<IMPLEMENT 생성2>
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.MessageVO;
@Repository
public class MessageDAOImpl implements MessageDAO{
@Inject
SqlSession session;
String namespace="MessageMapper";
@Override
public List<MessageVO> slist(String sender) {
// TODO Auto-generated method stub
return session.selectList(namespace+".slist",sender);
}
@Override
public List<MessageVO> tlist(String target) {
// TODO Auto-generated method stub
return session.selectList(namespace +".tlist",target);
}
@Override
public void insert(MessageVO vo) {
// TODO Auto-generated method stub
session.insert(namespace+".insert",vo);
}
@Override
public MessageVO read(int mid) {
// TODO Auto-generated method stub
return session.selectOne(namespace+".read",mid);
}
@Override
public void odateupdate(int mid) {
// TODO Auto-generated method stub
session.update(namespace+".odateupdate",mid);
}
}
<Rest CONTROLLER 생성>
package com.example.web;
import java.util.List;
import javax.inject.Inject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.domain.MessageVO;
import com.example.persistence.MessageDAO;
import com.example.service.MessageService;
@RestController
public class MessageRestController {
@Inject
MessageDAO dao;
@Inject
MessageService service;
@RequestMapping("slist.json")
public List<MessageVO> slist(String sender){
return dao.slist(sender);
}
@RequestMapping("tlist.json")
public List<MessageVO> tlist(String target){
return dao.tlist(target);
}
@RequestMapping("insert")
public void insert(MessageVO vo){
service.insert(vo);
}
@RequestMapping("mread.json")
public MessageVO read(int mid){
return service.read(mid);
}
}
<list>//사용자목록
<%@ 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/tbl_user.css" rel="stylesheet">
</head>
<body>
<h1 class="h1">[사용자 목록]</h1>
<table class="tbl_user">
<tr>
<th>아이디</th>
<th>비밀번호</th>
<th>성명</th>
<th>Point</th>
<th>보낸메시지</th>
<th>받은메시지</th>
</tr>
<c:forEach items="${list}" var="vo">
<tr>
<td width=100>${vo.uid}</td>
<td width=100>${vo.upw}</td>
<td width=100>${vo.uname}</td>
<td width=100>${vo.point}</td>
<td width=100 height=30><button onClick="location.href='slist?uid=${vo.uid}'">보낸메시지</button></td>
<td width=100><button onClick="location.href='tlist?uid=${vo.uid}'">받은메시지</button></td>
</tr>
</c:forEach>
</table>
</body>
</html>
<slist>//사용자가 메시지를 보낸 것
<%@ 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/tbl_user.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.js"></script>
<style>
table{
margin:0px auto;
}
</style>
</head>
<body>
<h1 class="h1">[보낸메시지]</h1>
<div>
<table width=600px border=1px class="tbl_tlist">
<tr>
<td>보낸이:</td>
<td>
이름:${vo.uname}
포인트:<span id="point"></span>
</td>
</tr>
<tr>
<td>받는이:</td>
<td>
<select style="width:200x; height:40px;" id="target">
<c:forEach items="${list}" var="vo">
<option value="${vo.uid}">${vo.uid}-${vo.uname}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>내용:</td>
<td><input type="text" size=50 id="message"></td>
</tr>
</table>
<div class="btnsend">
<input type="button" value="보내기" id="btnsend">
<button onClick="location.href='list'">목록</button>
</div>
</div>
<table id="tbl" width=600px border=1px class="tbl_tlist"></table>
<script id="temp" type="text/x-handlebars-template">
{{#each .}}
<tr>
<td>{{mid}}</td>
<td>{{target}}-{{uname}}</td>
<td>{{message}}</td>
</tr>
{{/each}}
</script>
</body>
<script>
var sender="${param.uid}";
//alert(sender);
getlist();
getuser();
function getuser(){
$.ajax({
type:"get",
url:"read.json",
data:{"uid":sender},
success:function(data){
$("#point").html(data.point);
}
});
}
$("#btnsend").on("click",function(){
var target=$("#target").val();
var message=$("#message").val();
//alert(sender +"\n"+ target +"\n"+message);
$.ajax({
type:"get",
url:"insert",
data:{"sender":sender,"target":target,"message":message},
success:function(){
alert("메시지를 보냈습니다.");
$("#message").val("");
getlist();
}
});
});
function getlist(){
$.ajax({
type:"get",
url:"slist.json",
data:{"sender":sender},
success:function(data){
//alert("성공");
var temp=Handlebars.compile($("#temp").html());
$("#tbl").html(temp(data));
}
});
getuser();
}
</script>
</html>
<tlist>//사용자가 메시지를 받은 것
<%@ 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/tbl_user.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.js"></script>
<style>
table{
margin:0px auto;
}
</style>
</head>
<body>
<h1 class="h1">[받은메시지]</h1>
<div>
<table width=600px border=1px class="tbl_tlist">
<tr>
<td>받은이:<span id="uname"></span></td>
<td> 포인트:<span id="point"></span></td>
</tr>
</table>
<div class="btnsend">
<button onClick="location.href='list'">목록</button>
</div>
</div>
<table id="tbl" width=600px border=1px class="tbl_tlist"></table>
<script id="temp" type="text/x-handlebars-template">
{{#each .}}
<tr>
<td>{{mid}}</td>
<td>{{uname}}-{{sender}}</td>
<td>{{message}}</td>
<td><button mid="{{mid}}">메시지읽기</button></td>
</tr>
{{/each}}
</script>
<div style="width:600px; background:yellow; color:gray; margin:0px auto; margin-top:30px; padding:5px;">
보낸이:<span id="sender"></span><br>
내용:<span id="message"></span><br>
발신일:<span id="sdate"></span><br>
수신일:<span id="odate"></span><br>
</div>
</body>
<script>
var target="${param.uid}";
getlist();
$("#tbl").on("click","tr button",function(){
var mid=$(this).attr("mid");
$.ajax({
type:"get",
url:"mread.json",
data:{"mid":mid},
success:function(data){
$("#sender").html(data.sender + "-" + data.uname);
$("#message").html(data.message);
$("#sdate").html(data.sdate);
if(data.odate==null){
//alert("공백");
var today=new Date();
$("#odate").html(today.format("yyyy-MM-dd kk:mm:ss"));
}else{
$("#odate").html(data.odate);
}
getuser();
}
});
});
getuser();
function getuser(){
$.ajax({
type:"get",
url:"read.json",
data:{"uid":target},
success:function(data){
$("#uname").html(data.uname);
$("#point").html(data.point);
}
});
}
function getlist(){
$.ajax({
type:"get",
url:"tlist.json",
data:{"target":target},
success:function(data){
var temp=Handlebars.compile($("#temp").html());
$("#tbl").html(temp(data));
getuser();
}
});
}
</script>
</html>
<com.example.sercive>패키지 생성
pom.xml에 라이브러리 추가
root-context.xml 스캔 추가
MessageService 생성
package com.example.service;
import com.example.domain.MessageVO;
public interface MessageService {
public void insert(MessageVO vo);
public MessageVO read(int mid);
}
MessageServiceImpl 생성
package com.example.service;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.domain.MessageVO;
import com.example.persistence.MessageDAO;
import com.example.persistence.UserDAO;
@Service
public class MessageServiceImpl implements MessageService{
@Inject
MessageDAO mdao;
@Inject
UserDAO udao;
@Transactional
@Override
public void insert(MessageVO vo) {
// TODO Auto-generated method stub
mdao.insert(vo);
udao.pointupdate(vo.getSender(), 10);
}
@Transactional
@Override
public MessageVO read(int mid) {
// TODO Auto-generated method stub
MessageVO vo=mdao.read(mid);
if(vo.getOdate()==null){
udao.pointupdate(vo.getTarget(), 5);
mdao.odateupdate(mid);
}
return vo;
}
}
'spring' 카테고리의 다른 글
게시글 댓글 조회수를 추가한다. (0) | 2019.11.27 |
---|---|
맛집목록 만들기 (0) | 2019.11.26 |
게시판에 댓글만들기 (0) | 2019.11.22 |
사용자 게시판 만들기 (0) | 2019.11.22 |
Jstl 게시판 만들기 (0) | 2019.11.22 |