idea利用spring框架整合thymeleaf展现数据库数据

idea初步利用thymeleaf展现列表

上一篇文章简单展现自己写的列表;

这篇文章连接mysql数据库实现数据库数据展现

主要三个文件

controller指定html界面

复制代码
package com.example.appledemo.controller;

import com.example.appledemo.mapper.UserMapper;
import com.example.appledemo.pojo.User;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.List;

@Controller
public class TestController {
    @Resource
    UserMapper userMapper;

    @RequestMapping("/login")
    public String login(Model model){
        List<User> user = userMapper.findAll();
        model.addAttribute("user",user);
        return "login";
    }
}

mapper写数据库sql查询语句

复制代码
package com.example.appledemo.mapper;

import com.example.appledemo.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user")
    List<User> findAll();
}

pojo中的user写具体数据库中的表包含哪些字段(这部分最好的方式写出变量名字然后alt+insert自动生成getter和setter不容易出错)

复制代码
package com.example.appledemo.pojo;

import lombok.Getter;

@Getter
public class User {
    private Integer userId;
    private String userName;
    private String userPass;

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setUserPass(String userPass) {
        this.userPass = userPass;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", userPass='" + userPass + '\'' +
                '}';
    }
}

最后写个login.html展现数据

复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>springboot-thymeleaf demo</title>
</head>
<body>
<table border="1" width="1000">
    <tr th:each="item,eee: ${user}">
        <td th:text="${item.userId}"></td>
        <td th:text="${item.userName}"></td>
        <td th:text="${item.userPass}"></td>
    </tr>
</table>
</body>
</html>
</html>

给出文件列表:

最后给出运行结果:

相关推荐
知识的搬运工旺仔5 分钟前
CREATE INDEX CONCURRENTLY:线上建索引不阻塞 DML 的代价与坑
数据库·后端·sql
guo_wen_qiang31 分钟前
mysql中有哪些日志
数据库·mysql
苏渡苇1 小时前
Spring Insight 里如何把 Span 收成一条链路
spring boot·spring·spring cloud·系统监控·apm
达梦数据1 小时前
达梦数据复制软件DMDRS搭建部署示例:源数据库DM8到目标数据库DM8数据迁移
数据库
沪上企服通1 小时前
旧账系统的信创迁移工程:从 MySQL/Oracle 到国产库、国密与电子会计档案闭环
数据库·笔记·数据库架构
m0_715674431 小时前
智能化+基于行标+场景化 政务数据库审计与风险监测全维度解决方案
网络·数据库·安全·网络安全·政务
天天喝旺仔2 小时前
MySQL索引优化实战:B+Tree原理、索引失效与覆盖索引调优
数据库·sql·mysql·性能优化
其实防守也摸鱼2 小时前
DeepSeek Harness 开源贡献手记:从入门到合入主线
服务器·数据库·windows·https·ssl
Omics Pro2 小时前
AI药物研发10原则!欧盟EMA×美国FDA
数据库·人工智能·算法·机器学习·自然语言处理