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>

给出文件列表:

最后给出运行结果:

相关推荐
星恒随风3 小时前
MySQL 从零入门(六):复合查询详解——多表查询、自连接、子查询与 UNION
linux·服务器·数据库·mysql
无奈笑天下4 小时前
银河麒麟上lsblk命令教程详解
linux·运维·服务器·数据库
xiaoye-duck5 小时前
《C++ 项目:手写大模型接入 SDK》会话模块整合:内存 SessionManager 与 DataManager 持久化联动
开发语言·数据库·c++·语言模型
AllData公司负责人6 小时前
AI大模型数据开发底座系列说明书:AllData集成DB-GPT开源项目建设 AI 多模态分析底座,涵盖模态解析/智能大屏/MCP 扩展!
大数据·数据库·人工智能·gpt·数据采集·数据接入·模态解析
2601_967987377 小时前
Sqlite参考手册
数据库·python·sqlite·api·教程
隔窗听雨眠7 小时前
Oracle日志体系分层结构与故障定位路径
数据库·oracle·故障排查
为思念酝酿的痛7 小时前
C语言操作MySQL
数据库·mysql
ShineWinsu8 小时前
对于Qt:QTabWidget常见属性的解析
开发语言·数据库·c++·qt·浏览器·qtabwidget·标签页
budingxiaomoli8 小时前
Spring AOP
java·spring·java-ee
星恒随风18 小时前
MySQL 从零入门(五):MySQL 内置函数详解——日期、字符串、数学函数与实战案例
linux·数据库·笔记·sql·mysql