SpringBoot框架大晚上报错404--我的路径问题(附上SpringBoot MVC管理系统的简单具体代码)

发布时间 2023-09-30 22:54:08作者: yesyes1

代码

application.yml

spring:
  web:
    resources:
      static-locations: classpath:/static/,classpath:/templates/
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
    username: root
    password: 20214063
    driver-class-name: com.mysql.cj.jdbc.Driver
    #???????get?post???
    #?????????????????????????
  mvc:
    hiddenmethod:
      filter:
        enabled: true
  devtools:
    restart:
      enabled: true #???????
  freemarker:
    cache: false #??????????????
mybatis:
  configuration:
    map-underscore-to-camel-case: true #???????
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl   # ??SQL??

User.java

package com.example.pojo;

public class User {
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getJia() {
        return jia;
    }

    public void setJia(String jia) {
        this.jia = jia;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", jia='" + jia + '\'' +
                '}';
    }

    public int id;
    public String name;
    public int age;
    public String jia;
}

UserMapper.java

package com.example.mapper;

import com.example.pojo.User;
import com.example.pojo.re;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    @Select("select * from student")
    public List<User> findAll();

    @Select("select * from student where id=#{id}")
    public User queryById(int id);

    @Insert("insert into student(name,age,jia) values(#{name},#{age},#{jia})")
    public int addUser(User user);

    @Delete("delete from student where id=#{id}")
    public int deleteUser(int id);

    @Update("update student set name=#{name},age=#{age},jia=#{jia} where id=#{id}")
    public int updateUser(User user);

    @Insert("insert into re values(#{id},MD5(#{password}))")
    public int addRe(re r);

    @Select("select id,password from re where id='33'")
    public List<re> selectRe();

    @Select("select password from re where id=#{id}")
    public String geti(int id);
}

UserService.java

package com.example.service;

import com.example.pojo.User;
import com.example.pojo.re;

import java.util.List;

public interface UserService {
    List<User> findAll();

    User queryById(int id);

    int addUser(User user);
    int deleteUser(int id);
    int updateUser(User user);

    int addRe(re r);

    List<re> selectRe();

    String geti(int id);
}

UserServiceImpl.java

package com.example.service.impl;

import com.example.mapper.UserMapper;
import com.example.pojo.User;
import com.example.pojo.re;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    public UserMapper userMapper;


    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public User queryById(int id) {
        return userMapper.queryById(id);
    }

    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public int deleteUser(int id) {
        return userMapper.deleteUser(id);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int addRe(re r) {
        return userMapper.addRe(r);
    }

    @Override
    public List<re> selectRe() {
        return userMapper.selectRe();
    }

    @Override
    public String geti(int id) {
        return userMapper.geti(id);
    }
}

UserController.java

package com.example.controller;


import com.example.pojo.User;
import com.example.pojo.re;
import com.example.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String checkAll(Model model){
        List<User> list=userService.findAll();
        model.addAttribute("list",list);
        System.out.println("显示到index.html页面");
        return "html/index";
    }

    //新增数据
    @PostMapping("/add")
    public String addIt(User user){
        userService.addUser(user);
        return "redirect:/index";
    }

    //删除数据
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id, HttpServletResponse servletResponse){
        userService.deleteUser(id);
        System.out.println("delete方法执行");
        return "redirect:/index";
    }

    //根据id进行查找数据
    @GetMapping("/query/{id}")
    public String updatePage(Model model, @PathVariable int id){
        User users = userService.queryById(id);
        model.addAttribute("list",users);
        //表示跳转到modifie,html界面
        return "html/modifie";
    }

    @RequestMapping("/update")
    public String updateUser(Model model, User user, HttpServletRequest request) {
        System.out.println("进入到update方法");
        String id = request.getParameter("id");
        System.out.println("id="+id);
        User userById = userService.queryById(Integer.parseInt(id));
        System.out.println("id="+id);
        userService.updateUser(user);
        System.out.println(user);
        return "redirect:/index";
    }

    //用户注册测试
    @RequestMapping("/regist")
    public String regis(HttpServletRequest request){
        int id=Integer.parseInt(request.getParameter("id"));
        String password=request.getParameter("password");

        re r=new re(id,password);

        int m=userService.addRe(r);
        if(m!=0){
            System.out.println("success");
            return "redirect:/index";
        }else{
            System.out.println("fail");
            return "redirect:/index";
        }
    }

    @RequestMapping("/sele")
    public String sele(Model model){
        List<re> list=userService.selectRe();



        model.addAttribute("list",list);
        return "html/all";
    }


    @RequestMapping("/login")
    public String geti(HttpServletRequest request){
        int id=Integer.parseInt(request.getParameter("id"));
        String pass=userService.geti(id);

        System.out.println("pass:  "+pass);

        String password=request.getParameter("password");

        String password1= DigestUtils.md5DigestAsHex(password.getBytes());

        System.out.println("md5_password:  "+password1);

        if(pass.equals(password1)){
            System.out.println("登录成功!");
            return "redirect:/index";
        }else{
            System.out.println("登录失败!");
            return "redirect:/index";
        }
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo1</name>
    <description>demo1</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.2</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>3.0.2</version>
            <scope>test</scope>
        </dependency>
        <!--阿里数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.16</version>
        </dependency>

        <!--        获取页面session对象request对象response对象 HttpServletXXX jar包-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!--        json转换工具包-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>

<style>
    a{
        color: #ffffff;
    }
    h1{
        /*文字对齐*/
        text-align: center;
    }
</style>

<body>
<h1>学生信息列表</h1>
<!--table-striped:斑马线格式,table-bordered:带边框的表格,table-hover:鼠标悬停高亮的表格-->
<table class="table table-striped table-bordered table-hover text-center">
    <thead>
    <tr style="text-align:center">
        <!--        th标签定义html表格中的表头单元格-->
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>家乡</th>
        <th>操作</th>
    </tr>
    </thead>
    <!--tr标签定义html表格中的所有行-->
    <!--    遍历集合,如果被遍历的变量users为null或者不存在,则不会进行遍历,也不会报错-->
    <tr th:each="l:${list}" align="center">
        <!--        td标签定义html表格中的标准单元格-->
        <td th:text="${l.getId()}"></td>
        <td th:text="${l.getName()}"></td>
        <td th:text="${l.getAge()}"></td>
        <td th:text="${l.getJia()}"></td>
        <td>
            <!--         a标签用来定义超链接 href表示超链接-->
            <a class="btn btn-primary" th:href="@{'/query/'+${l.getId()}}">更改</a>
            <a class="btn btn-danger" th:href="@{'/delete/'+${l.getId()}}">删除</a>
        </td>
    </tr>
</table>
<button class="btn btn-block" ><a href="/html/add.html">添加用户</a></button>

</body>

</html>