• 派大星的小站


    将之前写的博客项目改为SSM项目

    1.创建项目

    创建一个SpringBoot项目,添加如下依赖:
    在这里插入图片描述

    2.数据库实现及管理

    数据库使用之前的即可.
    在这里插入图片描述

    2.1使用MaBatis操作数据库

    2.1.1 UserMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.demo.model.UserMapper">
        <insert id="insert">
            insert into user(username,password) values(#{username},#{password});
        insert>
    
        <select id="selectById" resultType="com.example.demo.model.User">
            select * from user where userId = #{userId}
        select>
    
        <select id="selectByName" resultType="com.example.demo.model.User">
            select * from user where username = #{username}
        select>
    
    
    mapper>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    2.1.2 BlogMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.demo.model.BlogMapper">
    
        
        <insert id="insert">
            insert into blog values(null,#{title},#{blog_content},#{userId},now())
        insert>
    
        
        <select id="selectByBlogId" resultType="com.example.demo.model.Blog">
            select * from blog where blogId = #{blogId}
        select>
    
        
        <select id="selectByUserId" resultType="com.example.demo.model.Blog">
            select * from blog where userId = #{userId}
        select>
    
        
        <select id="selectAll" resultType="com.example.demo.model.Blog">
            select * from blog order by postTime desc
        select>
    
    
        
        <delete id="deleteByBlogId">
            delete from blog where blogId = #{blogId}
        delete>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    2.2 实体类

    2.2.1 Blog类

    @Data
    public class Blog {
        private int blogId;
        private String title;
        private String blog_content;
        private int userId;
        private Timestamp postTime;
    
        public String getPostTime() {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return simpleDateFormat.format(postTime);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.2.2 User类

    @Data
    public class User {
        private int userId = 0;
        private String username = "";
        private String password = "";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.3 接口类

    2.3.1 BlogMapper

    @Mapper
    public interface BlogMapper {
        int deleteByBlogId(Integer blogId);
        Blog selectByBlogId(Integer blogId);
        List<Blog> selectByUserId(Integer userId);
        int insert(Blog blog);
        List<Blog> selectAll();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.3.2 UserMapper

    @Mapper
    public interface BlogMapper {
        int deleteByBlogId(Integer blogId);
        Blog selectByBlogId(Integer blogId);
        List<Blog> selectByUserId(Integer userId);
        int insert(Blog blog);
        List<Blog> selectAll();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.4 调用类

    2.4.1 BlogService

    @Service
    public class BlogService {
    
        @Resource
        private BlogMapper blogMapper;
    
        public int insert(Blog blog){
            return blogMapper.insert(blog);
        }
    
        public List<Blog> selectByUserId(Integer userId){
            return blogMapper.selectByUserId(userId);
        }
    
        public Blog selectByBlogId(Integer BlogId){
            return blogMapper.selectByBlogId(BlogId);
        }
    
        public void deleteByBlogId(Integer BlogId){
            blogMapper.deleteByBlogId(BlogId);
        }
    
        public List<Blog> selectAll(){
            return blogMapper.selectAll();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    2.4.2 UserService

    @Service
    public class UserService {
        @Resource
        private UserMapper userMapper;
    
        public User selectById(Integer id){
            return userMapper.selectById(id);
        }
    
        public int insert(User user){
            return userMapper.insert(user);
        }
    
        public User selectByName(String username){
            return userMapper.selectByName(username);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3. 前后端约定

    1
    在这里插入图片描述2

    在这里插入图片描述3

    在这里插入图片描述
    4
    在这里插入图片描述
    5
    在这里插入图片描述6
    在这里插入图片描述
    7
    在这里插入图片描述
    8
    在这里插入图片描述

    4. 导入前端代码

    前端代码基本不用修改,直接引入即可

    5. 编写后端代码

    5.1 实现返回类

    在这里插入图片描述

    5.2 实现博客主页和详情页

    和原来一样,我们依旧可以通过判断是否有拥有参数来实现跳转到博客主页还是详情页

    在这里插入图片描述在这里插入图片描述在这里插入图片描述
    具体的可参考之前的博客: 博客系统

    5.3 实现登录页

    在这里插入图片描述
    在这里插入图片描述

    5.4 实现登录判断-拦截器

    5.4.1 实现自定义拦截器

    public class LoginIntercept implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            HttpSession session = request.getSession(false);
            if(session != null && session.getAttribute("user") != null){
                return true;
            }
            response.setStatus(401);
            response.sendRedirect("/login.html");
            return false;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5.4.2 将自定义拦截器加入到系统配置中

    @Configuration
    public class APPConfig implements WebMvcConfigurer {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new LoginIntercept()).
                    //所有路由都拦截
                    addPathPatterns("/**").
                    //不拦截这个后缀的所有文件
                    excludePathPatterns("/**/*.css").
                    excludePathPatterns("/**/*.js").
                    excludePathPatterns("/**/*.png").
                    excludePathPatterns("/**/*.jpg").
                    //登录页和注册页也不用拦截
                    excludePathPatterns("/**/login.html").
                    excludePathPatterns("/**/register.html").
                    excludePathPatterns("/**/login").
                    excludePathPatterns("/**/register");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    5.4.3 登录判断

    在这里插入图片描述在这里插入图片描述

    5.5 获取博客作者信息

    在这里插入图片描述
    在这里插入图片描述

    5.6 发布博客

    在这里插入图片描述在这里插入图片描述

    5.7 注销

    在这里插入图片描述

    5.8 删除博客

    在这里插入图片描述
    在这里插入图片描述

    5.9 注册

    在这里插入图片描述
    在这里插入图片描述

    6. 部署到云服务器

    6.1打包

    在这里插入图片描述

    6.2 找到该包

    在这里插入图片描述

    6.3 拖拽发送

    在这里插入图片描述

    6.4 运行

    java - jar 加包名即可运行
    在这里插入图片描述

  • 相关阅读:
    使用Babel将ES6代码转为ES5代码,从而在现有环境执行。
    优化CPU占用率及内存占用2
    SpringBoot整合Memcached缓存技术/JetCache缓存技术以及J2Cache缓存技术怎么在Spring Boot中配置
    web自动化测试(java+seleium)元素定位,全屏截图
    如何找回误删的文件呢?
    springboot redisTemplate.opsForValue().setIfAbsent返回null原理
    python 基于aiohttp的异步爬虫实战
    十天学完Vue学习总结
    ​软考-高级-系统架构设计师教程(清华第2版)【第18章 安全架构设计理论与实践(P648~690)-思维导图】​
    js事件循环机制
  • 原文地址:https://blog.csdn.net/qq_59689127/article/details/126924764