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

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

时间:2023-09-30 22:55:30浏览次数:39  
标签:return SpringBoot -- id int 报错 import public String

代码

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>

标签:return,SpringBoot,--,id,int,报错,import,public,String
From: https://www.cnblogs.com/liuzijin/p/17738358.html

相关文章

  • 每日总结
    今日收获背单词!!我真的服了,每天不爆出来几个错就不是电脑啦~~~~技术的进步!明天预计预计明后两天将erp的基本框架搭好,然后就开始进行动态的设计和开发(压力还是蛮大的!!!)背单词必不可少的啦!......
  • The name org.freedesktop.secrets was not provided by any .service files 报错问
    在搭建cicddocker靶场的时候出现这个问题,由于是第一次遇到,就想记录下来 通过各种搜索资料搜索发现只要安装如下包就没问题 1apt-yinstallgnome-keyring 这样做之后,一切都可以这样就可以了 ......
  • 9月阅读笔记1
    这本书强调了编程不仅仅是写出可以工作的代码,而是一门艺术和科学的结合。以下是一些我从书中学到的原则导向的编程观点:持续学习:编程是不断演进的领域,要不断学习新技术和方法,以保持竞争力。做好基础工作:仔细设计、文档化和测试代码是成功的关键。不要急于编写代码,要先规划和......
  • [CF1654F] Minimal String Xoration
    MinimalStringXoration有点智慧但不是特别智慧反正是我达不到的智慧。打表可以看出长度为\(2^x\)的\(i\oplusk\)出现次数为\(2^{n-k}\)。进一步发现,设\(f(k,x)\)当前选取k时,数列前\(2^k\)的下标。则\(f(k,x)=f(k,x-1)+f(k\oplus{2^{x-1}},x-1)\)因为对于\(......
  • 9月阅读笔记2
    本书提供了关于软件开发哲学的深刻洞察。以下是一些我认为特别重要的思想:机械式编码不足够:简单地编写代码是不够的,需要思考和规划。解决问题的过程涉及到设计、抽象、模块化和理解问题的本质。了解领域:了解解决问题的领域非常重要。软件开发者应该主动与领域专家合作,以便更......
  • 《看了受制了》第三十一天,7道题,合计164道题
    2023年9月30日Acwing5266队列题目大意问你,n加在三个数上,看最后三个数能不能相等。题目理解先把三个数都变成最大的,然后最后的n如果是3的倍数且大于等于0即可代码实现voidsolve(){lln,a,b,c;cin>>a>>b>>c>>n;llk=max(a,max(b,c));......
  • 9月读书笔记2
    正交性,设计自足的软件,让软件的各个部分不会过度互相依赖,提高生产效率并且降低风险。曳光代码,在不完全了解用户需求的情况下,我们可以向初始框架中添加曳光代码,虽然并不能百分百命中,但其代码得以保存复用,这样的代码往往简约但复用率高。构建原型,这与本学期所学的uml建模类似,用非代码......
  • Ubuntu 16.04 上安装 OrientDB!
    这两种模型在如何处理(存储)数据的方面存在着巨大的差异。关系数据库管理系统在关系模型中(如MySQL,或者其分支MariaDB),一个数据库是一个表的集合,其中每个表包含一个或多个以列组织的数据分类。数据库的每行包含一个唯一的数据实例,其分类由列定义。举个例子,想象一个包含客户的表。......
  • 苍穹外卖-第一章项目介绍
    1.苍穹外卖项目介绍1.1项目介绍1)管理端功能员工登录/退出,员工信息管理,分类管理,菜品管理,套餐管理,菜品口味管理,订单管理,数据统计,来单提醒。2)用户端功能微信登录,收件人地址管理,用户历史订单查询,菜品规格查询,购物车功能,下单,支付、分类......
  • Educational Codeforces Round 155 (Rated for Div
    B.ChipsontheBoard题解:贪心显然我们可以把题意转化为:对于任意一个\((i,j)\),我们可以花费\(a_{i,j}\)的代价占据第\(i\)行和第\(j\)列,求占据所有格子的最小代价考虑两种情况:在每一行选一个格子在每一列选一个格子贪心选即可intn,a[N],b[N];voidsolve......