2022-05-12 自己的项目二

项目名称:相宠博客系统

1、引言:

        随着计算机科学技术的迅速发展,越来越多的计算机应用成为人们生活或者工作中的一般部分,诸如QQ、微信这样非常火热社交软件也层出不穷,同时,也有很多软件应用逐渐从我们的生活中消失。但是,博客系统已经有很长一段时间在社交平台中都占有一席之地了,并且它将会持续的发展下去。

2、项目介绍:

        我的“相宠博客”系统是基于Tomcat、servlet的一个web博客系统,用户可以浏览博客中的历史文章,登录成功后可进行文章的增、删、改、查等操作。该项目采用了mvc设计模式,分为controller层、view层、dao层。dao层主要整合了jdbc,将数据库中的记录抽象化为Java中的实例对象,将表数据模型化对象。controller主要有servlet实现,可以接收用户的请求、控制页面的跳转,数据的传递,controller的数据传递:从dao层获取数据并将数据以request、session的方式传递给jsp页面,并通过请求转发、重定向跳转到目标jsp页面。view层主要由jsp实现,jsp主要负责页面的展示,业务处理都交给servlet去做,将各层功能区别开来,降低各层的耦合性,方便多人开发协作。

3、总体设计展示:

主界面


用户注册界面
用户登录界面
登录后首界面
展示文章详情界面
文章下评论展示
发表文章界面
登录人修改、删除自己发表的文章,并展示文章评论量、浏览量界面

4、核心代码

主界面:

 

    

            

                

  •                 

    ${article.title}

                    

    ${article.summary}

                    

                        作者:${article.user.nickname}

                        字符数:${article.letterTotal}

                        阅读量:${article.readTotal}

                        评论量:${article.commentTotal}

                        发表时间:

                        

                            编辑

                            删除

                        

                    

                

  •         

        

数据库增删改查部分代码

public ArrayList

selectAllArticles() {

    //将文章记录转成文章对象存放在list中

    ArrayList

articles = new ArrayList<>();

    //sql语句必须将所有article/user所有的劣质查询出来

    String sql = "SELECT article.*,user.id as uid,username,password,nickname,avatar,user.status as ustatus,user.addtime as uaddtime from article,user where article.userid = user.id and article.status=0 order by article.addtime desc";

    Connection connection = DBTools.getConnection();

    PreparedStatement ps = null;

    ResultSet rs = null;

    try {

        ps = connection.prepareStatement(sql);

        rs = ps.executeQuery();

        while (rs.next()) {

            //将记录列值转为article对象属性值

            Article article = new Article();

            article.setId(rs.getInt("id"));

            article.setTitle(rs.getString("title"));

            article.setSummary(rs.getString("summary"));

            article.setContent(rs.getString("content"));

            article.setReadTotal(rs.getInt("read_total"));

            article.setCommentTotal(rs.getInt("comment_total"));

            article.setLetterTotal(rs.getInt("letter_total"));

            article.setStatus(rs.getInt("status"));

            Timestamp time = rs.getTimestamp("addtime");

            Date addtime = new Date(time.getTime());

            article.setAddtime(addtime);

            //将结果中的user记录转为user对象

            User user = new User();

            user.setId(rs.getInt("uid"));

            user.setUsername(rs.getString("username"));

            user.setPassword(rs.getString("password"));

            user.setNickname(rs.getString("nickname"));

            user.setAvatar(rs.getString("avatar"));

            user.setStatus(rs.getInt("ustatus"));

            Timestamp utime = rs.getTimestamp("uaddtime");

            Date uaddtime = new Date(utime.getTime());

            user.setAddtime(uaddtime);

            article.setUser(user);

            //将文章对象添加到文章列表中

            articles.add(article);

        }

    } catch (SQLException throwables) {

        throwables.printStackTrace();

    } finally {

        DBTools.close(connection, ps, rs);

    }

    return articles;

}

public Article selectArticleById(String aid) {

    Article article = new Article();

    String sql = "SELECT article.*,user.id as uid,username,password,nickname,avatar,user.status as ustatus,user.addtime as uaddtime from article,user where article.userid = user.id and article.id = ?";

    Integer id = Integer.parseInt(aid);

    Connection connection = DBTools.getConnection();

    PreparedStatement ps = null;

    ResultSet rs = null;

    try {

        ps = connection.prepareStatement(sql);

        ps.setInt(1, id);

        rs = ps.executeQuery();

        while (rs.next()) {

            //将记录列值转为article对象属性值

            //将记录值转为article对象属性值

            article.setId(rs.getInt("id"));

            article.setTitle(rs.getString("title"));

            article.setSummary(rs.getString("summary"));

            article.setContent(rs.getString("content"));

            article.setReadTotal(rs.getInt("read_total"));

            article.setCommentTotal(rs.getInt("comment_total"));

            article.setLetterTotal(rs.getInt("letter_total"));

            article.setStatus(rs.getInt("status"));

            Timestamp time = rs.getTimestamp("addtime");

            Date addtime = new Date(time.getTime());

            article.setAddtime(addtime);

            //将结果中user记录转为user对象

            User user = new User();

            user.setId(rs.getInt("uid"));

            user.setUsername(rs.getString("username"));

            user.setPassword(rs.getString("password"));

            user.setNickname(rs.getString("nickname"));

            user.setAvatar(rs.getString("avatar"));

            user.setStatus(rs.getInt("ustatus"));

            Date uaddtime =new Date(time.getTime());

            user.setAddtime(uaddtime);

            article.setUser(user);

        }

    } catch (SQLException throwables) {

        throwables.printStackTrace();

    }

    String sql1 = "update article set read_total=read_total+1 where id = ?";

    Connection connection1 = DBTools.getConnection();

    PreparedStatement ps1 = null;

    try {

        ps1 = connection1.prepareStatement(sql1);

        ps1.setInt(1,id);

        ps1.executeUpdate();

    } catch (SQLException throwables) {

        throwables.printStackTrace();

    }

    finally {

        DBTools.close(connection,ps,null);

        DBTools.close(connection1,ps1,null);

    }

    return article;

}

public void deleteArticleById(String aid) {

    String sql = "update article set status=1 where id = ?";

    Connection connection = DBTools.getConnection();

    PreparedStatement ps = null;

    try {

        ps = connection.prepareStatement(sql);

        Integer id = Integer.parseInt(aid);

        ps.setInt(1,id);

        ps.executeUpdate();

    } catch (SQLException throwables) {

        throwables.printStackTrace();

    } finally {

        DBTools.close(connection,ps,null);

    }

}

public void updateArticleleById(String title, String summary, String content, String id) {


    String sql = "update article set title=?,summary=?,content=? where id=?";

    Connection connection = DBTools.getConnection();

    PreparedStatement ps = null;

    try {

        ps = connection.prepareStatement(sql);

        ps.setString(1,title);

        ps.setString(2,summary);

        ps.setString(3,content);

        Integer aid = Integer.parseInt(id);

        ps.setInt(4,aid);

        ps.executeUpdate();

    } catch (SQLException throwables) {

        throwables.printStackTrace();

    } finally {

        DBTools.close(connection,ps,null);

 }

}

创建对象:

//user类

public class User {

    private Integer id;

    private String username;

    private String password;

    private String nickname;

    private String avatar;

    private Integer status;

    private Date addtime;


//comment类

public class Comment {

    private Integer id;

    private String content;

    private User user;

    private Article article;

    private Integer status;

    private Date addtime;


//article类

public class Article {

    private Integer id;

    private String title;

    private String summary;

    private String content;

    private Integer readTotal;

    private Integer commentTotal;

    private Integer letterTotal;


    private User user;

    private Integer status;

    private Date addtime;

程序

@WebServlet("/main")

public class MainServlet extends HttpServlet {

    ArticleDao articleDao = new ArticleDao();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //调用articleDao获取文章

        ArrayList

articles = articleDao.selectAllArticles();

        //文中列表放入请求作用域中

        request.setAttribute("articles",articles);

        //请求转发到main.jsp

        request.getRequestDispatcher("/main.jsp").forward(request,response);

    }

}

记录完毕,继续努力!

你可能感兴趣的:(2022-05-12 自己的项目二)