day2 - SpringBoot框架开发技术
主要内容
1. SpringBoot简介
2. 构建springboot工程
3. springboot接口返回json
4. springboot热部署
5. springboot资源属性配置
6. springboot整合模板引擎
7. springboot异常处理
8. springboot整合MyBatis
9. springboot整合redis
10. springboot整合定时任务
11. springboot整合异步任务以及使用场景
12. springboot中如何使用拦截器
1. SpringBoot简介
SpringBoot特点
- 基于spring, 使开发者快速入门,门槛较低
- Springboot可以创建独立运行的应用而不依赖于容器
- 不需要打包成war包,可以放入tomcat直接运行
- 提供maven极简配置,缺点是会引入很多你不需要的包
- 根据项目来依赖,从而配置spring,需要什么配什么
- 提供可视化的相关功能,方便监控,比如性能,应用的健康程度等
- 简化配置,不用再看过多的xml
- 为微服务SpringCloud铺路, SpringBoot可以整合很多各式各样的框架来构建微服务,比如dubbo, thrift等等
2. 构建springboot工程
Reference: https://blog.csdn.net/typa01_kk/article/details/76696618
3. springboot接口返回json
SpringBoot构造并返回一个json对象
package com.firsttry.helloworld.controller;import com.firsttry.helloworld.pojo.IMoocJSONResult;
import com.firsttry.helloworld.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;import java.util.Date;//@Controller
@RestController //@RestController = @Controller + @ResponseBody
@RequestMapping("/user")
public class UserController {@RequestMapping("/getUser")/*表示将返回的数据封装成json字符串*///@ResponseBodypublic User getUser(){User u = new User();u.setName("Alisa");u.setAge(21);u.setBirthday(new Date());u.setPassword("111111111");u.setDesc("hello I hope I can be much happier, hahahah");return u;}@RequestMapping("/getUserJson")/*表示将返回的数据封装成json字符串*///@ResponseBodypublic IMoocJSONResult getUserJson(){User u = new User();u.setName("Alisa");u.setAge(21);u.setBirthday(new Date());u.setPassword("111111");u.setDesc("emmmmmm11122222");return IMoocJSONResult.ok(u);}
} Jackson的基本演绎法
package com.firsttry.helloworld.pojo;import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;import java.util.Date;public class User {private String name;@JsonIgnoreprivate String password;private Integer age;@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss a", locale = "Aus", timezone = "GMT+10")private Date birthday;@JsonInclude(JsonInclude.Include.NON_NULL)private String desc;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 Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}
} 4. springboot热部署
SpringBoot使用devtools进行热部署,不需要重启服务器就可以部署文件
Reference: https://www.jianshu.com/p/0e3efd50e3e3
5. springboot资源属性配置
6. springboot整合模板引擎
1. springBoot整合freemarker
pom.xml
<!--引入freemarker模板依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency>
application.properties
########
#
# freemarker 静态资源配置
#
########
# 设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
# 关闭缓存,即时刷新,上线生产环境需改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
freemarkerController
package com.firsttry.helloworld.controller;import com.firsttry.helloworld.pojo.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
@RequestMapping("ftl")
public class FreemarkerController {@Autowiredprivate Resource resource;@RequestMapping("/index")public String index(ModelMap map){map.addAttribute("resource", resource);return "freemarker/index";}@RequestMapping("center")public String center(){return "freemarker/center/center";}} 2. springBoot整合thymeleaf
pom.xml
<!--引入thymeleaf模板依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
application.properties
########
#
# thymeleaf 静态资源配置
#
########
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
# 关闭缓存,即时刷新,上线生产环境需要改为true
spring.thymeleaf.cache=false
ThymeleafController
package com.firsttry.helloworld.controller;import com.firsttry.helloworld.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;import java.util.ArrayList;
import java.util.Date;
import java.util.List;@Controller
@RequestMapping("th")
public class ThymeleafController {@RequestMapping("/index")public String index(ModelMap map){map.addAttribute("name", "thymeleaf-imooc");return "thymeleaf/index";}@RequestMapping("center")public String center() {return "thymeleaf/center/center";}@RequestMapping("test")public String test(ModelMap map) {User u = new User();u.setName("manager");u.setAge(10);u.setPassword("123465");u.setBirthday(new Date());u.setDesc("<font color='green'><b>hello imooc</b></font>");map.addAttribute("user", u);User u1 = new User();u1.setAge(19);u1.setName("imooc");u1.setPassword("123456");u1.setBirthday(new Date());User u2 = new User();u2.setAge(17);u2.setName("superadmin");u2.setPassword("123456");u2.setBirthday(new Date());List<User> userList = new ArrayList<User>();userList.add(u);userList.add(u1);userList.add(u2);map.addAttribute("userList", userList);return "thymeleaf/test";}@PostMapping("postform")public String postform(User u) {System.out.println("姓名:" + u.getName());System.out.println("年龄:" + u.getAge());return "redirect:/th/test";}
} thymeleaf常用标签的使用方法
- 基本使用方式
- 对象引用方式
- 时间类型转换
- text与utext的比较
utext: html代码会解析成对应的css代码 - URL
- 引入静态资源
- 条件判断th:if
- th:unless 与 th:if 的使用
- 循环 th:each
- text 与 utext
- th:switch 与 th:case
7. springboot异常处理
- 页面跳转形式
- ajax形式
- 统一返回异常的形式
8. springboot整合MyBatis
- 使用generatorConfig生成mapper以及pojo
- 实现基于mybatis的CRUD功能
- 整合mybatis-pagehelper实现分页
- 自定义mapper的实现
SpringBoot整合持久层事务(为数据库操作设定事务级别)
- 事务隔离级别:DEFAULT READ_UNCOMMITTED READ_COMMITTED REPEATABLE_READ SERIALIZABLE
-事务的传播行为:REQUIRED SUPPORTS MANDATORY REQUIRES_NEW NOT_SUPPORTED NEVER NESTED
9. springboot整合redis
- pom.xml引入需要的依赖
- 资源文件中对redis进行配置
- 引入redis工具类
10. springboot整合定时任务task
- 使用注解@EbableScheduling开启定时任务,会自动扫描
- 定义@Component作为组件被容器扫描
- 表达式生成地址: https://cron.qqe2.com/
11. springboot整合异步任务以及使用场景
- 使用注解@EnableAsync开启异步,会自动扫描
- 定义@Component @Async作为组件被容器扫描执行
12. springboot中如何使用拦截器
- 使用注解@Configuration配置拦截器
- 继承WebMvcConfigurerAdapter
- 重写addInterceptors添加需要的拦截器地址
本文主要参考源码:https://github.com/leechenxiang/imooc-springboot-starter
喜欢的朋友记得点赞、收藏、关注哦!!!
相关文章:
day2 - SpringBoot框架开发技术
主要内容 1. SpringBoot简介 2. 构建springboot工程 3. springboot接口返回json 4. springboot热部署 5. springboot资源属性配置 6. springboot整合模板引擎 7. springboot异常处理 8. springboot整合MyBatis 9. springboot整合redis 10. springboot整合定时任务 11. springbo…...
Flash-03
1-问题:Flash软件画两个图形,若有部分重合则变为一个整体 解决方法1:两个图形分属于不同的图层 解决方法2:将每个图形都转化为【元件】 问题2:元件是什么? 在 Adobe Flash(现在称为 Adobe Anim…...
新建菜单项的创建之CmpGetValueListFromCache函数分析
第一部分: PCELL_DATA CmpGetValueListFromCache( IN PHHIVE Hive, IN PCACHED_CHILD_LIST ChildList, OUT BOOLEAN *IndexCached, OUT PHCELL_INDEX ValueListToRelease ) 0: kd> dv KeyControlBlock 0xe1…...
【Word2Vec】Skip-gram 的直观理解(深入浅出)
01 什么是skip-gram 一句话来说就是,给定中心词,然后预测其周围的词: 02 模型结构 对于skip-gram来说,输入是一个[1 x V]维的ont-hot向量,其中V为词表大小,值为1的那一项就表示我们的中心词。经过一个[V x…...
在MacOS上打造本地部署的大模型知识库(一)
一、在MacOS上安装Ollama docker run -d -p 3000:8080 --add-hosthost.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:main 最后停掉Docker的ollama,就能在webui中加载llama模…...
(21)从strerror到strtok:解码C语言字符函数的“生存指南2”
❤个人主页:折枝寄北的博客 ❤专栏位置:简单入手C语言专栏 目录 前言1. 错误信息报告1.1 strerror 2. 字符操作2.1 字符分类函数2.2 字符转换函数 3. 内存操作函数3.1 memcpy3.2 memmove3.2memset3.3 memcmp 感谢您的阅读 前言 当你写下strcpy(dest, s…...
DeepSeek推出DeepEP:首个开源EP通信库,让MoE模型训练与推理起飞!
今天,DeepSeek 在继 FlashMLA 之后,推出了第二个 OpenSourceWeek 开源项目——DeepEP。 作为首个专为MoE(Mixture-of-Experts)训练与推理设计的开源 EP 通信库,DeepEP 在EP(Expert Parallelism)…...
1.2 Kaggle大白话:Eedi竞赛Transformer框架解决方案02-GPT_4o生成训练集缺失数据
目录 0. 本栏目竞赛汇总表1. 本文主旨2. AI工程架构3. 数据预处理模块3.1 配置数据路径和处理参数3.2 配置API参数3.3 配置输出路径 4. AI并行处理模块4.1 定义LLM客户端类4.2 定义数据处理函数4.3 定义JSON保存函数4.4 定义数据分片函数4.5 定义分片处理函数4.5 定义文件名排序…...
数据结构-顺序表专题
大家好!这里是摆子,今天给大家带来的是C语言数据结构开端-顺序表专题,主要介绍了数据结构和动态顺序表的实现,快来看看吧!记得一键三连哦! 1.数据结构的概念 1.1什么是数据结构? 数据结构是计…...
docker和containerd从TLS harbor拉取镜像
私有镜像仓库配置了自签名证书,https访问,好处是不需要处理免费证书和付费证书带来的证书文件变更,证书文件变更后需要重启服务,自签名证书需要将一套客户端证书存放在/etc/docker/cert.d目录下,或者/etc/containerd/c…...
kafka-关于ISR-概述
一. 什么是ISR ? Kafka 中通常每个分区都有多个副本,其中一个副本被选举为 Leader,其他副本为 Follower。ISR 是指与 Leader 副本保持同步的 Follower 副本集合。ISR 机制的核心是确保数据在多个副本之间的一致性和可靠性,同时在 …...
el-input实现金额输入
需求:想要实现一个输入金额的el-input,限制只能输入数字和一个小数点。失焦数字转千分位,聚焦转为数字,超过最大值,红字提示 效果图 失焦 聚焦 报错效果 // 组件limitDialog <template><el-dialog:visible.s…...
C++11智能指针
一、指针管理的困境 资源释放了,但指针没有置空(野指针、指针悬挂、踩内存) 没有释放资源,产生内存泄漏问题;重复释放资源,引发coredump 二、智能指针...
安装Git(小白也会装)
一、官网下载:Git 1.依次点击(红框) 不要安装在C盘了,要炸了!!! 后面都 使用默认就好了,不用改,直接Next! 直到这里,选第一个 这两种选项的区别如…...
驭势科技9周年:怀揣理想,踏浪前行
2025年的2月,驭势科技迎来9岁生日。位于国内外不同工作地的Uiseeker齐聚线上线下,共同庆祝驭势走过的璀璨九年。 驭势科技联合创始人、董事长兼CEO吴甘沙现场分享了驭势9年的奔赴之路,每一段故事都包含着坚持与拼搏。 左右滑动查看更多 Part.…...
一款在手机上制作电子表格
今天给大家分享一款在手机上制作电子表格的,免费好用的Exce1表格软件,让工作变得更加简单。 1 软件介绍 Exce1是一款手机制作表格的办公软件,您可以使用手机exce1在线制作表格、工资表、编辑xlsx和xls表格文件等,还可以学习使用…...
Python解决“比赛配对”问题
Python解决“比赛配对”问题 问题描述测试样例解决思路代码 问题描述 小R正在组织一个比赛,比赛中有 n 支队伍参赛。比赛遵循以下独特的赛制: 如果当前队伍数为 偶数,那么每支队伍都会与另一支队伍配对。总共进行 n / 2 场比赛,…...
【AI论文】RAD: 通过大规模基于3D图形仿真器的强化学习训练端到端驾驶策略
摘要:现有的端到端自动驾驶(AD)算法通常遵循模仿学习(IL)范式,但面临着因果混淆和开环差距等挑战。在本研究中,我们建立了一种基于3D图形仿真器(3DGS)的闭环强化学习&…...
Web开发:ORM框架之使用Freesql的导航属性
一、什么时候用导航属性 看数据库表的对应关系,一对多的时候用比较好,不用多写一个联表实体,而且查询高效 二、为实体配置导航属性 1.给关系是一的父表实体加上: [FreeSql.DataAnnotations.Navigate(nameof(子表.子表关联字段))]…...
【docker】namespace底层机制
Linux 的 Namespace 机制是实现容器化(如 Docker、LXC 等)的核心技术之一,它通过隔离系统资源(如进程、网络、文件系统等)为进程提供独立的运行环境。其底层机制涉及内核数据结构、系统调用和进程管理。以下是其核心实…...
Visual Paradigm 17.0 团队协作新功能实测:手把手教你用项目模板和文件夹管理提效
Visual Paradigm 17.0 团队协作实战指南:从模板配置到文件夹管理的高效工作流在敏捷开发团队中,项目启动速度和资产管理的规范性往往直接影响整体效率。Visual Paradigm 17.0针对这一痛点推出的团队协作增强功能,特别是服务器端项目模板和文件…...
自制射频功率计:基于AD8317芯片,成本43欧元实现1MHz-10GHz测量
1. 项目概述:为什么我要亲手打造一台射频功率计在无人机和模型飞行器的圈子里,尤其是在我们荷兰FMS Spaarnwoude俱乐部,合规飞行是头等大事。我给我的八轴飞行器加装了云台相机和图传系统,工作在5.8GHz频段。根据本地法规…...
Veo 2胶片质感生成器失效?——深度解析Color Science v2.3内核中被屏蔽的Cinematic Grain Injection层
更多请点击: https://kaifayun.com 第一章:Veo 2胶片质感生成器失效现象全景透视 近期大量用户反馈,Veo 2 胶片质感生成器在调用 generate_film_effect() 接口后返回空纹理、纯灰帧或 HTTP 503 Service Unavailable 错误,且该问题…...
WebSocket实时通信架构进阶:Room、命名空间与集群部署
WebSocket实时通信架构进阶:Room、命名空间与集群部署 作者:Crown_22 | AI Agent & Hermes Agent 桌面程序开发者 前言 WebSocket已经成为实时应用的标准技术,但大多数教程只停留在"建立连接、发送消息"的基础阶段。在生产环境中,你需要处理Room管理、命名空…...
不止于绘图:用GMT 6.4的`grdtrack`和`project`命令玩转地形剖面分析与可视化
不止于绘图:用GMT 6.4的grdtrack和project命令玩转地形剖面分析与可视化 当我们谈论地理空间分析时,很多人首先想到的是绘制精美的地图。但GMT(Generic Mapping Tools)的真正魅力在于它强大的地理计算能力。本文将带你超越基础绘图…...
别再把大模型当搜索框了:一文讲透 LLM 的基本原理、能力边界与局限性
写在前面很多人把大语言模型当成“会聊天的搜索引擎”,结果一上线就遇到幻觉、口径不稳、上下文丢失、成本失控。真正理解 LLM,要先抓住一句话:它是基于 Transformer 的概率生成模型,核心能力来自海量预训练、上下文学习与后训练对…...
Taotoken的稳定性与低延迟在实时对话应用中的实际体验
🚀 告别海外账号与网络限制!稳定直连全球优质大模型,限时半价接入中。 👉 点击领取海量免费额度 Taotoken的稳定性与低延迟在实时对话应用中的实际体验 在开发需要快速响应的AI聊天应用时,后端API的稳定性和延迟表现是…...
概率论:常见分布的期望与方差、中心极限定理、切比雪夫不等式
目录 一、0、1分布 二、二项分布 三、泊松分布 四、均匀分布 五、指数分布 六、正态分布 七、中心极限定理及其应用 (1)中心极限定理的定义 (2)使用示例 八、切比雪夫不等式 (1)切比雪夫不…...
Diablo Edit2:3步掌握暗黑破坏神2存档修改的终极秘籍
Diablo Edit2:3步掌握暗黑破坏神2存档修改的终极秘籍 【免费下载链接】diablo_edit Diablo II Character editor. 项目地址: https://gitcode.com/gh_mirrors/di/diablo_edit 还在为暗黑破坏神2中刷装备的漫长过程感到疲惫吗?Diablo Edit2这款免费…...
SpringBoot WebClient 介绍
目录一、什么是 WebClient?二、 WebClient 能解决什么问题?三、WebClient 和 RestTemplate 的区别四、WebClient 的核心优势1. 非阻塞(Non-Blocking)2. 支持异步3. 链式 API 更现代五、WebClient 的核心对象六、Mono 和 Flux 是什…...
