在spring boot 项目中使用thymeleaf模板;小案例
2020-12-19 13:33
YPE html>
标签:framework doctype 技术 stp info att example boot turn
准备
MySql数据库,表Prereg,IDEA
数据库中的表如下所示:
IDEA目录结构如下:
添加thymeleaf依赖:
org.springframework.boot spring-boot-starter-thymeleaf
开始添加代码:
在controller包添加类“PreregController”
package com.example.demo.controller; import com.example.demo.mapper.PreregMapper; import com.example.demo.pojo.Prereg; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import java.util.List; @Controller public class PreregController { @Resource PreregMapper preregMapper; @RequestMapping("/listPrereg") public String listPrereg(Model model) { Listpreregs=preregMapper.findAll(); model.addAttribute("preregs",preregs); return "listPrereg"; } }
在Mapper包下添加映射interface:“PreregMapper”
package com.example.demo.mapper; import com.example.demo.pojo.Prereg; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration; import java.util.List; @Mapper public interface PreregMapper { @Select("SELECT * FROM Prereg") ListfindAll(); }
在pojo包下添加类Prereg:
package com.example.demo.pojo; import java.util.Date; public class Prereg { private String StuId; private String StuName; private String Trans; private int IsCompany; private int PeopleCount; private Date ArrTime; public String getStuId() { return StuId; } public void setStuId(String stuId) { StuId = stuId; } public String getStuName() { return StuName; } public void setStuName(String stuName) { StuName = stuName; } public String getTrans() { return Trans; } public void setTrans(String trans) { Trans = trans; } public int getIsCompany() { return IsCompany; } public void setIsCompany(int isCompany) { IsCompany = isCompany; } public int getPeopleCount() { return PeopleCount; } public void setPeopleCount(int peopleCount) { PeopleCount = peopleCount; } public Date getArrTime() { return ArrTime; } public void setArrTime(Date arrTime) { ArrTime = arrTime; } @Override public String toString() { return "Prereg{" + "StuId=‘" + StuId + ‘\‘‘ + ", StuName=‘" + StuName + ‘\‘‘ + ", Trans=‘" + Trans + ‘\‘‘ + ", IsCompany=" + IsCompany + ", PeopleCount=" + PeopleCount + ", ArrTime=" + ArrTime + ‘}‘; } }
注:小技巧:定义好变量后,Alt+insert弹出“Generate”,选择“Getter and Setter”,再选择toString()即可完成。
最后是写HTML页面:
springboot-thymeleaf demo
学生学号 | 学生姓名 | 到达时间 | 家人陪伴 | 陪伴数量 | 交通工具 |
效果图如下:
————————————————
版权声明:本文为CSDN博主「YUEXILIULI」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/YUEXILIULI/java/article/details/103099403
在spring boot 项目中使用thymeleaf模板;小案例
标签:framework doctype 技术 stp info att example boot turn
原文地址:https://www.cnblogs.com/kuangwl/p/13381840.html
文章标题:在spring boot 项目中使用thymeleaf模板;小案例
文章链接:http://soscw.com/index.php/essay/37211.html