springboot实现查询学生

文章目录

数据库


前端 请求

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a href="/list">启动学生管理系统</a>
</body>
</html>

mybatis

java 复制代码
package com.example.demo.mapper;

import com.example.demo.po.StudentPO;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StudentMapper {

    @Select("select * from student")
    List<StudentPO> selectStudentAll();



}
java 复制代码
package com.example.demo.po;

import java.util.Objects;

public class StudentPO {
    Integer id;
    String name;
    String stu_id;
    String major;

    @Override
    public String toString() {
        return "StudentPO{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", stu_id='" + stu_id + '\'' +
                ", major='" + major + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        StudentPO studentPO = (StudentPO) o;
        return Objects.equals(id, studentPO.id) && Objects.equals(name, studentPO.name) && Objects.equals(stu_id, studentPO.stu_id) && Objects.equals(major, studentPO.major);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, stu_id, major);
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getStu_id() {
        return stu_id;
    }

    public void setStu_id(String stu_id) {
        this.stu_id = stu_id;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public StudentPO(Integer id, String name, String stu_id, String major) {
        this.id = id;
        this.name = name;
        this.stu_id = stu_id;
        this.major = major;
    }

    public StudentPO() {
    }
}
java 复制代码
package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com/example/demo/mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
java 复制代码
package com.example.demo.controller;

import com.example.demo.mapper.StudentMapper;
import com.example.demo.po.StudentPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.rmi.MarshalledObject;

@Controller
public class List {
    @Autowired
    StudentMapper studentMapper;


    @RequestMapping("/list")
    public ModelAndView listStudent(){
        ModelAndView modelAndView = new ModelAndView();
        java.util.List<StudentPO> studentPOS = studentMapper.selectStudentAll();


        modelAndView.addObject("students",studentPOS);
        modelAndView.setViewName("list");
        return  modelAndView;
    }


}

展示页面

html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>学生列表</h1>

<table border="1px">
    <tr>
        <td>姓名</td>
        <td>学号</td>
        <td>专业</td>


    </tr>

    <tr th:each="stu:${students}">
        <td th:text="${stu.getName()}"></td>
        <td th:text="${stu.getStu_id()}"></td>
        <td th:text="${stu.getMajor()}"></td>
    </tr>
</table>


</body>
</html>


xml 复制代码
spring.application.name=demo
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/yanyu
spring.datasource.username=root
spring.datasource.password=root
server.port=8081
#  默认  8080   ,更改  spring boot  项目 启动的端口号
相关推荐
Victor35626 分钟前
MySQL(163) 如何理解MySQL的隔离级别?
后端
Victor35631 分钟前
MySQL(164)如何设置MySQL的隔离级别?
后端
hqxstudying2 小时前
Java异常处理
java·开发语言·安全·异常
代码老y2 小时前
ASP.NET Core 高并发万字攻防战:架构设计、性能优化与生产实践
后端·性能优化·asp.net
愿你天黑有灯下雨有伞3 小时前
告别复杂配置!Spring Boot优雅集成百度OCR的终极方案
spring boot·百度·ocr
我命由我123454 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list
武子康7 小时前
Java-80 深入浅出 RPC Dubbo 动态服务降级:从雪崩防护到配置中心秒级生效
java·分布式·后端·spring·微服务·rpc·dubbo
舒一笑7 小时前
我的开源项目-PandaCoder迎来史诗级大更新啦
后端·程序员·intellij idea
@昵称不存在8 小时前
Flask input 和datalist结合
后端·python·flask
zhuyasen8 小时前
Go 分布式任务和定时任务太难?sasynq 让异步任务从未如此简单
后端·go