您好,欢迎访问代理记账网站
移动应用 微信公众号 联系我们

咨询热线 -

电话 15988168888

联系客服
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

springboot使用mybatis+thymeleaf将数据传递到html页面上

 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.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ty</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ty</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.4</version>
            </plugin>
        </plugins>
    </build>

</project>

application.properties文件代码

server.port=8081
server.address=127.0.0.1
# mysql的驱动连接
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/person?serverTimezone=GMT%2B8
# mysql Driver8版本
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# mybatis
mybatis.mapper-locations=classpath*:mapping/*.xml
mybatis.type-aliases-package=com.tianyi.entity
# 后台打印sql语句
logging.level.com.example.mybatis.mapper=debug
# 关闭页面缓存
spring.thymeleaf.cache=false
# 模板编码
spring.thymeleaf.encoding=utf-8
# 页面映射路径
spring.thymeleaf.prefix=classpath:/templates/
# thymeleaf视图后的后缀
spring.thymeleaf.suffix=.html
# thymeleaf模板模式
spring.thymeleaf.mode=HTML5
spring.mvc.view.suffix=.html
spring.web.resources.static-locations=classpath:/templates/,classpath:/static/

User.java文件

对表中需要传递的字段在java中创建get和set方法

package com.ty.entity;

public class User {
    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public int getPower() {
        return power;
    }

    public void setPower(int power) {
        this.power = power;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    private String name;
    private String password;
    private String email;
    private String gender;
    private String time;
    private String nickname;
    private int power;
    private int id;
}

UserMapper.java文件

package com.ty.mapping;


import com.ty.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;


import java.util.List;

@Mapper 
public interface UserMapper {
    @Select("SELECT * FROM users")       //sql搜索语句
    List<User> toAll();                
}

UserControl.java文件

package com.ty.controller;

import com.ty.entity.User;
import com.ty.mapping.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import java.util.List;


@Controller
public class UserController {

    @Resource
    private UserMapper a;

    @RequestMapping("/index")
    public String user(Model model) {
        model.addAttribute("list", a.toAll());      
        return "index";               //返回到index页面
    }

    @RequestMapping("/1")
    @ResponseBody      //传输Json数据所需注解
    public List<User> user() {
        return a.toAll();        //直接将json传递到页面上
    }
}

index.html页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul th:each="t,s: ${list}">
    <p>
        <span th:text="${list.get(s.index).id}  "></span>
        <span th:text="${list.get(s.index).name}  "></span>
        <span th:text="${list.get(s.index).password}    "></span>
        <span th:text="${list.get(s.index).email}  "></span>
        <span th:text="${list.get(s.index).gender}   "></span>
        <span th:text="${list.get(s.index).time}   "></span>
        <span th:text="${list.get(s.index).power}"></span>
    </p>
</ul>

</body>
</html>

 


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进