SpringBoot2(Spring Boot 的Web开发 springMVC 请求处理 参数绑定 常用注解 数据传递 文件上传)
SpringBoot2(Spring Boot 的Web开发 springMVC 请求处理 参数绑定 常用注解 数据传递 文件上传)
一、Spring Boot的Web开发
1.静态资源映射规则


总结:只要静态资源放在类路径下:
called /static (or /public or /resources or //METAINF/resources
一启动服务器就能访问到静态资源文件


springboot只需要将图片放在 static 下 就可以被访问到了
** 总结: **
只要静态资源放在类路径下: called /static (or INF/resources
访问 : 当前项目根路径/ + 静态资源名
**静态资源访问前缀 **
spring:mvc:static-path-pattern: static/test/**



2.enjoy模板引擎
“四个步骤”
1.添加坐标
<dependency><groupId>com.jfinal</groupId><artifactId>enjoy</artifactId><version>5.0.3</version>
</dependency>

2.开启配置
package com.stringzhua.springboot_web_demo01.config;import com.jfinal.template.Engine;
import com.jfinal.template.ext.spring.JFinalViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SpringBootConfig {@Bean(name = "jfinalViewResolver")public JFinalViewResolver getJFinalViewResolver() {// 创建用于整合 spring boot 的 ViewResolver 扩展对象JFinalViewResolver jfr = new JFinalViewResolver();// 对 spring boot 进行配置jfr.setSuffix(".html");jfr.setContentType("text/html;charset=UTF-8");jfr.setOrder(0);// 设置在模板中可通过 #(session.value) 访问 session 中的数据jfr.setSessionInView(true);// 获取 engine 对象,对 enjoy 模板引擎进行配置,配置方式与前面章节完全一样Engine engine = JFinalViewResolver.engine;// 热加载配置能对后续配置产生影响,需要放在最前面engine.setDevMode(true);// 使用 ClassPathSourceFactory 从 class path 与 jar 包中加载模板文件engine.setToClassPathSourceFactory();// 在使用 ClassPathSourceFactory 时要使用 setBaseTemplatePath// 代替 jfr.setPrefix("/view/")engine.setBaseTemplatePath("/templates/");// 更多配置与前面章节完全一样// engine.addDirective(...)// engine.addSharedMethod(...);return jfr;}
}

3.将页面保存在templates目录下

4.编写代码
Spring整合mybatis-plus

“四步”
1.导入坐标
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.3</version></dependency><!-- <dependency>-->
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!-- <version>2.2.2</version>-->
<!-- </dependency>-->
2.编写配置实体类与关系表映射关系
package com.stringzhua.springboot_mybatis_demo01.pojo;import com.baomidou.mybatisplus.annotation.*;
import org.springframework.web.multipart.MultipartFile;import java.io.Serializable;/*** @Author Stringzhua* @Date 2024/9/19 17:21* description:*/
@TableName(value = "student")
public class Student implements Serializable {@TableId(value = "stu_id", type = IdType.AUTO)private int stuId;@TableField(value = "stu_name")private String stuName;@TableField(value = "nick_name")private String nickName;@TableField(value = "stu_age")private int stuAge;//逻辑查询@TableLogic(value = "0", delval = "1")private int isDelete;//数据库无关字段@TableField(exist = false)private MultipartFile file;@Overridepublic String toString() {return "Student{" +"stuId=" + stuId +", stuName='" + stuName + '\'' +", nickName='" + nickName + '\'' +", stuAge=" + stuAge +'}';}public int getStuId() {return stuId;}public void setStuId(int stuId) {this.stuId = stuId;}public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public String getNickName() {return nickName;}public void setNickName(String nickName) {this.nickName = nickName;}public int getStuAge() {return stuAge;}public void setStuAge(int stuAge) {this.stuAge = stuAge;}public Student() {}public Student(String stuName, String nickName, int stuAge) {this.stuName = stuName;this.nickName = nickName;this.stuAge = stuAge;}
}
3.1使用公共的数据访问层
@Mapper
public interface StudentMapper extends BaseMapper<Student> {@Select("select * from student")public List<Student> selectAll();
}
3.2使用公共业务层
4.编写配置文件
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mavendb?serverTimezone=Asia/Shanghaiusername: rootpassword: 12345678
mybatis:configuration:map-underscore-to-camel-case: true
mybatis-plus:configuration:map-underscore-to-camel-case: truelog-impl: org.apache.ibatis.logging.stdout.StdOutImpl #日志
mybatis-plus的增删改查:
@SpringBootTest
public class Test01 {@Autowired(required = false)StudentMapper studentMapper;//新增@Testpublic void add() {Student student = new Student("一猫人", "大熊猫本猫", 3);int count = studentMapper.insert(student);System.out.println("主键回填" + student.getStuId());System.out.println("影响的行数" + count);}//根据学生id修改@Testpublic void modifyById() {Student student = new Student();student.setStuId(12);student.setStuName("李前");student.setStuAge(18);int count = studentMapper.updateById(student);System.out.println("影响行数" + count);}//根据姓名修改,条件@Testpublic void modifyByName() {//1.修改数据Student student = new Student();student.setNickName("猫眼电影");student.setStuAge(18);//2.创建条件QueryWrapper<Student> wrapper = new QueryWrapper<>();wrapper.eq("stu_name", "猫猫");//3.执行sqlstudentMapper.update(student, wrapper);}//根据学生id查询@Testpublic void selectById() {Student student = studentMapper.selectById(10);System.out.println(student);}//根据多个学生id查询@Testpublic void selectByMoreId() {List<Student> students = studentMapper.selectBatchIds(Arrays.asList(1, 2, 3, 4, 5, 6));for (int i = 0; i < students.size(); i++) {Student student = students.get(i);System.out.println(student);}}//查询count@Testpublic void selectCount() {QueryWrapper<Student> wrapper = new QueryWrapper<>();wrapper.eq("stu_name", "猫猫");
// int count = studentMapper.selectCount(null);//全查int count = studentMapper.selectCount(wrapper);//条件查询System.out.println(count);}//条件查询@Testpublic void selectByCondition() {QueryWrapper<Student> wrapper = new QueryWrapper<>();
// wrapper.eq("stu_name","猫猫");//相当于这里有个and
// wrapper.eq("nick_name","猫眼电影");//or查询wrapper.eq("stu_name", "猫猫").or().eq("nick_name", "猫眼电影");List<Student> list = studentMapper.selectList(wrapper);for (int i = 0; i < list.size(); i++) {Student student = list.get(i);System.out.println(student);}}
}
mybatis-plus的分页插件[自带]
package com.stringzhua.springboot_mybatis_demo01.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @Author Stringzhua* @Date 2024/9/20 12:10* description:*/
@Configuration
public class MyBatisConfig {//注入mp拦截器@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){//1.实例化拦截器MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();//2.分页拦截器mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());return mybatisPlusInterceptor;}
}
@SpringBootTest
public class Test01 {@Autowired(required = false)StudentMapper studentMapper;/*** mp分页使用* 注意:* 1.page.setCurrent(2);当前页码从1开始* 2.mybatis分页需要配置插件,mp不用*///这里为逻辑分页@Testpublic void Page() {//1.定义分页规则Page<Student> page = new Page<>();page.setSize(4);//每页记录数page.setCurrent(2);//当前页码//2.条件查询(可选)QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.eq("stu_sex", "男");Page<Student> iPage = studentMapper.selectPage(page, null);List<Student> list = iPage.getRecords();System.out.println("总记录数" + iPage.getTotal());System.out.println("总记页数" + iPage.getPages());for (int i = 0; i < list.size(); i++) {Student student = list.get(i);System.out.println(student);}}//逻辑删除//数据库中只是把is_delete设置为1,mp查询时默认查询为0字段的//这里用mp查询时只有12条记录@Testpublic void deleteById() {int count = studentMapper.deleteById(13);System.out.println("影响的行数" + count);}//这里用mp查询时只有12条记录@Testpublic void selectAll() {//queryWrapper为null,则没有查询条件,为全查List<Student> list = studentMapper.selectList(null);for (int i = 0; i < list.size(); i++) {Student student = list.get(i);System.out.println(student);}}
}
这里使用mp进行全查,则为12条记录,原因是mp走@TableLogic注解时,走的是查询表中is_delete字段为0的值。
//逻辑查询
@TableLogic(value = "0", delval = "1")
private int isDelete;

这里使用mybatis进行全查,则为13条记录
package com.stringzhua.springboot_mybatis_demo01;import com.stringzhua.springboot_mybatis_demo01.mapper.StudentMapper;
import com.stringzhua.springboot_mybatis_demo01.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
class SpringbootMybatisDemo01ApplicationTests {@Autowired(required = false)StudentMapper studentMapper;//如果是mybatis的话,走自己写的sql,则是全查,相当于count(*)//13条记录@Testvoid test01() {List<Student> students = studentMapper.selectAll();for (int i = 0; i < students.size(); i++) {Student student = students.get(i);System.out.println(student);}}}

相关文章:
SpringBoot2(Spring Boot 的Web开发 springMVC 请求处理 参数绑定 常用注解 数据传递 文件上传)
SpringBoot2(Spring Boot 的Web开发 springMVC 请求处理 参数绑定 常用注解 数据传递 文件上传) 一、Spring Boot的Web开发 1.静态资源映射规则 总结:只要静态资源放在类路径下: called /static (or /public or /resources or …...
成都网安周暨CCS2024 | 大模型安全与产业应用创新研讨活动成功举办
9月11日-12日,作为2024年国家网络安全宣传周成都系列活动的重磅活动之一,CCS 2024成都网络安全系列活动在成都举行。“大模型安全与产业应用创新研讨活动”同期举办,本场活动由百度安全、成都无糖信息联合承办,特邀云安全联盟CSA大…...
React 解释常见的 hooks: useState / useRef / useContext / useReducer
前言 如果对 re-render 概念还不清楚,建议先看 React & 理解 re-render 的作用、概念,并提供详细的例子解释 再回头看本文。 如果对 React 基础语法还不熟练,建议先看 React & JSX 日常用法与基本原则 再回头看本文。 useState useS…...
telnet发送邮件教程:安全配置与操作指南?
telnet发送邮件的详细步骤?怎么用telnet命令发邮件? 尽管现代邮件客户端和服务器提供了丰富的功能和安全性保障,但在某些特定场景下,了解如何使用telnet发送邮件仍然是一项有价值的技能。AokSend将详细介绍如何安全配置和操作tel…...
超强大的 Nginx 可视化管理工具
今天给大家介绍一款 Nginx 可视化管理界面,非常好用,小白也能立马上手。 nginx-proxy-manager 是一个反向代理管理系统,它基于 NGINX,具有漂亮干净的 Web UI。还可以获得受信任的 SSL 证书,并通过单独的配置、自定义和…...
Android 安装应用-提交阶段之后剩下的操作
它的实现代码在executePostCommitSteps(commitRequest)中,看一下它的代码: /*** On successful install, executes remaining steps after commit completes and the package lock* is released. These are typically more expensive or require calls t…...
buuctf [ACTF2020 新生赛]Include
学习笔记。 开启靶机。 进入靶场: 我们跟进 tips瞅瞅: 额,纯小白,能想到的就是先F12看看,在CTRLu、以及抓包。 得,不会了,看wp呗,不会死磕没脑子0,0? 参考:…...
JS使用MutationObserver接口来监听DOM的更新
在JavaScript中,可以使用MutationObserver接口来监听DOM的更新。以下是一个使用MutationObserver的示例代码,它监听一个DOM节点的变化,并在变化发生时输出信息 // 选择目标节点 const targetNode document.getElementById(some-id);// 创建…...
图解C#高级教程(三):泛型
本讲用许多代码示例介绍了 C# 语言当中的泛型,主要包括泛型类、接口、结构、委托和方法。 文章目录 1. 为什么需要泛型?2. 泛型类的定义2.1 泛型类的定义2.2 使用泛型类创建变量和实例 3. 使用泛型类实现一个简单的栈3.1 类型参数的约束3.2 Where 子句3…...
240930_CycleGAN循环生成对抗网络
240930_CycleGAN循环生成对抗网络 CycleGAN,也算是笔者记录GAN生成对抗网络的第四篇,前三篇可以跳转 240925-GAN生成对抗网络-CSDN博客 240929-DCGAN生成漫画头像-CSDN博客 240929-CGAN条件生成对抗网络-CSDN博客 在第三篇中,我们采用了p…...
ide 使用技巧与插件推荐
ide 使用技巧与插件推荐 一、IDE 使用技巧 1. 快捷键 掌握常用快捷键: Windows: 使用 Ctrl、Alt 和 Shift 的组合。 Mac: 使用 Cmd、Option 和 Shift。 常用快捷键示例: VS Code: Ctrl P: 快速打开文件。 Ctrl Shift P: 打开命令面板。 Ctrl /…...
【node】 cnpm|npm查看、修改镜像地址操作 换源操作
【node】 cnpm|npm查看、修改镜像地址操作 换源操作 安装完node后 npm 1.查看当前npm信息 npm -v2.查看当前的镜像源 npm config get registry3.如果需要淘宝镜像源,修改当前的镜像源为淘宝镜像源 registry https://registry.npm.taobao.org弃用 npm config se…...
大数据-152 Apache Druid 集群模式 配置启动【下篇】 超详细!
点一下关注吧!!!非常感谢!!持续更新!!! 目前已经更新到了: Hadoop(已更完)HDFS(已更完)MapReduce(已更完&am…...
IDE 使用技巧与插件推荐全面指南
目录 目录 常用IDE概述 Visual Studio Visual Studio Code IntelliJ IDEA PyCharm Eclipse IDE 使用技巧 通用技巧 Visual Studio 专属技巧 Visual Studio Code 专属技巧 IntelliJ IDEA 专属技巧 插件推荐 Visual Studio 插件 Visual Studio Code 插件 IntelliJ…...
java-快速将普通main类变为javafx类,并加载自定义fxml
java-快速将普通main类变为javafx类,并加载自定义fxml 前提步骤1. 普通类继承Application2. 实现main方法3. 写一个controller4. 写一个fxml文件5. 写start方法加载fxml6. 具体代码7. 运行即可 前提 使用自带javafx的jdk,这里使用的是jdk1.834ÿ…...
数据结构之——单循环链表和双向循环链表
一、单循环链表的奥秘 单循环链表是一种特殊的链表结构,它在数据结构领域中具有重要的地位。其独特的循环特性使得它在某些特定的应用场景中表现出强大的优势。 (一)结构与初始化 单循环链表的结构由节点组成,每个节点包含数据域…...
Git Stash: 管理临时更改的利器
Git 是一个非常强大的版本控制系统,它不仅帮助我们管理代码的版本,还提供了许多实用的功能来优化我们的工作流程。今天,我们要介绍的是 Git 中的一个非常实用的功能——git stash。 什么是 Git Stash? 在开发过程中,…...
ELK--收集日志demo
ELK--收集日志demo 安装ELK日志收集配置启动容器springboot配置测试 之前项目多实例部署的时候,由于请求被负载到任意节点,所以查看日志是开多个终端窗口。后来做了简单处理,将同一项目的多实例日志存入同一个文件,由于存在文件锁…...
Redis的主要特点及运用场景
Redis的主要特点及运用场景 Redis(Remote Dictionary Server)是一个开源的高性能键值对(key-value)数据库。它支持多种类型的数据结构,如字符串(strings)、散列(hashes&…...
与我免费ai书童拆解《坚持》创作历程
插科打诨的海侃胡闹,调侃舒展《坚持》诗创的灵魂盛宴之旅。 (笔记模板由python脚本于2024年09月30日 19:11:42创建,本篇笔记适合喜欢python和诗歌的coder翻阅) 【学习的细节是欢悦的历程】 Python 官网:https://www.python.org/ Free&#x…...
XCTF-web-easyupload
试了试php,php7,pht,phtml等,都没有用 尝试.user.ini 抓包修改将.user.ini修改为jpg图片 在上传一个123.jpg 用蚁剑连接,得到flag...
React 第五十五节 Router 中 useAsyncError的使用详解
前言 useAsyncError 是 React Router v6.4 引入的一个钩子,用于处理异步操作(如数据加载)中的错误。下面我将详细解释其用途并提供代码示例。 一、useAsyncError 用途 处理异步错误:捕获在 loader 或 action 中发生的异步错误替…...
C++初阶-list的底层
目录 1.std::list实现的所有代码 2.list的简单介绍 2.1实现list的类 2.2_list_iterator的实现 2.2.1_list_iterator实现的原因和好处 2.2.2_list_iterator实现 2.3_list_node的实现 2.3.1. 避免递归的模板依赖 2.3.2. 内存布局一致性 2.3.3. 类型安全的替代方案 2.3.…...
Linux链表操作全解析
Linux C语言链表深度解析与实战技巧 一、链表基础概念与内核链表优势1.1 为什么使用链表?1.2 Linux 内核链表与用户态链表的区别 二、内核链表结构与宏解析常用宏/函数 三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势5.1 插入效率5.2 安全…...
前端倒计时误差!
提示:记录工作中遇到的需求及解决办法 文章目录 前言一、误差从何而来?二、五大解决方案1. 动态校准法(基础版)2. Web Worker 计时3. 服务器时间同步4. Performance API 高精度计时5. 页面可见性API优化三、生产环境最佳实践四、终极解决方案架构前言 前几天听说公司某个项…...
无法与IP建立连接,未能下载VSCode服务器
如题,在远程连接服务器的时候突然遇到了这个提示。 查阅了一圈,发现是VSCode版本自动更新惹的祸!!! 在VSCode的帮助->关于这里发现前几天VSCode自动更新了,我的版本号变成了1.100.3 才导致了远程连接出…...
visual studio 2022更改主题为深色
visual studio 2022更改主题为深色 点击visual studio 上方的 工具-> 选项 在选项窗口中,选择 环境 -> 常规 ,将其中的颜色主题改成深色 点击确定,更改完成...
理解 MCP 工作流:使用 Ollama 和 LangChain 构建本地 MCP 客户端
🌟 什么是 MCP? 模型控制协议 (MCP) 是一种创新的协议,旨在无缝连接 AI 模型与应用程序。 MCP 是一个开源协议,它标准化了我们的 LLM 应用程序连接所需工具和数据源并与之协作的方式。 可以把它想象成你的 AI 模型 和想要使用它…...
Java - Mysql数据类型对应
Mysql数据类型java数据类型备注整型INT/INTEGERint / java.lang.Integer–BIGINTlong/java.lang.Long–––浮点型FLOATfloat/java.lang.FloatDOUBLEdouble/java.lang.Double–DECIMAL/NUMERICjava.math.BigDecimal字符串型CHARjava.lang.String固定长度字符串VARCHARjava.lang…...
python爬虫:Newspaper3k 的详细使用(好用的新闻网站文章抓取和解析的Python库)
更多内容请见: 爬虫和逆向教程-专栏介绍和目录 文章目录 一、Newspaper3k 概述1.1 Newspaper3k 介绍1.2 主要功能1.3 典型应用场景1.4 安装二、基本用法2.2 提取单篇文章的内容2.2 处理多篇文档三、高级选项3.1 自定义配置3.2 分析文章情感四、实战案例4.1 构建新闻摘要聚合器…...
