我爱写作--我的博客项目总结

写过一个简易的博客项目,在这里我总结一下这个博客项目的思想。

登录功能

首先写个前端页面来实现登录页面,从登录页面点击登录,定位到都后端的功能模块,对前端的登录请求加以验证:
1.根据前端请求的信息:用户名和密码
2.在后端的数据库里面找有没有用户名,没有用户名–返回提示信息;如果有这个用户名,继续查询前端发来的密码信息和数据库里面密码信息是否想等;如果不相等,那就登录失败,如果相等的话,就登录成功,可以访问相应的资源。并且创建当前用户的session。
在此过程中,实现一个过滤器接口,自己做个过滤器,对当前路径定位,如已经是内部资源路径,那就可以直接向下访问,如果不是内部资源路径,那么就要重定向到登录页面。

数据存储

数据存储是利用初始化数据存储进去的。在使用数据的时候需要建立数据库
使用的技术使数据库JDBC连接。下分为面来讲一下这个JDBC连接:
分为下面几步:
1.注册驱动
在项目里面使用的数据库连接池 DataSource
2.建立连接
Connection c=DS.getConnection();

3.创建执行MySQL语句的操作
有两个对象可用:preparedStatement和statement
前者是提前设置占位符的

4.执行MySQL语句(CRUD)
查询:executeQuery(String sql)或者executeQuery()
非查询操作(增删改):executeUpdate()或者executeUpdate(String sql)
5.返回结果集
查询的操作使用:resultSet()接收;
非查询操作有返回值,都是int类型
6.释放资源
依次释放ResultSet、Statement(或PreparedStatement)、Connection对象,释放顺序与创建顺序相反(类似“栈”结构)。并且connection是稀有资源,用完要马上释放。

新增博客

新增博客的主要思想就是,连接上数据库之后,在数据库中插入博客内容,使用mySQL语言,发生异常就要抛出
代码如下:

public static int insert(Article a) {
    
    
        Connection c = null;
        PreparedStatement ps = null;
        try{
    
    
            c = DBUtil.getConnection();
            String sql = "insert into article(title, content, user_id)" +
                    " values (?, ?, ?)";
            ps = c.prepareStatement(sql);
            //替换占位符
            ps.setString(1, a.getTitle());
            ps.setString(2, a.getContent());
            ps.setInt(3, a.getUserId());
            return ps.executeUpdate();
        }catch (Exception e){
    
    
            throw new AppException("ART005", "新增文章操作出错", e);
        }finally {
    
    
            DBUtil.close(c, ps);
        }
    }

修改博客

主要思想就是,连接上数据库之后,在数据库中修改博客内容,使用mySQL语言,代码如下:

public static int update(Article a) {
    
    
        Connection c = null;
        PreparedStatement ps = null;
        try{
    
    
            c = DBUtil.getConnection();
            String sql = "update article set title=?, content=? where id=?";
            ps = c.prepareStatement(sql);
            //设置占位符
            ps.setString(1, a.getTitle());
            ps.setString(2, a.getContent());
            ps.setInt(3, a.getId());
            return ps.executeUpdate();
        }catch (Exception e){
    
    
            throw new AppException("ART007", "修改文章操作出错", e);
        }finally {
    
    
            DBUtil.close(c, ps);
        }
    }

删除博客

删除博客,就是依靠数据库连接,将数据库中的文章,按照前端传过来的
删除:

public static int delete(String[] split) {
    
    
        Connection c = null;
        PreparedStatement ps = null;
        try{
    
    
            c = DBUtil.getConnection();
            StringBuilder sql = new StringBuilder("delete from article where id in (");
            for(int i=0; i<split.length; i++){
    
    
                if(i != 0)
                    sql.append(",");
                sql.append("?");
            }
            sql.append(")");
            ps = c.prepareStatement(sql.toString());
            //设置占位符的值
            for(int i=0; i<split.length; i++){
    
    
                ps.setInt(i+1, Integer.parseInt(split[i]));
            }
            return ps.executeUpdate();
        }catch (Exception e){
    
    
            throw new AppException("ART004", "文章删除操作出错", e);
        }finally {
    
    
            DBUtil.close(c, ps);
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_43815275/article/details/114888867