当前位置: 首页 > news >正文

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常用标签的使用方法

  1. 基本使用方式
  2. 对象引用方式
  3. 时间类型转换
  4. text与utext的比较
    utext: html代码会解析成对应的css代码
  5. URL
  6. 引入静态资源
  7. 条件判断th:if
  8. th:unless 与 th:if 的使用
  9. 循环 th:each
  10. text 与 utext
  11. 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-问题&#xff1a;Flash软件画两个图形&#xff0c;若有部分重合则变为一个整体 解决方法1&#xff1a;两个图形分属于不同的图层 解决方法2&#xff1a;将每个图形都转化为【元件】 问题2&#xff1a;元件是什么&#xff1f; 在 Adobe Flash&#xff08;现在称为 Adobe Anim…...

新建菜单项的创建之CmpGetValueListFromCache函数分析

第一部分&#xff1a; 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 一句话来说就是&#xff0c;给定中心词&#xff0c;然后预测其周围的词&#xff1a; 02 模型结构 对于skip-gram来说&#xff0c;输入是一个[1 x V]维的ont-hot向量&#xff0c;其中V为词表大小&#xff0c;值为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&#xff0c;就能在webui中加载llama模…...

(21)从strerror到strtok:解码C语言字符函数的“生存指南2”

❤个人主页&#xff1a;折枝寄北的博客 ❤专栏位置&#xff1a;简单入手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模型训练与推理起飞!

今天&#xff0c;DeepSeek 在继 FlashMLA 之后&#xff0c;推出了第二个 OpenSourceWeek 开源项目——DeepEP。 作为首个专为MoE&#xff08;Mixture-of-Experts&#xff09;训练与推理设计的开源 EP 通信库&#xff0c;DeepEP 在EP&#xff08;Expert Parallelism&#xff09…...

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 定义文件名排序…...

数据结构-顺序表专题

大家好&#xff01;这里是摆子&#xff0c;今天给大家带来的是C语言数据结构开端-顺序表专题&#xff0c;主要介绍了数据结构和动态顺序表的实现&#xff0c;快来看看吧&#xff01;记得一键三连哦&#xff01; 1.数据结构的概念 1.1什么是数据结构&#xff1f; 数据结构是计…...

docker和containerd从TLS harbor拉取镜像

私有镜像仓库配置了自签名证书&#xff0c;https访问&#xff0c;好处是不需要处理免费证书和付费证书带来的证书文件变更&#xff0c;证书文件变更后需要重启服务&#xff0c;自签名证书需要将一套客户端证书存放在/etc/docker/cert.d目录下&#xff0c;或者/etc/containerd/c…...

kafka-关于ISR-概述

一. 什么是ISR &#xff1f; Kafka 中通常每个分区都有多个副本&#xff0c;其中一个副本被选举为 Leader&#xff0c;其他副本为 Follower。ISR 是指与 Leader 副本保持同步的 Follower 副本集合。ISR 机制的核心是确保数据在多个副本之间的一致性和可靠性&#xff0c;同时在 …...

el-input实现金额输入

需求&#xff1a;想要实现一个输入金额的el-input&#xff0c;限制只能输入数字和一个小数点。失焦数字转千分位&#xff0c;聚焦转为数字&#xff0c;超过最大值&#xff0c;红字提示 效果图 失焦 聚焦 报错效果 // 组件limitDialog <template><el-dialog:visible.s…...

C++11智能指针

一、指针管理的困境 资源释放了&#xff0c;但指针没有置空&#xff08;野指针、指针悬挂、踩内存&#xff09; 没有释放资源&#xff0c;产生内存泄漏问题&#xff1b;重复释放资源&#xff0c;引发coredump 二、智能指针...

安装Git(小白也会装)

一、官网下载&#xff1a;Git 1.依次点击&#xff08;红框&#xff09; 不要安装在C盘了&#xff0c;要炸了&#xff01;&#xff01;&#xff01; 后面都 使用默认就好了&#xff0c;不用改&#xff0c;直接Next&#xff01; 直到这里&#xff0c;选第一个 这两种选项的区别如…...

驭势科技9周年:怀揣理想,踏浪前行

2025年的2月&#xff0c;驭势科技迎来9岁生日。位于国内外不同工作地的Uiseeker齐聚线上线下&#xff0c;共同庆祝驭势走过的璀璨九年。 驭势科技联合创始人、董事长兼CEO吴甘沙现场分享了驭势9年的奔赴之路&#xff0c;每一段故事都包含着坚持与拼搏。 左右滑动查看更多 Part.…...

一款在手机上制作电子表格

今天给大家分享一款在手机上制作电子表格的&#xff0c;免费好用的Exce1表格软件&#xff0c;让工作变得更加简单。 1 软件介绍 Exce1是一款手机制作表格的办公软件&#xff0c;您可以使用手机exce1在线制作表格、工资表、编辑xlsx和xls表格文件等&#xff0c;还可以学习使用…...

Python解决“比赛配对”问题

Python解决“比赛配对”问题 问题描述测试样例解决思路代码 问题描述 小R正在组织一个比赛&#xff0c;比赛中有 n 支队伍参赛。比赛遵循以下独特的赛制&#xff1a; 如果当前队伍数为 偶数&#xff0c;那么每支队伍都会与另一支队伍配对。总共进行 n / 2 场比赛&#xff0c;…...

【AI论文】RAD: 通过大规模基于3D图形仿真器的强化学习训练端到端驾驶策略

摘要&#xff1a;现有的端到端自动驾驶&#xff08;AD&#xff09;算法通常遵循模仿学习&#xff08;IL&#xff09;范式&#xff0c;但面临着因果混淆和开环差距等挑战。在本研究中&#xff0c;我们建立了一种基于3D图形仿真器&#xff08;3DGS&#xff09;的闭环强化学习&…...

Web开发:ORM框架之使用Freesql的导航属性

一、什么时候用导航属性 看数据库表的对应关系&#xff0c;一对多的时候用比较好&#xff0c;不用多写一个联表实体&#xff0c;而且查询高效 二、为实体配置导航属性 1.给关系是一的父表实体加上&#xff1a; [FreeSql.DataAnnotations.Navigate(nameof(子表.子表关联字段))]…...

【docker】namespace底层机制

Linux 的 Namespace 机制是实现容器化&#xff08;如 Docker、LXC 等&#xff09;的核心技术之一&#xff0c;它通过隔离系统资源&#xff08;如进程、网络、文件系统等&#xff09;为进程提供独立的运行环境。其底层机制涉及内核数据结构、系统调用和进程管理。以下是其核心实…...

Cesium1.95中高性能加载1500个点

一、基本方式&#xff1a; 图标使用.png比.svg性能要好 <template><div id"cesiumContainer"></div><div class"toolbar"><button id"resetButton">重新生成点</button><span id"countDisplay&qu…...

mongodb源码分析session执行handleRequest命令find过程

mongo/transport/service_state_machine.cpp已经分析startSession创建ASIOSession过程&#xff0c;并且验证connection是否超过限制ASIOSession和connection是循环接受客户端命令&#xff0c;把数据流转换成Message&#xff0c;状态转变流程是&#xff1a;State::Created 》 St…...

Python爬虫实战:研究feedparser库相关技术

1. 引言 1.1 研究背景与意义 在当今信息爆炸的时代,互联网上存在着海量的信息资源。RSS(Really Simple Syndication)作为一种标准化的信息聚合技术,被广泛用于网站内容的发布和订阅。通过 RSS,用户可以方便地获取网站更新的内容,而无需频繁访问各个网站。 然而,互联网…...

Opencv中的addweighted函数

一.addweighted函数作用 addweighted&#xff08;&#xff09;是OpenCV库中用于图像处理的函数&#xff0c;主要功能是将两个输入图像&#xff08;尺寸和类型相同&#xff09;按照指定的权重进行加权叠加&#xff08;图像融合&#xff09;&#xff0c;并添加一个标量值&#x…...

[10-3]软件I2C读写MPU6050 江协科技学习笔记(16个知识点)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16...

基于matlab策略迭代和值迭代法的动态规划

经典的基于策略迭代和值迭代法的动态规划matlab代码&#xff0c;实现机器人的最优运输 Dynamic-Programming-master/Environment.pdf , 104724 Dynamic-Programming-master/README.md , 506 Dynamic-Programming-master/generalizedPolicyIteration.m , 1970 Dynamic-Programm…...

回溯算法学习

一、电话号码的字母组合 import java.util.ArrayList; import java.util.List;import javax.management.loading.PrivateClassLoader;public class letterCombinations {private static final String[] KEYPAD {"", //0"", //1"abc", //2"…...

使用Spring AI和MCP协议构建图片搜索服务

目录 使用Spring AI和MCP协议构建图片搜索服务 引言 技术栈概览 项目架构设计 架构图 服务端开发 1. 创建Spring Boot项目 2. 实现图片搜索工具 3. 配置传输模式 Stdio模式&#xff08;本地调用&#xff09; SSE模式&#xff08;远程调用&#xff09; 4. 注册工具提…...

xmind转换为markdown

文章目录 解锁思维导图新姿势&#xff1a;将XMind转为结构化Markdown 一、认识Xmind结构二、核心转换流程详解1.解压XMind文件&#xff08;ZIP处理&#xff09;2.解析JSON数据结构3&#xff1a;递归转换树形结构4&#xff1a;Markdown层级生成逻辑 三、完整代码 解锁思维导图新…...

论文阅读:Matting by Generation

今天介绍一篇关于 matting 抠图的文章&#xff0c;抠图也算是计算机视觉里面非常经典的一个任务了。从早期的经典算法到如今的深度学习算法&#xff0c;已经有很多的工作和这个任务相关。这两年 diffusion 模型很火&#xff0c;大家又开始用 diffusion 模型做各种 CV 任务了&am…...