Thymeleaf的内置对象、SpringBoot整合Thymeleaf和JDBC
Thymeleaf的对象
Thymeleaf是直接支持访问Servlet web的原生资源,HttpServletRequest HttpServletResponse HttpSession ServletContext.
#request:获取HttpServletRequest对象
#response:获取HttpServletResponse 对象
#session:获取HttpSession 对象
#servletContext:获取ServletContext对象
1.Handler
@GetMapping("/servlet")
public String servlet(HttpServletRequest request){
request.setAttribute("valye","request");
request.getSession().setAttribute("valye","request");
request.getServletContext().setAttribute("valye","request");
return "test";
}
<!--request-->
<p th:text="${#request.getAttribute('value')}"></p>
<p th:text="${#session.getAttribute('value')}"></p>
<p th:text="${#servletContext.getAttribute('value')}"></p>
<p th:text="${#reponse}"></p>
Thymeleaf支持直接访问session,相对于
${#servletContext.getAttribute('value')}也可以简化为${value}
Thymeleaf的内置对象
- dates;日期格式化
- calendars:日期操作
- numbers:数字格式化
- Strings:字符格式化
- bools:boolean
- arrays:数组内置对象
- lists:list集合内置对象
- sets:set集合内置对象
- maps;map集合内置对象
@GetMapping("/uniltity")
public ModelAndView uniltity(){
ModelAndView modelAndView =new ModelAndView();
modelAndView.setViewName("test");
modelAndView.addObject("date",new Date());
Calendar calendar =Calendar.getInstance();
calendar.set(2020,1,1);
modelAndView.addObject("calendar",calendar);
modelAndView.addObject("number",0.06);
modelAndView.addObject("string","Springboot");
modelAndView.addObject("boolean",true);
modelAndView.addObject("array",Arrays.asList("张三","李四","王五"));
List<User> users = Arrays.asList(new User("张三",1),new User("李四",2),new User("王五",3));
modelAndView.addObject("list",users);
Set<User> set =new HashSet<>();
set.add(new User("张三",1));
set.add(new User("李四",2));
modelAndView.addObject("set",set);
Map<Integer,User> map=new HashMap<>();
map.put(1,new User("张三",1));
map.put(2,new User("李四",2));
modelAndView.addObject("map",map);
return modelAndView;
}
date:格式化<span th:text="${#dates.format(date,'yyy-mm-dd')}"></span><br>
当前时间:<span th:text="${#dates.cteateToday()}"></span><br>
Calendar格式化:<span th:text="${#calendars.format(calendar,'yyyy-mm-dd')}"></span><br>
number百分比格式化:<span th:text="${#numbers.formatPercent(number,2,2)}"></span><br>
name是否为空:<span th:text="${#strings.isTmpty(string)}"></span><br>
name的长度:<span th:text="${#strings.length(string)}"></span><br>
name的拼接:<span th:text="${#strings.concat('Good',string)}"></span><br>
boolen是否为true:<span th:text="${#bools.isTrue(boolean)}"></span><br>
arrys的长度:<span th:text="${#arrays.length(array)}"></span><br>
array是否包含张三:<span th:text="${#arrays.contains(array,'张三')}"></span><br>
list是否为空:<span th:text="${#list.isEmpty(list)}"></span><br>
list的长度:<span th:text="${#list.size(list)}"></span><br>
Set是否为空:<span th:text="${#sets.isEmpty(set)}"></span><br>
Set的长度:<span th:text="${#sets.size(set)}"></span><br>
Map是否为空:<span th:text="${#maps.isEmpty(map)}"></span><br>
Map是的长度:<span th:text="${#maps.size(map)}"></span><br>
Spring boot 整合JDBC Template
JDBC Template是spring自带的一个JDBC的模版组件,底层实现了对JDBC的封装,用法和Mybatis类似,需要开发者定义SQL语句,JDBC Template帮助我们完成数据库的连接,SQL的执行,结果集的封装
不足之处灵活性不如Mybatis,因为Mybatis是直接卸载XML王文建中,更有利于扩展和维护,JDBC Template是以硬编码的形式将SQL直接写在java代码中,不利于扩展和维护
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>org.example</groupId>
<artifactId>soringbootdo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
2.实体类:
package com.southwind.entity;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private Integer money;
}
3.Repository
接口:
package com.southwind.Repository;
import com.southwind.entity.User;
import java.util.List;
public interface UserRepository {
public List<User> findAll();
public User finById(Integer id);
public int save (User user);
public int update(User user);
public int delete(Integer id);
}
package com.southwind.Repository.impl;
import com.southwind.Repository.UserRepository;
import com.southwind.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UserRepositoryImpl implements UserRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<User> findAll() {
return jdbcTemplate.query(
"select * from people",
new BeanPropertyRowMapper<>(User.class)
);
}
public User finById(Integer id) {
return jdbcTemplate.queryForObject(
"select * from people where id=?",
new Object[]{id},
new BeanPropertyRowMapper<>(User.class)
);
}
public int save(User user) {
return jdbcTemplate.update(
"insert into people(id,name,money) values (?,?,?)",
user.getId(),
user.getName(),
user.getMoney()
);
}
public int update(User user) {
return jdbcTemplate.update(
"update people set name=?,money=? where id=?",
user.getName(),
user.getMoney(),
user.getId()
);
}
public int delete(Integer id) {
return jdbcTemplate.update(
"delete from people where id=?",
id
);
}
}
4.Handeler
package com.southwind.Controller;
import com.southwind.Repository.UserRepository;
import com.southwind.Repository.impl.UserRepositoryImpl;
import com.southwind.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserHandler {
@Autowired
private UserRepositoryImpl userRepository;
@GetMapping("/findall")
@ResponseBody
public List<User> findall(){
return userRepository.findAll();
}
@GetMapping("/findbyid/{id}")
@ResponseBody
public User findbyid(@PathVariable("id")Integer id) {
return userRepository.finById(id);
}
@PostMapping("/save")
@ResponseBody
public int save(@RequestBody User user){
return userRepository.save(user);
}
@PutMapping("/update")
@ResponseBody
public int update(@RequestBody User user){
return userRepository.update(user);
}
@DeleteMapping("/delete/{id}")
@ResponseBody
public int delete(@PathVariable Integer id){
return userRepository.delete(id);
}
}
方法
query:
public <T> List<T> query(String sql, RowMapper<T> rowMapper) throws DataAccessException {
return (List)result(this.query((String)sql, (ResultSetExtractor)(new RowMapperResultSetExtractor(rowMapper))));
}
RowMapper是一个接口,作用是分析结果集,将JDBC查询的ResultSet对象转化成对应的POJO
queryForObjiect(String sql,Object[] args, RowMapper rowMapper)
该方法查询一条数据,并将结果封装成一个POJO
update
public int update(String sql, @Nullable Object... args) throws DataAccessException {
return this.update(sql, this.newArgPreparedStatementSetter(args));
}
增加、修改、删除都可以用这个方法
Thymeleaf的内置对象、SpringBoot整合Thymeleaf和JDBC的更多相关文章
- Thymeleaf 之 内置对象、定义变量、URL参数及标签自定义属性
Thymeleaf 之 内置对象.定义变量.URL参数及标签自定义属性 本文章来自[知识林] 如标题所述,这篇文章主要讲述Thymeleaf中的内置对象(list解析.日期格式化.数字格式化等).定义 ...
- thymeleaf 的内置对象
- Springboot整合thymeleaf模板
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- 03-03:springBoot 整合thymeleaf
thymeleaf 语法详解1.变量输出: th:text :在页面中输出某个值 th:value :将一个值放到input标签中的value中.2.判断字符串是否为空 ①:调用内置对象一定要用# ② ...
- SpringBoot系列三:SpringBoot基本概念(统一父 pom 管理、SpringBoot 代码测试、启动注解分析、配置访问路径、使用内置对象、项目打包发布)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.了解SpringBoot的基本概念 2.具体内容 在之前所建立的 SpringBoot 项目只是根据官方文档实现的一个基础程 ...
- 三、SpringBoot整合Thymeleaf视图
目录 3.1 Thymeleaf视图介绍 3.2 创建SpringBoot项目 3.2 配置Thymeleaf 3.3 编写Demo 3.4 小结 3.1 Thymeleaf视图介绍 先看下官网的介绍 ...
- SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目
如果你和我一样,是一名 Java 道路上的编程男孩,其实我不太建议你花时间学 Thymeleaf,当然他的思想还是值得借鉴的.但是他的本质在我看来就是 Jsp 技术的翻版(Jsp 现在用的真的很少很少 ...
- SpringBoot 整合thymeleaf
1.Thymeleaf介绍(官网推荐:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) Thymeleaf是跟Veloc ...
- JavaWeb学习笔记--2.jsp指令元素、动作元素、内置对象
目录: 一.指令 1.1 page指令 :page指令有13种属性 1.2 include指令:include指令有1种属性,为file.(注意<%@ include%>和<jsp: ...
随机推荐
- 【云原生 · Kubernetes】Taint和Toleration(污点和容忍)
个人名片: 因为云计算成为了监控工程师 个人博客:念舒_C.ying CSDN主页️:念舒_C.ying Taint和Toleration(污点和容忍) 概念 添加多个tainit(污点) 使用例子 ...
- vue 3.0 常用api 的简介
vue3.0 生命周期 写法一 和vue2.x 一致 区别在于(beforeUnmount.unmount)名称不一样 写法二 在setup 中使用, 需要引用 如: import { onBefor ...
- Vue 路由跳转显示空白页面的问题
在写一个登录界面跳转到首页时,路由如下 export default new VueRouter({ routes: [ { path: "/", name: "Logi ...
- devexpress 中advBandedGridView内容自动换行和调整自适应行高
首先是自动换行,可以创建一个repositoryItemMemoEdit 并绑定到需要换行的列中 再设置一下repositoryItemMemoEdit高度自适应,这样子就完成了自动换行了 repos ...
- 关于CSDN博客上传图片的接口研究
代码实现 import requests from requests_toolbelt import MultipartEncoder import urllib.parse fields = { ' ...
- BFS算法套路框架
一.概念 1.定义 Broad First Search 2.与DFS区别 BFS找到的路径最短 3.本质 找出图中从起点到终点的最近距离 二.二叉树的最小高度111 1.代码 /** * Defin ...
- 解决.net Core中WebApi自动Model验证导致数据格式不能统一
简言: 最近做项目用WebAPI Core时,想把返回数据的格式,统一弄成:{"errorMsg":"xxx","Data":"x ...
- kestrel网络编程--开发Fiddler
1 文章目的 本文讲解基于kestrel开发类似Fiddler应用的过程,让读者了解kestrel网络编程里面的kestrel中间件和http应用中间件.由于最终目的不是输出完整功能的产品,所以这里只 ...
- 【深入浅出SpringCloud原理及实战】「SpringCloud-Alibaba系列」微服务模式搭建系统基础架构实战指南及版本规划踩坑分析
Spring Cloud Alibaba Nacos Discovery Spring Boot 应用程序在服务注册与发现方面提供和 Nacos 的无缝集成. 通过一些简单的注解,您可以快速来注册一个 ...
- 搭建漏洞环境及实战——搭建DVWA漏洞环境
DVWA是一款开源的渗透测试漏洞练习平台,其中内涵XSS.SQL注入.文件上传.文件包含.CSRF和暴力破解等各个难度的测试环境. 1.在安装时需要在数据库里创建一个数据库名,进入MySQL管理中的p ...