본문 바로가기
스프링 게시판

채팅형 게시판

by 뇽꾸리 2021. 2. 6.
반응형

jsp랑 스크립트는 복구불가함 ㅠㅠ 따로저장을안해놔서 대충 예전에 만들다가 쪼개서 저장해논거 복구했슴다 어휴 힘들어... 요거 은근 다들 많이 요청해가지고 영상을 따로찍어야하는데 못찍고있어용 주말에 넘 피곤.... 약속도 많구 ㅠ 핑계이긴해요 ㅋㅋ 옛날에 그 영상만 잘 찍었어도...

하 원래 1월에 올려준다하고 계속 미뤄가지고... 대충 이거라도....  

근데 옛날에 그냥 동빈나보고 따라친거라 사실 스프링으로 만든거빼고 다 똑같아서 이번엔 제 방식대로 만들예정이에요. 만약 지금 다시 만든다면 요거랑 다를거같습니다.

restcontroller 회사에서 한번도 아직 안써봣어요 아마 이번에 다시만들면 저거 안쓸거같습니당. 

아니면써서 만들까요? restcontroller = controller+responsebody ? 

inject 안쓰고 지금은 autowired 씁니다 사실 이거 차이 있나요?? 

 

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="crazychat">

<update id="crazyreadupdate">
update crazychat set chatread = 0 where fromid =#{toid} and toid=#{fromid}
</update>


<select id="crazychatread" resultType="int">
select count(seq) from crazychat where toid=#{fromid} and chatread = 1
</select>


<insert id="crazymessagesend">
insert into crazychat(seq,fromid,toid,chatcontent,chattime,chatread)
values(crazychat_seq.nextval,#{fromid},#{toid},#{chatcontent},sysdate,1)
</insert>


<select id="crazychatList" resultType="crazychatVO">
  select * from crazychat
    where  fromid =#{fromid} and toid = #{toid}
    or toid = #{fromid} and fromid = #{toid}
    order by chattime
</select>

<select id="findfirend"  resultType="String">
select userid 
from crazymember 
where userid= #{findid}
</select>

<select id="crazymessagebox" resultType="crazychatVO">
select * from crazychat 
where seq 
in (select max(seq) 
from crazychat 
where toid = #{fromid} or fromid=#{fromid} 
group by fromid,toid)
</select>


<select id="crazychatreadperson" resultType="int" parameterType="java.util.Map">
select count(seq) from crazychat where toid=#{toid} and fromid=#{fromid} and chatread = 1
</select>
</mapper>

dao

package com.spring.crazy.dao;

import java.util.List;


import com.spring.crazy.model.CrazyChatVO;


public interface CrazyChatDAO {

	//채팅 
	public List<CrazyChatVO> crazychatList(CrazyChatVO vo)throws Exception;
	
	//친구찾기
	public String findfriend(String findid)throws Exception;
	
	//메시지함
	public List<CrazyChatVO> messagebox(String fromid)throws Exception;

	//메시지보내기
	public void crazymessagesend(CrazyChatVO vo)throws Exception;
	
	//안읽은메시지전체
	public int crazychatread(String fromid)throws Exception;
	
	//안읽은메시지개개인
	public int crazychatreadperson(String from,String to)throws Exception;
	
	//메시지 읽음처리
	public void crazyreadupdate(CrazyChatVO vo)throws Exception;
}

daoImpl

package com.spring.crazy.daoimpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.spring.crazy.dao.CrazyChatDAO;
import com.spring.crazy.model.CrazyChatVO;


@Repository
public class CrazyChatDAOImpl implements CrazyChatDAO {

	@Inject
	SqlSession session;
	
	@Override
	public List<CrazyChatVO> crazychatList(CrazyChatVO vo) throws Exception {
		// TODO Auto-generated method stub
		return session.selectList("crazychat.crazychatList",vo);
	}

	@Override
	public String findfriend(String findid) throws Exception {
		// TODO Auto-generated method stub
		return session.selectOne("crazychat.findfirend",findid);
		
	}

	@Override
	public List<CrazyChatVO> messagebox(String fromid) throws Exception {
		// TODO Auto-generated method stub
		List<CrazyChatVO> crazymessagebox = session.selectList("crazychat.crazymessagebox",fromid);
		
		for(int i = 0; i < crazymessagebox.size(); i++) {
			CrazyChatVO x = crazymessagebox.get(i); // 
			//System.out.println(x + "x리스트입니다");
			
			for(int j = 0; j<crazymessagebox.size(); j++) {
				CrazyChatVO y = crazymessagebox.get(j);
				//System.out.println(y + "y리스트입니다");
				
				if(x.getFromid().equals(y.getToid()) && x.getToid().equals(y.getFromid())) {
					if(x.getSeq() < y.getSeq()) {
						crazymessagebox.remove(x);
						i--;
						break;
					}else {
						crazymessagebox.remove(y);
						j--;
					}				
				}
			
			}				
		}
		//System.out.println("daoImpl = "+ crazymessagebox);
		return crazymessagebox;
	}

	@Override
	public void crazymessagesend(CrazyChatVO vo) throws Exception {
		// TODO Auto-generated method stub
		
		session.insert("crazychat.crazymessagesend",vo);
	}

	@Override
	public int crazychatread(String fromid) throws Exception {
		// TODO Auto-generated method stub
		return session.selectOne("crazychat.crazychatread",fromid); // fromid == toid 에다가 넣을거임 nyong 에게 보낸 메시지카운트 
	}

	@Override
	public int crazychatreadperson(String from, String to) throws Exception {
		// TODO Auto-generated method stub
		Map<String,Object> map = new HashMap<String,Object>();
		for(int i = 0 ; i<=from.length(); i++) {
		String fromid = from;
		String toid = to;
		map.put("fromid", from);  //crazy
		map.put("toid", to);    //nyong
		}
		return session.selectOne("crazychat.crazychatreadperson",map);
	}

	@Override
	public void crazyreadupdate(CrazyChatVO vo) throws Exception {
		// TODO Auto-generated method stub
		
		session.update("crazychat.crazyreadupdate",vo);
		
	}

	
}

service

package com.spring.crazy.service;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.spring.crazy.model.CrazyChatVO;

public interface CrazyChatService {

	public List<CrazyChatVO> crazychatList(CrazyChatVO vo)throws Exception;	

	public String findfriend(String findid,HttpServletResponse res)throws Exception;
	
	public List<CrazyChatVO> crazymessagebox(String fromid)throws Exception;
	
	public void crazymessagesend(CrazyChatVO vo)throws Exception;
	
	public int crazychatread(String fromid)throws Exception;
	
	public int crazychatreadperson(String from,String to)throws Exception;
	
	public void crazychatreadupdate(CrazyChatVO vo)throws Exception;

}
package com.spring.crazy.serviceimpl;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.spring.crazy.dao.CrazyChatDAO;
import com.spring.crazy.model.CrazyChatVO;
import com.spring.crazy.model.CrazyMemberVO;
import com.spring.crazy.service.CrazyChatService;


@Service
public class CrazyChatServiceImpl implements CrazyChatService {

	@Inject
	CrazyChatDAO dao;
	
	@Override
	public List<CrazyChatVO> crazychatList(CrazyChatVO vo) throws Exception {
		// TODO Auto-generated method stub
		return dao.crazychatList(vo);
	}

	@Override
	public String findfriend(String findid,HttpServletResponse res) throws Exception {
		// TODO Auto-generated method stub
		String findedid = dao.findfriend(findid);
		if(findedid == null) {
			res.getWriter().print("1");  // 사용자가없음
		}else {
			res.getWriter().print("0");  // 찾음
			return findedid;
		}
		return null;
	}

	@Override
	public List<CrazyChatVO> crazymessagebox(String fromid) throws Exception {
		// TODO Auto-generated method stub
		
		return dao.messagebox(fromid);
	}

	@Override
	public void crazymessagesend(CrazyChatVO vo) throws Exception {
		// TODO Auto-generated method stub
		dao.crazymessagesend(vo);
	}

	@Override
	public int crazychatread(String fromid) throws Exception {
		// TODO Auto-generated method stub
		return dao.crazychatread(fromid);
	}

	@Override
	public int crazychatreadperson(String from, String to) throws Exception {
		// TODO Auto-generated method stub
		return dao.crazychatreadperson(from, to);
	}

	@Override
	public void crazychatreadupdate(CrazyChatVO vo) throws Exception {
		// TODO Auto-generated method stub
		dao.crazyreadupdate(vo);
	}

	
	
	
}

serviceimpl

package com.spring.crazy.controller;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
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.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.spring.crazy.model.CrazyChatVO;
import com.spring.crazy.service.CrazyChatService;

@RestController
public class ChatController {

	@Inject
	CrazyChatService service;

	@RequestMapping(value = "/messagesend.do", method = RequestMethod.POST)
	public ModelAndView crazysendmessage(@ModelAttribute CrazyChatVO vo, HttpSession session) throws Exception {
		String fromid = (String) session.getAttribute("userid");
		vo.setFromid(fromid);
		service.crazymessagesend(vo);
		String b = vo.getToid();
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:crazychat.do?toid=" + b);
		return mav;
	}

	@RequestMapping(value = "/crazychat.do")
	public ModelAndView crazychatlist(HttpSession session, CrazyChatVO vo, @RequestParam(defaultValue = "") String toid)
			throws Exception {
		ModelAndView mav = new ModelAndView();
		String fromid = (String) session.getAttribute("userid");

		// message box
		if (fromid != null) {

            // 받는사람,보낸사람 fromid(로그인한유저) 나인데 seq(max) 제일최근꺼
			List<CrazyChatVO> crazymessagebox = service.crazymessagebox(fromid);

			// 안읽은 메시지 전체
			int chatread = service.crazychatread(fromid);

			// 어떤사람으로부터 나한테온 chatread가 1인 메시지
			for (CrazyChatVO a : crazymessagebox) {
				String from = a.getToid(); // hi ,crazy
				String to = a.getFromid(); // nyong , nyong 
				if (fromid.equals(to)) {
					int unread = service.crazychatreadperson(from, to);
					if (unread == 0) {
						System.out.println("전부읽었고");
						System.out.println(unread);
						a.setChatread(0);
					} else {
						a.setChatread(unread);
						System.out.println(unread + "안읽은메시지몇개?");
						System.out.println(from);
						
					}

				}
			}
			mav.addObject("messagebox", crazymessagebox);
			mav.addObject("chatread", chatread);	
			mav.setViewName("crazychat");
			
		}

		if (fromid == null) {
			mav.setViewName("crazychat");

		} else if (fromid != null || toid != null) {
			vo.setFromid(fromid);
			vo.setToid(toid);
			List<CrazyChatVO> chatlist = service.crazychatList(vo);
			mav.setViewName("crazychat");
			mav.addObject("list", chatlist);
			String b = vo.getToid();
			mav.addObject("toid", b);
			
		
			service.crazychatreadupdate(vo);
		}

		return mav;
	}

	@RequestMapping(value = "/findfriend.do", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView findfriend(HttpServletResponse res, @RequestParam("findid") String findid) throws Exception {
		ModelAndView mav = new ModelAndView();
		System.out.println(findid);
		String findedid = service.findfriend(findid, res);
		System.out.println(findedid);
		if (findedid == null) {
			return null;
		} else {
			mav.setViewName("findid");
			mav.addObject("findedid", findedid);
			return mav;
		}

	}

}

 

 

반응형

댓글