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: ...
随机推荐
- c++小练习——黑白棋
没什么好发的,发给黑白棋水一水,如果有人能发现问题就更好了. /* Othello.cpp 黑白棋,实现随时结束并判断胜负的功能 成功运行于Visual Studio 2013 */ #include ...
- 是时候考虑升级 JDK 17 了
Spring,作为 Java EE 的事实规范,在2022年11月16日发布了最新的 6.0.0 GA 版本.这个版本是框架后续新生代的初始版本,拥抱持续创新的 OpenJDK 和 Java 生态.新 ...
- 实用!7个强大的Python机器学习库!⛵
作者:韩信子@ShowMeAI 机器学习实战系列:https://www.showmeai.tech/tutorials/41 本文地址:https://www.showmeai.tech/artic ...
- freeswitch的gateway配置方案
概述 freeswitch是一款简单好用的VOIP开源软交换平台. 在voip的网络模型中,网关是我们经常会遇到的概念. 在freeswitch中,如何配置gateway,如何使用好gateway的模 ...
- ctfshow——萌新web3
题目如下: 源码分析: 通过id可以传入一个参数,对id的值进行了过滤,这里是正则匹配过滤,内容分析:or,-,\,*,<,>,!,x,hex,+.最外面的i是同时匹配过滤内容的大小写.在 ...
- anaconda peompt 、labalimg 数据标注
安装anaconda,进行数据标注 1.安装前准备:下好安装包和所需文件 https://www.aliyundrive.com/s/XyH2JQ5TjCz 提取码: 3c2w 2.运行anacond ...
- BZOJ4919 大根堆(树形dp+线段树合并)
用 multiset 启发式合并贪心维护 LIS 的做法就不多说了,网上题解一大堆,着重讲一下线段树合并维护 \(dp\). \(O(n^2)\) 的 \(dp\) 非常显然.离散化后,设 \(dp[ ...
- CVE-2020-1957
漏洞名称 Apache Shiro 认证绕过漏洞 CVE-2020-1957 利用条件 Apache Shiro < 1.5.1 漏洞原理 Apache Shiro 是一款开源安全框架,提供身份 ...
- Redis哨兵集群搭建-Docker-Compose
title: Redis哨兵集群搭建(Docker-Compose) date: 2022-09-27 17:00:56 tags: - Redis 代码地址:https://github.com/l ...
- group by 语句怎么优化?
一.一个简单使用示例 我这里创建一张订单表 CREATE TABLE `order_info` ( `id` int NOT NULL AUTO_INCREMENT COMMENT '主键', `or ...