SpringBoot实现数据库信息网页输出(html+MySQL+Thymeleaf)

SpringBoot实现数据库信息网页输出(html+MySQL+Thymeleaf)

本来是周五下午打算写这篇博客的,结果整了一下午,莫名其妙报了个错,就拖到了现在才来发
废话stop,正文开始:

上篇博客是简单介绍了在控制台的数据库操作,有需要的友友可以去看看
https://blog.csdn.net/m0_51242270/article/details/127546231?spm=1001.2014.3001.5501

这次我们来简单说一下网页端的数据库信息表格输出,需要用到Thymeleaf
之后也会写一下完整的网页端操作数据库的增删改查,需要的友友可以留意一下哟~

本次的博客大纲如下:

  1. 数据库选择及项目结构(我创建的数据库名为spring,表名为student)
  2. 代码粘贴(所有的代码都是可以运行的,如果运行不了,可以后台留言帮看代码)
  3. 结果展示
  4. 写代码过程中我遇到的问题和一些注意事项(细节处,易出错)

1、数据库选择及项目结构

数据库依旧是用到之前的spring数据库下的student表
在这里插入图片描述

项目结构如下所示:
在这里插入图片描述

2、代码粘贴

先创建entity包,包下有Student

package cn.edu.ldu.exper7test.entity;

import lombok.Data;
import org.springframework.stereotype.Component;

import java.math.BigInteger;

@Data
@Component
public class Student {
    private BigInteger id;
    private String username;
    private String password;
    private String course;
}

再创建dao包下StudentDao接口类

package cn.edu.ldu.exper7test.dao;

import cn.edu.ldu.exper7test.entity.Student;

import java.util.List;

public interface StudentDao {
    List<Student> findAll();
}

dao.impl下StudentDaoImpl实现类

package cn.edu.ldu.exper7test.dao.impl;

import cn.edu.ldu.exper7test.dao.StudentDao;
import cn.edu.ldu.exper7test.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class StudentDaoImpl implements StudentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Student> findAll() {
        String sql = "select * from student";
        RowMapper<Student> rowMapper = new BeanPropertyRowMapper<Student>(Student.class);
        return this.jdbcTemplate.query(sql,rowMapper);
    }
}

controller下StudentController

package cn.edu.ldu.exper7test.controller;

import cn.edu.ldu.exper7test.dao.StudentDao;
import cn.edu.ldu.exper7test.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentDao studentDao;
    @GetMapping("/findAll")
    public String findAll(Model model){
        List<Student> studentList=studentDao.findAll();
        System.out.println(studentList);
        model.addAttribute("studentList", studentList);
        return "studentList";
    }
}

在resources包下的templates包下创建studentList.html

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>Student</title>
</head>
<body>
<table border="1" align="center">
  <p align="center">MySQL学生信息表</p>
  <tr>
    <th>num</th>
    <th>id</th>
    <th>username</th>
    <th>password</th>
    <th>course</th>
  </tr>
  <tr th:each="student,stat:${studentList}">
    <td th:text="${stat.count}"></td>
    <td th:text="${student.id}"></td>
    <td th:text="${student.username}"></td>
    <td th:text="${student.password}"></td>
    <td th:text="${student.course}"></td>
  </tr>
</table>
</body>
</html>

application.properties

spring.thymeleaf.cache=false
spring.thymeleaf.encoding = UTF-8

application.yaml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/spring?serverTimezone=GMT%2B8
    driverClassName: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    initialsize: 5
    minldle: 5
    maxActive: 20

依赖的配置也是相当重要的,出现一丁点差错都有可能花费好几天时间才能解决,依赖的注入尽量不要写一些与项目无关的依赖,不要多写也不要少些,都挺头疼的。
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>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.edu.ldu</groupId>
    <artifactId>exper-7-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>exper-7-test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <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>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.23</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3、结果展示

最终运行结果如下所示:
在这里插入图片描述
在这里插入图片描述

4、写代码过程中我遇到的问题和一些注意事项(细节处,易出错)

(1)创建项目时应注意添加这些依赖:
在这里插入图片描述
(2)Student.java在编写时应注意下面两个注解:
在这里插入图片描述
@Data注解:主要作用是提高代码的简洁,这个注解可以省去实体类中所有的get()、 set()、 toString()等方法;
@Component注解:它的作用是实现bean的注入;
(3)在实现类中要注意这个在这里插入图片描述
@Repository:它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean
在实现类的编写中可能会遇到报红的问题,不过不要紧
在这里插入图片描述
代码仍然是可以运行的,没影响,如果实在看它不顺眼可以自行百度消灭它^ ^……
(4)在编写StudentController时应注意注解
在这里插入图片描述
@RestController == @Controller+@ResponseBody
在这里,不可以可以把Controller换成@RestController ,如果换的话,返回的就不是一个html,而是一个简单的字符串,如果与增删改查等方法同时使用时,就不能换了,同时应该在增删改查这些方法上加上@ResponseBody(如果有疑问欢迎留言,经常在线~~)
(5)在编写studentList.html时有报错没影响,但是有一个要注意的地方:
在这里插入图片描述

<html lang="en" >

不能在后面加上Thymeleaf的网址,会报错的
(6)在application.yaml文件中

url: jdbc:mysql://localhost:3306/spring?serverTimezone=GMT%2B8

这行代码中的spring指的是我的数据库的名字,可以依据自己的数据库名字进行更改

有其他的问题,各位可以留言哟~在自己能力范围之内的会及时回复哒!

觉得有帮助的,可以鼓励一下哟~感谢感谢!

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuckyInn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值