项目说明
跟学尚硅谷最新版本2022,完成了qqzone项目的制作,项目所有源码给出,方便所有需要学习的朋友,项目给出,Gitee码云的连接,可以自行fork。麻烦顺手给文章点个赞谢谢啦!
qqzone项目完整版
- 数据库连接池:Druid跟学韩顺平老师的项目,这里没按照视频老师的方式配置,所以Service中有些代码的地方是按照自己的实际情况写的。
- 源码和素材可以去看视频底下有获取方式
项目结构
界面展示
用户的登录
还没有账号,这个功能暂时没有实现
主界面
进入朋友空间
当前登录用户是Jim,现在点击左侧好友列表,进入kate的空间,可以查看kate发表的日志
针对朋友发表的日志进行回复
在朋友的日志下,可以进行回复
主人回复
在当前登录的用户日志列表中,鼠标靠近别人的回复,可以直接添加主人回复,这里是用form表单实现,因为超链接跳转隐藏form表单的方法不是太会,就用了这种方式,大家可以自行尝试
添加日志
点击日志列表右上角的“发表新日志,即可跳转发表新日志”
数据库相关sql
CREATE DATABASE `qqzonedb2` CHAR SET utf8;
USE qqzonedb2;
`t_friend``t_friend`
CREATE TABLE `t_user_basic` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`loginId` VARCHAR(20) NOT NULL,
`nickName` VARCHAR(50) NOT NULL,
`pwd` VARCHAR(20) NOT NULL,
`headImg` VARCHAR(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `loginId` (`loginId`)
) ENGINE=INNODB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
INSERT INTO `t_user_basic`(`id`,`loginId`,`nickName`,`pwd`,`headImg`) VALUES (1,'u001','jim','ok','h1.jpeg'),(2,'u002','tom','ok','h2.jpeg'),(3,'u003','kate','ok','h3.jpeg'),(4,'u004','lucy','ok','h4.jpeg'),(5,'u005','张三丰','ok','h5.jpeg');
CREATE TABLE `t_friend` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`uid` INT(11) DEFAULT NULL,
`fid` INT(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_friend_basic_uid` (`uid`),
KEY `FK_friend_basic_fid` (`fid`),
CONSTRAINT `FK_friend_basic_fid` FOREIGN KEY (`fid`) REFERENCES `t_user_basic` (`id`),
CONSTRAINT `FK_friend_basic_uid` FOREIGN KEY (`uid`) REFERENCES `t_user_basic` (`id`)
) ENGINE=INNODB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
SELECT * FROM t_friend;
INSERT INTO `t_friend`(`id`,`uid`,`fid`) VALUES (1,1,2),(2,1,3),(3,1,4),(4,1,5),(5,2,3),(6,2,1),(7,2,4),(8,3,1),(9,3,2),(10,5,1);
CREATE TABLE `t_topic` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`title` VARCHAR(100) NOT NULL,
`content` VARCHAR(500) NOT NULL,
`topicDate` DATETIME NOT NULL,
`author` INT(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_topic_basic` (`author`),
CONSTRAINT `FK_topic_basic` FOREIGN KEY (`author`) REFERENCES `t_user_basic` (`id`)
) ENGINE=INNODB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
INSERT INTO `t_topic`(`id`,`title`,`content`,`topicDate`,`author`)
VALUES
(3,'我的空间开通了,先做自我介绍!','大家好,我是铁锤妹妹!','2021-06-18 11:25:30',2),
(8,'我的空间','我的空间','2021-07-14 16:16:40',1);
CREATE TABLE `t_reply` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`content` VARCHAR(500) NOT NULL,
`replyDate` DATETIME NOT NULL,
`author` INT(11) NOT NULL,
`topic` INT(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_reply_basic` (`author`),
KEY `FK_reply_topic` (`topic`),
CONSTRAINT `FK_reply_basic` FOREIGN KEY (`author`) REFERENCES `t_user_basic` (`id`),
CONSTRAINT `FK_reply_topic` FOREIGN KEY (`topic`) REFERENCES `t_topic` (`id`)
) ENGINE=INNODB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
INSERT INTO `t_reply`(`id`,`content`,`replyDate`,`author`,`topic`) VALUES (3,'回复','2021-07-14 16:16:54',2,8),(4,'回复2222','2021-07-14 16:17:11',3,8),(5,'这里是第三个回复','2021-07-14 16:30:49',1,8);
CREATE TABLE `t_host_reply` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`content` VARCHAR(500) NOT NULL,
`hostReplyDate` DATETIME NOT NULL,
`author` INT(11) NOT NULL,
`reply` INT(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_host_basic` (`author`),
KEY `FK_host_reply` (`reply`),
CONSTRAINT `FK_host_basic` FOREIGN KEY (`author`) REFERENCES `t_user_basic` (`id`),
CONSTRAINT `FK_host_reply` FOREIGN KEY (`reply`) REFERENCES `t_reply` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `t_user_detail` (
`id` INT(11) NOT NULL,
`realName` VARCHAR(20) DEFAULT NULL,
`tel` VARCHAR(11) DEFAULT NULL,
`email` VARCHAR(30) DEFAULT NULL,
`birth` DATETIME DEFAULT NULL,
`star` VARCHAR(10) DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_detail_basic` FOREIGN KEY (`id`) REFERENCES `t_user_basic` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
相关代码
具体相关代码可以直接fork上面的项目,这里只放出一些关键核心的java代码
BasicDAO 调用德鲁伊连接池,完成sql执行
因为表中很多是多表连接,这里就改造了一些方法,具体多表连接的方式可以参考韩老师发的连接,当然这里我自己也有用到,可以参考这篇博主的文章
DButils数据库连接池多表关联处理方式
package com.atguigu.qqzone.basic;
import com.atguigu.qqzone.exceptions.DAOException;
import com.atguigu.qqzone.pojo.HostReply;
import com.atguigu.qqzone.pojo.Reply;
import com.atguigu.qqzone.pojo.Topic;
import com.atguigu.qqzone.pojo.UserBasic;
import com.atguigu.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @ClassName: BasicDAO
* @Description:
* @Author: wty
* @Date: 2022/12/5
*/
public class BasicDAO<T> {
private QueryRunner queryRunner = new QueryRunner();
/**
* @param
* @return int
* @description //TODO
* @param: sqlStr
* @param: params
* @date 2022/12/4 17:53
* @author wty
**/
public int dml(String sqlStr, Object... params) {
Connection connection = null;
int affectedrows = 0;
try {
connection = DruidUtils.getConnection();
affectedrows = queryRunner.update(connection, sqlStr, params);
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException("BasicDAO dml出错!");
} finally {
DruidUtils.close(null, null, connection);
}
return affectedrows;
}
/**
* @return java.util.List<T>
* @description 返回多行结果
* @param: sqlStr
* @param: clazz
* @param: params
* @date 2022/12/4 17:59
* @author wty
**/
public List<T> queryMulti(String sqlStr, Class<T> clazz, Object... params) {
Connection connection = null;
List<T> list = null;
try {
connection = DruidUtils.getConnection();
list = queryRunner.query(connection, sqlStr, new BeanListHandler<T>(clazz), params);
} catch (Exception e) {
e.printStackTrace();
throw new DAOException("BasicDAO queryMulti出错!");
} finally {
DruidUtils.close(null, null, connection);
}
return list;
}
/**
* @param
* @return T
* @description 返回单行多列数据
* @param: sqlStr
* @param: clazz
* @param: params
* @date 2022/12/4 18:01
* @author wty
**/
public T querySingle(String sqlStr, Class<T> clazz, Object... params) {
Connection connection = null;
T t = null;
try {
connection = DruidUtils.getConnection();
t = queryRunner.query(connection, sqlStr, new BeanHandler<T>(clazz), params);
} catch (Exception e) {
e.printStackTrace();
throw new DAOException("BasicDAO querySingle出错!");
} finally {
DruidUtils.close(null, null, connection);
}
return t;
}
public Object queryScalar(String sqlStr, Object... params) {
Connection connection = null;
Object o = null;
try {
connection = DruidUtils.getConnection();
o = queryRunner.query(connection, sqlStr, new ScalarHandler(), params);
} catch (Exception e) {
e.printStackTrace();
throw new DAOException("BasicDAO queryScalar出错!");
} finally {
DruidUtils.close(null, null, connection);
}
return o;
}
/**
* @return java.util.List<T>
* @description 返回多行结果--用于多表查询
* @param: sqlStr
* @param: clazz
* @param: params
* @date 2022/12/4 17:59
* @author wty
**/
public List<Topic> queryMultiListAndMap(String sqlStr, List<Topic> list, Object... params) {
Connection connection = null;
try {
connection = DruidUtils.getConnection();
MapListHandler mapListHandler = new MapListHandler();
List<Map<String, Object>> query = queryRunner.query(connection, sqlStr, mapListHandler, params);
for (Map<String, Object> map : query) {
UserBasic userBasic = new UserBasic();
Topic topic = new Topic();
userBasic.setId((Integer) map.get("author"));
topic.setAuthor(userBasic);
topic.setId((Integer) map.get("id"));
topic.setTitle((String) map.get("title"));
topic.setContent((String) map.get("content"));
topic.setTopicDate((java.util.Date) map.get("topicDate"));
list.add(topic);
}
} catch (Exception e) {
e.printStackTrace();
throw new DAOException("BasicDAO queryMultiListAndMap出错!");
} finally {
DruidUtils.close(null, null, connection);
}
return list;
}
/**
* @param
* @return java.util.List<com.atguigu.qqzone.pojo.Topic>
* @description 返回多行结果--用于多表查询
* @param: sqlStr
* @param: list
* @param: params
* @date 2022/12/10 17:24
* @author wty
**/
public List<Reply> queryMultiListAndMapReply(String sqlStr, List<Reply> list, Object... params) {
Connection connection = null;
try {
connection = DruidUtils.getConnection();
MapListHandler mapListHandler = new MapListHandler();
List<Map<String, Object>> query = queryRunner.query(connection, sqlStr, mapListHandler, params);
for (Map<String, Object> map : query) {
Topic topic = new Topic();
Reply reply = new Reply();
UserBasic userBasic = new UserBasic();
topic.setId((Integer) map.get("topic"));
userBasic.setId((Integer) map.get("author"));
reply.setTopic(topic);
reply.setAuthor(userBasic);
reply.setId((Integer) map.get("id"));
reply.setContent((String) map.get("content"));
reply.setReplyDate((java.util.Date) map.get("replyDate"));
list.add(reply);
}
} catch (Exception e) {
e.printStackTrace();
throw new DAOException("BasicDAO queryMultiListAndMapReply出错!");
} finally {
DruidUtils.close(null, null, connection);
}
return list;
}
/**
* @param
* @return T
* @description 返回单行记录,用于Topic
* @param: sqlStr
* @param: clazz
* @param: params
* @date 2022/12/10 17:43
* @author wty
**/
public Topic querySingleTopic(String sqlStr, Object... params) {
Connection connection = null;
Topic topic = new Topic();
try {
connection = DruidUtils.getConnection();
MapListHandler mapListHandler = new MapListHandler();
List<Map<String, Object>> map = queryRunner.query(connection, sqlStr, mapListHandler, params);
if (map.size() == 0) {
return null;
}
UserBasic userBasic = new UserBasic();
userBasic.setId((Integer) map.get(0).get("author"));
topic.setAuthor(userBasic);
topic.setId((Integer) map.get(0).get("id"));
topic.setTitle((String) map.get(0).get("title"));
topic.setContent((String) map.get(0).get("content"));
topic.setTopicDate((java.util.Date) map.get(0).get("topicDate"));
} catch (Exception e) {
e.printStackTrace();
throw new DAOException("BasicDAO querySingleTopic出错!");
} finally {
DruidUtils.close(null, null, connection);
}
return topic;
}
/**
* @param
* @return T
* @description 单行查询--HostReply
* @param: sqlStr
* @param: clazz
* @param: params
* @date 2022/12/10 18:05
* @author wty
**/
public HostReply querySingleHostReply(String sqlStr, Object... params) {
Connection connection = null;
HostReply hostReply = new HostReply();
try {
connection = DruidUtils.getConnection();
MapListHandler mapListHandler = new MapListHandler();
List<Map<String, Object>> map = queryRunner.query(connection, sqlStr, mapListHandler, params);
if (map.size() == 0) {
return null;
}
UserBasic userBasic = new UserBasic();
Reply reply = new Reply();
userBasic.setId((Integer) map.get(0).get("author"));
hostReply.setAuthor(userBasic);
reply.setId((Integer) map.get(0).get("reply"));
hostReply.setReply(reply);
hostReply.setId((Integer) map.get(0).get("id"));
hostReply.setContent((String) map.get(0).get("content"));
hostReply.setHostReplyDate((java.util.Date) map.get(0).get("hostReplyDate"));
} catch (Exception e) {
e.printStackTrace();
throw new DAOException("BasicDAO querySingle出错!");
} finally {
DruidUtils.close(null, null, connection);
}
return hostReply;
}
/**
* @param
* @return com.atguigu.qqzone.pojo.Topic
* @description 返回单行记录,用于Reply
* @param: sqlStr
* @param: params
* @date 2022/12/11 23:53
* @author wty
**/
public Reply querySingleReply(String sqlStr, Object... params) {
Connection connection = null;
Reply reply = new Reply();
try {
connection = DruidUtils.getConnection();
MapListHandler mapListHandler = new MapListHandler();
List<Map<String, Object>> map = queryRunner.query(connection, sqlStr, mapListHandler, params);
if (map.size() == 0) {
return null;
}
UserBasic userBasic = new UserBasic();
Topic topic = new Topic();
HostReply hostReply = new HostReply();
userBasic.setId((Integer) map.get(0).get("author"));
reply.setAuthor(userBasic);
topic.setId((Integer) map.get(0).get("topic"));
reply.setTopic(topic);
hostReply.setId((Integer) map.get(0).get("id"));
reply.setHostReply(hostReply);
reply.setId((Integer) map.get(0).get("id"));
reply.setContent((String) map.get(0).get("content"));
reply.setReplyDate((java.util.Date) map.get(0).get("replyDate"));
} catch (Exception e) {
e.printStackTrace();
throw new DAOException("BasicDAO querySingleTopic出错!");
} finally {
DruidUtils.close(null, null, connection);
}
return reply;
}
}
HostReplyController 主人回复
package com.atguigu.qqzone.controller;
import com.atguigu.qqzone.pojo.HostReply;
import com.atguigu.qqzone.pojo.Reply;
import com.atguigu.qqzone.pojo.Topic;
import com.atguigu.qqzone.pojo.UserBasic;
import com.atguigu.qqzone.service.impl.HostReplyServiceImpl;
import javax.servlet.http.HttpSession;
import java.util.Date;
/**
* @ClassName: HostReplyController
* @Description:
* @Author: wty
* @Date: 2022/12/11
*/
public class HostReplyController {
private HostReplyServiceImpl hostReplyServiceImpl;
public String addHostReply(String content, Integer replyId, HttpSession session) {
UserBasic author = (UserBasic) session.getAttribute("userBasic");
Reply reply = new Reply(replyId);
HostReply hostReply = new HostReply(content, new Date(), author, reply);
hostReplyServiceImpl.addHostReply(hostReply);
// 重定向返回日志详情
Topic topic = (Topic) session.getAttribute("topic");
return "redirect:topic.do?operate=topicDetail&id=" + topic.getId();
}
}
ReplyController 回复
package com.atguigu.qqzone.controller;
import com.atguigu.qqzone.pojo.Reply;
import com.atguigu.qqzone.pojo.Topic;
import com.atguigu.qqzone.pojo.UserBasic;
import com.atguigu.qqzone.service.impl.ReplyServiceImpl;
import javax.servlet.http.HttpSession;
import java.util.Date;
/**
* @ClassName: ReplyController
* @Description:
* @Author: wty
* @Date: 2022/12/10
*/
public class ReplyController {
private ReplyServiceImpl replyServiceImpl;
public String addReply(String content, Integer topicId, HttpSession session) {
// 当前作者
UserBasic author = (UserBasic) session.getAttribute("userBasic");
Reply reply = new Reply(content, new Date(), author, new Topic(topicId));
replyServiceImpl.addReply(reply);
return "redirect:topic.do?operate=topicDetail&id=" + topicId;
}
public String delReply(Integer replyId, Integer topicId) {
replyServiceImpl.delReply(replyId);
return "redirect:topic.do?operate=topicDetail&id=" + topicId;
}
}
TopicController 日志
package com.atguigu.qqzone.controller;
import com.atguigu.qqzone.pojo.Reply;
import com.atguigu.qqzone.pojo.Topic;
import com.atguigu.qqzone.pojo.UserBasic;
import com.atguigu.qqzone.service.impl.ReplyServiceImpl;
import com.atguigu.qqzone.service.impl.TopicServiceImpl;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
/**
* @ClassName: TopicController
* @Description:
* @Author: wty
* @Date: 2022/12/10
*/
public class TopicController {
private TopicServiceImpl topicServiceImpl;
public String topicDetail(Integer id, HttpSession session) {
Topic topic = topicServiceImpl.getTopicById(id);
//List<Reply> replyList = replyServiceImpl.getReplyListByTopicId(id);
//topic.setReplyList(replyList);
session.setAttribute("topic", topic);
return "frames/detail";
}
public String delTopic(Integer topicId) {
topicServiceImpl.delTopic(topicId);
return "redirect:topic.do?operate=getTopicList";
}
public String getTopicList(HttpSession session) {
// 从session中获取当前用户信息
UserBasic userBasic = (UserBasic) session.getAttribute("userBasic");
// 再次查询当前用户关联的所有日志
List<Topic> topicList = topicServiceImpl.getTopicList(userBasic);
// 设置关联的日志列表
/**
* <tr th:unless="${#lists.isEmpty(session.friend.topicList)}" th:each="topic : ${session.friend.topicList}">
*/
// 重新覆盖
userBasic.setTopicList(topicList);
session.setAttribute("friend", userBasic);
return "frames/main";
}
public String addTopic(String title, String content, HttpSession session) {
UserBasic userBasic = (UserBasic) session.getAttribute("userBasic");
Topic topic = new Topic(title, content, new Date(), new UserBasic(userBasic.getId()));
topicServiceImpl.addTopic(topic);
return "redirect:topic.do?operate=getTopicList";
}
}
UserController用户信息以及登录
package com.atguigu.qqzone.controller;
import com.atguigu.qqzone.pojo.Topic;
import com.atguigu.qqzone.pojo.UserBasic;
import com.atguigu.qqzone.service.impl.TopicServiceImpl;
import com.atguigu.qqzone.service.impl.UserBasicServiceImpl;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
* @ClassName: QqZoneController
* @Description:
* @Author: wty
* @Date: 2022/12/9
*/
public class UserController {
private UserBasicServiceImpl userBasicServiceImpl;
private TopicServiceImpl topicServiceImpl;
/**
* @param
* @return java.lang.String
* @description 注册
* @param: loginId
* @param: pwd
* @param: session
* @date 2022/12/9 18:29
* @author wty
**/
public String login(String loginId, String pwd, HttpSession session) {
UserBasic userBasic = userBasicServiceImpl.login(loginId, pwd);
if (null != userBasic) {
// 获取好友列表
List<UserBasic> friendList = userBasicServiceImpl.getFriendList(userBasic);
// 获取日志列表
List<Topic> topicList = topicServiceImpl.getTopicList(userBasic);
userBasic.setFriendList(friendList);
userBasic.setTopicList(topicList);
// userBasic这个key保存的是登录者的信息
session.setAttribute("userBasic", userBasic);
// friend这个key保存的是登录者的信息
session.setAttribute("friend", userBasic);
// 验证成功,跳转主界面
return "index";
} else {
// 验证失败,跳转注册界面
return "login";
}
}
public String friend(Integer id, HttpSession session) {
// 1.根据id获取指定的用户信息
UserBasic currendFriend = userBasicServiceImpl.getUserBasicById(id);
if (null != currendFriend) {
// 获取日志列表
List<Topic> topicList = topicServiceImpl.getTopicList(currendFriend);
currendFriend.setTopicList(topicList);
// friend这个key保存的是登录者的信息
session.setAttribute("friend", currendFriend);
// 验证成功,跳转主界面
return "index";
} else {
// 验证失败,跳转注册界面
return "login";
}
}
}
DAOException自定义异常
package com.atguigu.qqzone.exceptions;
/**
* @ClassName: 抛出异常整合
* @Description:
* @Author: wty
* @Date: 2022/12/9
*/
public class DAOException extends RuntimeException {
public DAOException(String msg) {
super(msg);
}
}
DispatcherServletException中央控制器自定义异常
package com.atguigu.qqzone.exceptions;
/**
* @ClassName: DispatcherServletException
* @Description:
* @Author: wty
* @Date: 2022/12/9
*/
public class DispatcherServletException extends RuntimeException {
public DispatcherServletException(String msg) {
super(msg);
}
}
HostReplyServiceImpl主人回复实现
package com.atguigu.qqzone.service.impl;
import com.atguigu.qqzone.dao.HostReplyDAO;
import com.atguigu.qqzone.pojo.HostReply;
import com.atguigu.qqzone.service.HostReplyService;
import java.util.List;
/**
* @ClassName: HostReplyServiceImpl
* @Description:
* @Author: wty
* @Date: 2022/12/9
*/
public class HostReplyServiceImpl implements HostReplyService {
private HostReplyDAO hostReplyDAO;
@Override
public HostReply getHostReplyByReplyId(Integer replyId) {
String sqlStr = "SELECT * FROM qqzonedb2.`t_host_reply` a WHERE a.`reply` = ?";
return hostReplyDAO.querySingleHostReply(sqlStr, replyId);
}
@Override
public void addHostReply(HostReply hostReply) {
String sqlStr = "INSERT INTO qqzonedb2.`t_host_reply` VALUES (0,?,?,?,?)";
hostReplyDAO.dml(sqlStr, hostReply.getContent(), hostReply.getHostReplyDate(), hostReply.getAuthor().getId(), hostReply.getReply().getId());
}
@Override
public void delHostReply(Integer id) {
String sqlStr = "DELETE FROM qqzonedb2.`t_host_reply` WHERE id = ?";
hostReplyDAO.dml(sqlStr, id);
}
}
ReplyServiceImpl 回复实现
package com.atguigu.qqzone.service.impl;
import com.atguigu.qqzone.dao.HostReplyDAO;
import com.atguigu.qqzone.dao.ReplyDAO;
import com.atguigu.qqzone.pojo.HostReply;
import com.atguigu.qqzone.pojo.Reply;
import com.atguigu.qqzone.pojo.Topic;
import com.atguigu.qqzone.pojo.UserBasic;
import com.atguigu.qqzone.service.ReplyService;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: ReplyServiceImpl
* @Description:
* @Author: wty
* @Date: 2022/12/9
*/
public class ReplyServiceImpl implements ReplyService {
private ReplyDAO replyDAO;
/**
* 调用hostReplyServiceImpl的实现
*/
private HostReplyServiceImpl hostReplyServiceImpl;
private UserBasicServiceImpl userBasicServiceImpl;
@Override
public List<Reply> getReplyListByTopicId(Integer topicId) {
String sqlStr = "SELECT a.* FROM qqzonedb2.`t_reply` a,qqzonedb2.`t_topic` b,qqzonedb2.`t_user_basic` c WHERE a.`topic` = b.`id` AND a.`author` = c.`id` AND a.`topic` = ?";
ArrayList<Reply> list = new ArrayList<>();
List<Reply> replyList = replyDAO.queryMultiListAndMapReply(sqlStr, list, topicId);
for (int i = 0; i < replyList.size(); i++) {
Reply reply = replyList.get(i);
// 1.将关联的作者设置进去
UserBasic author = userBasicServiceImpl.getUserBasicById(reply.getAuthor().getId());
reply.setAuthor(author);
// 2.将关联的hostReplyByReplyId设置
HostReply hostReplyByReplyId = hostReplyServiceImpl.getHostReplyByReplyId(reply.getId());
reply.setHostReply(hostReplyByReplyId);
}
return replyList;
}
@Override
public void addReply(Reply reply) {
String sqlStr = "INSERT INTO qqzonedb2.`t_reply` VALUES(0,?,?,?,?)";
replyDAO.dml(sqlStr, reply.getContent(), reply.getReplyDate(), reply.getAuthor().getId(), reply.getTopic().getId());
}
@Override
public void delReply(Integer id) {
String sqlStr = "";
// 1.根据id获取到reply
Reply reply = getReply(id);
// 2.如果reply有关联的hostreply那么需要删除hostreply
if (null != reply) {
HostReply hostReply = hostReplyServiceImpl.getHostReplyByReplyId(reply.getId());
if (null != hostReply) {
hostReplyServiceImpl.delHostReply(hostReply.getId());
}
// 3.最后再删除reply
sqlStr = "DELETE FROM qqzonedb2.`t_reply` WHERE id = ?";
replyDAO.dml(sqlStr, id);
}
}
@Override
public Reply getReply(Integer id) {
String sqlStr = "SELECT a.* FROM qqzonedb2.`t_reply` a WHERE a.id = ?";
return replyDAO.querySingleReply(sqlStr, id);
}
@Override
public void delReplyList(Topic topic) {
List<Reply> replyList = getReplyListByTopicId(topic.getId());
if (null != replyList) {
for (Reply reply : replyList) {
delReply(reply.getId());
}
}
}
}
TopicServiceImpl日志实现类
package com.atguigu.qqzone.service.impl;
import com.atguigu.qqzone.dao.TopicDAO;
import com.atguigu.qqzone.pojo.Reply;
import com.atguigu.qqzone.pojo.Topic;
import com.atguigu.qqzone.pojo.UserBasic;
import com.atguigu.qqzone.service.TopicService;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: TopicServiceImpl
* @Description:
* @Author: wty
* @Date: 2022/12/9
*/
public class TopicServiceImpl implements TopicService {
private TopicDAO topicDAO;
/**
* 引入ReplyServiceImpl
*/
private ReplyServiceImpl replyServiceImpl;
private UserBasicServiceImpl userBasicServiceImpl;
@Override
public List<Topic> getTopicList(UserBasic userBasic) {
String sqlStr = "SELECT a.* FROM qqzonedb2.`t_topic` a INNER JOIN qqzonedb2.`t_user_basic` b ON a.`author` = b.`id` AND a.`author` = ?";
ArrayList<Topic> list = new ArrayList<>();
return topicDAO.queryMultiListAndMap(sqlStr, list, userBasic.getId());
}
@Override
public void addTopic(Topic topic) {
String sqlStr = "INSERT INTO qqzonedb2.`t_topic` VALUES (0,?,?,?,?)";
topicDAO.dml(sqlStr, topic.getTitle(), topic.getContent(), topic.getTopicDate(), topic.getAuthor().getId());
}
@Override
public void delTopic(Topic topic) {
String sqlStr = "DELETE FROM qqzonedb2.`t_topic` WHERE id = ?";
topicDAO.dml(sqlStr, topic.getId());
}
@Override
public void delTopic(Integer id) {
Topic topic = getTopic(id);
if (null != topic) {
// 删除所有日志中的回复
replyServiceImpl.delReplyList(topic);
// 删除所有日志
delTopic(topic);
}
}
@Override
public Topic getTopic(Integer id) {
String sqlStr = "SELECT * FROM qqzonedb2.`t_topic` a WHERE a.`id` = ?";
Topic topic = topicDAO.querySingleTopic(sqlStr, id);
UserBasic author = topic.getAuthor();
author = userBasicServiceImpl.getUserBasicById(author.getId());
topic.setAuthor(author);
return topic;
}
@Override
public Topic getTopicById(Integer id) {
Topic topic = getTopic(id);
List<Reply> replyList = replyServiceImpl.getReplyListByTopicId(topic.getId());
topic.setReplyList(replyList);
return topic;
}
}
UserBasicServiceImpl用户功能实现类
package com.atguigu.qqzone.service.impl;
import com.atguigu.qqzone.dao.UserBasicDAO;
import com.atguigu.qqzone.pojo.UserBasic;
import com.atguigu.qqzone.service.UserBasicService;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: UserBasicServiceImpl
* @Description:
* @Author: wty
* @Date: 2022/12/9
*/
public class UserBasicServiceImpl implements UserBasicService {
private UserBasicDAO userBasicDAO;
@Override
public UserBasic getUserBasic(String loginId, String pwd) {
String sqlStr = "SELECT * FROM t_user_basic a WHERE a.loginId = ? AND a.pwd = ?";
return userBasicDAO.querySingle(sqlStr, UserBasic.class, loginId, pwd);
}
@Override
public List<UserBasic> getUserBasicList(UserBasic userBasic) {
String sqlStr = "SELECT a.fid AS id FROM t_friend a WHERE a.uid = ?";
return userBasicDAO.queryMulti(sqlStr, UserBasic.class, userBasic.getId());
}
@Override
public UserBasic getUserBasicById(Integer id) {
String sqlStr = "SELECT * FROM t_user_basic a WHERE a.id = ?";
return userBasicDAO.querySingle(sqlStr, UserBasic.class, id);
}
/**
* @param
* @return com.atguigu.qqzone.pojo.UserBasic
* @description 注册
* @param: loginId
* @param: pwd
* @date 2022/12/9 18:22
* @author wty
**/
public UserBasic login(String loginId, String pwd) {
UserBasic userBasic = getUserBasic(loginId, pwd);
return userBasic;
}
/**
* @param
* @return java.util.List<com.atguigu.qqzone.pojo.UserBasic>
* @description 获取好友列表
* @param: userBasic
* @date 2022/12/9 18:23
* @author wty
**/
public List<UserBasic> getFriendList(UserBasic userBasic) {
List<UserBasic> userBasicList = getUserBasicList(userBasic);
List<UserBasic> friendList = new ArrayList<>(userBasicList.size());
for (int i = 0; i < userBasicList.size(); i++) {
UserBasic friend = userBasicList.get(i);
friend = getUserBasicById(friend.getId());
friendList.add(friend);
}
return friendList;
}
}
DruidUtils德鲁伊连接池
package com.atguigu.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* @ClassName: DruidUtils
* @Description:
* @Author: wty
* @Date: 2022/12/5
*/
public class DruidUtils {
private static DataSource dataSource;
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
static {
Properties properties = new Properties();
try {
InputStream inputStream = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(inputStream);
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 获取链接
* @Author: wty
* @Date: 2022/12/4
*/
public static Connection getConnection() {
Connection connection = null;
try {
connection = threadLocal.get();
if (null == connection) {
connection = dataSource.getConnection();
threadLocal.set(connection);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return threadLocal.get();
}
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
//try {
//
// if (null != resultSet) {
// resultSet.close();
// }
// if (null != statement) {
// statement.close();
// }
// if (null != connection) {
// connection.close();
// }
//} catch (SQLException e) {
// throw new RuntimeException(e);
//}
}
public static void closeConnection() throws SQLException {
Connection connection = threadLocal.get();
if (null == connection) {
return;
}
if (!connection.isClosed()) {
connection.close();
threadLocal.set(null);
// 或者remove也可以
//threadLocal.remove();
}
}
}