springboot网页显示数据库数据步骤(以Teacher为例):

  1. 创建一个实体类(entity):
package cn.xdh.entity;


import java.io.Serializable;

public class Teacher implements Serializable {
    private int id;
    private String name;
    private String mobile;
    // 登录密码(使用MD5加密)
    private String password;
    // 入职时间
    private int add_time;
    private String add_ip;

    public Teacher() {
    }

    public Teacher(int id, String name, String mobile, String password, int add_time, String add_ip) {
        this.id = id;
        this.name = name;
        this.mobile = mobile;
        this.password = password;
        this.add_time = add_time;
        this.add_ip = add_ip;
    }

    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 String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAdd_time() {
        return add_time;
    }

    public void setAdd_time(int add_time) {
        this.add_time = add_time;
    }

    public String getAdd_ip() {
        return add_ip;
    }

    public void setAdd_ip(String add_ip) {
        this.add_ip = add_ip;
    }
}


  1. 在resource中新建mapper.xml配置文件(TeacherMapper.xml):

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.xdh.dao.TeacherDao">

    <select id="selectByTeacher" resultType="cn.xdh.entity.Teacher">
        select * from xdh_teacher
    </select>

</mapper>



  1. 新建一个Dao接口(TeacherDao):

package cn.xdh.dao;

import cn.xdh.entity.Teacher;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TeacherDao {
    public List<Teacher> selectByTeacher();
}



  1. 新建一个Service接口(TeacherService):

package cn.xdh.service;


import cn.xdh.entity.Teacher;

import java.util.List;

public interface TeacherService {
   public List<Teacher> selectByTeacher();
}



  1. 编写一个Service实现接口(TeacherServiceImpl):

package cn.xdh.service.impl;

import cn.xdh.dao.TeacherDao;
import cn.xdh.entity.Teacher;
import cn.xdh.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherDao teacherDao;

    @Override
    public List<Teacher> selectByTeacher() {
        List<Teacher> selectByTeacher = teacherDao.selectByTeacher();
        return selectByTeacher;
    }
}



  1. 新建一个Controller控制器类(TeacherController):

package cn.xdh.web;

import cn.xdh.entity.Teacher;
import cn.xdh.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @RequestMapping(value = "/teacher")
    public String selectTeacher(Model model){
        List<Teacher> selectByTeacher = teacherService.selectByTeacher();
        model.addAttribute("selectTeacher",selectByTeacher);
        return "admin/teacher";
    }


}


  1. 新建一个test页面(test.html):

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p th:text="${selectTeacher.getId()}"> </p>
    <p th:text="${selectTeacher.getClass_name()}"> </p>
    <p th:text="${selectTeacher.getAdd_time()}"> </p>
    <p th:text="${selectTeacher.getTeacher_id()}"> </p>
    <p th:text="${selectTeacher.getIs_graduate()}"> </p>
</body>
</html>

后面附上一些不太主要的文件:

  1. 运行类RunBoot:

package cn.xdh;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
@MapperScan(basePackages="cn.xdh.dao")
public class RunBoot {
    public static void main(String[] args) {
        ApplicationContext ac = SpringApplication.run(RunBoot.class, args);
    }
}


  1. 配置文件application.properties:

server.port=8888
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/xdhbg?useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.driverClassName=com.mysql.jdbc.Driver

mybatis.mapper-locations= classpath:/mapper/*.xml
mybatis.type-aliases-package= com.xdl.entity

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false







  1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.xdl</groupId>
    <artifactId>xdhbg</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-->spring boot web启动<-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

        <!-- springboot推荐的模板引擎,要想映射HTML/JSP,必须引入thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!-- 热部署用,改变代码不需要重启项目  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

        <!-->数据库连接<-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>9.0.14</version>
        </dependency>

        <!-->分页<-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

        <!-->识别less.css<-->
        <dependency>
            <groupId>com.asual.lesscss</groupId>
            <artifactId>lesscss-engine</artifactId>
            <version>1.3.0</version>
        </dependency>

    </dependencies>

</project>

  1. 项目目录结构: