Spring boot学习(二)页面模板与MySQL交互
来源:互联网 发布:问卷录入软件 编辑:程序博客网 时间:2024/06/15 19:09
Spring Boot支持的模板类型有:
velocity、freemarker、Thymeleaf等
在本博客当中主要介绍的是使用thymeleaf模板构建页面view
Thymeleaf
Thymeleaf是一个XML/XHTML/HTML5模板引擎,可用于Web与非Web环境中的应用开发。它是一个开源的Java库,基于Apache License 2.0许可,由Daniel Fernández创建,该作者还是Java加密库Jasypt的作者。
Thymeleaf提供了一个用于整合Spring MVC的可选模块,在应用开发中,你可以使用Thymeleaf来完全代替JSP,或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。你可以使用它创建经过验证的XML与HTML模板。相对于编写逻辑或代码,开发者只需将标签属性添加到模板中即可。接下来,这些标签属性就会在DOM(文档对象模型)上执行预先制定好的逻辑。
在使用thymeleaf模板之前,我们需要在pom.xml文档当中引入jar:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
在application.properties文件当中增加相关的配置:
#thymeleaf startspring.thymeleaf.mode=LEGACYHTML5#spring.thymeleaf.encoding=UTF-8#spring.thymeleaf.content-type=text/html#开发的时候,不启用缓存spring.thymeleaf.cache=false#thymeleaf end
创建用户实体:
import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;@Entity@Table(name="t_s_base_user")public class User { @Id @GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "uuid") @Column(name ="ID",nullable=false,length=32) private String id; @Column(name = "activitisync") private Short activitiSync; @Column(name = "browser", length = 20) private String browser; @Column(name = "password", length = 100) private String password; @Column(name = "realname", length = 50) private String realname; @Column(name = "status") private Short status; @Column(name = "username", nullable = false, length = 10) private String username; @Column(name="delete_flag") private Integer deleteFlag;//getter和setter}
创建对应的UserServiceI.java:
@Transactionalpublic interface UserServiceI { List<User> findAll();}
@Service("userService")@Transactionalpublic class UserServiceImpl implements UserServiceI { @Resource private UserDao userDao; @Override public List<User> findAll() { return userDao.findAll(); }}
与数据库之间交互对应的DAO文件:
import org.springframework.data.jpa.repository.JpaRepository;import com.novel.base.entity.User;public interface UserDao extends JpaRepository<User, String>{}
这边的DAO直接继承自JpaRespository,在JpaRespository当中实现了JavaBean实体基本的增加(save)、更新(save)、删除(delete)、查找(findAll、findOne)等基本的操作。JpaRespository能够根据我们定义的方法完成相关的复杂查询或者操作。
在实际情况当中,我们可能需要根据JavaBean的一个字段获取对应的列表数据信息,那么此时的方法名称定义如下:
//根据用户名查询对应的用户List<User> findByUsername(String username);
当然我们也可能会需要使用多个字段进行联合查询数据信息:
//使用用户的用户名和密码查询用户User findByUsernameAndPassword(String username,String password);
如果我们需要使用更加复杂的查询,我们可以使用原生的SQL语句
//使用原生的SQL进行查询@Query(value="select count(1) from t_s_base_user",nativeQuery=true)List<User> findUserCount();
对应的Controller:
@Controller@RequestMapping("/user")public class UserController { @Resource private UserServiceI userService; @RequestMapping("/list") public String list(HttpServletRequest request){ List<User> userList = userService.findAll(); request.setAttribute("userList", userList); return "/userlist"; }}
此时返回的是”/userlist”,那么此时对应的模板库会去src/main/resources/templates/当中查询userlist.html模板。
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"><title>用户列表</title></head><body><table> <thead> <tr> <th>序号</th> <th>用户名</th> <th>真实姓名</th> </tr> </thead> <tbody th:each="user,index : ${userList}"> <tr> <td th:text="${index.count}">1</td> <td th:text="${user.username}">admin</td> <td th:text="${user.realname}">admin</td> </tr> </tbody></table></body></html>
- Spring boot学习(二)页面模板与MySQL交互
- Spring Boot学习记录(二)--thymeleaf模板
- Spring Boot学习记录(二)--thymeleaf模板
- Spring Boot学习记录(二)--thymeleaf模板
- spring boot 教程(二)模板依赖
- Spring Boot学习(二)
- Spring Boot学习入门篇(二)--使用Spring Boot
- eclipse中使用spring boot 入门开发(包含:与jsp页面和数据库交互,cmd打包运行war包)
- spring-boot thymeleaf模板学习
- Spring Boot学习第一季(DB交互)
- Spring Boot 学习(二)特点
- spring boot学习笔记(二)
- Spring Boot学习笔记(二)
- Spring Boot 学习笔记(二)
- spring boot集成mysql(二)
- spring boot 学习笔记(008)mysql
- spring boot学习二:Spring Boot自动装配分析与实战
- Spring boot(二)
- 苦恼的window
- 周志华《机器学习》第 1 章 绪论
- 建议7 将0值作为枚举的默认值
- java线程模型
- 建议8 避免给枚举类型的元素提供显式的值
- Spring boot学习(二)页面模板与MySQL交互
- Linux 安裝 Redis 教程
- 英雄黑客:一个试图提高物联网安全性的“义务警员”
- 170504 CentOS搭建
- GBDT(Gradient Boosting Decision Tree)
- Leetcode-Reverse words in string
- Swift中将PHAsset对象转为UIImage对象
- cut命令
- Yale人脸数据库、PCA算法实现Matlab中特征脸生成和简单的人脸识别