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…...
Python爬虫实战:研究MechanicalSoup库相关技术
一、MechanicalSoup 库概述 1.1 库简介 MechanicalSoup 是一个 Python 库,专为自动化交互网站而设计。它结合了 requests 的 HTTP 请求能力和 BeautifulSoup 的 HTML 解析能力,提供了直观的 API,让我们可以像人类用户一样浏览网页、填写表单和提交请求。 1.2 主要功能特点…...

Lombok 的 @Data 注解失效,未生成 getter/setter 方法引发的HTTP 406 错误
HTTP 状态码 406 (Not Acceptable) 和 500 (Internal Server Error) 是两类完全不同的错误,它们的含义、原因和解决方法都有显著区别。以下是详细对比: 1. HTTP 406 (Not Acceptable) 含义: 客户端请求的内容类型与服务器支持的内容类型不匹…...

Linux-07 ubuntu 的 chrome 启动不了
文章目录 问题原因解决步骤一、卸载旧版chrome二、重新安装chorme三、启动不了,报错如下四、启动不了,解决如下 总结 问题原因 在应用中可以看到chrome,但是打不开(说明:原来的ubuntu系统出问题了,这个是备用的硬盘&a…...

Ascend NPU上适配Step-Audio模型
1 概述 1.1 简述 Step-Audio 是业界首个集语音理解与生成控制一体化的产品级开源实时语音对话系统,支持多语言对话(如 中文,英文,日语),语音情感(如 开心,悲伤)&#x…...
【C语言练习】080. 使用C语言实现简单的数据库操作
080. 使用C语言实现简单的数据库操作 080. 使用C语言实现简单的数据库操作使用原生APIODBC接口第三方库ORM框架文件模拟1. 安装SQLite2. 示例代码:使用SQLite创建数据库、表和插入数据3. 编译和运行4. 示例运行输出:5. 注意事项6. 总结080. 使用C语言实现简单的数据库操作 在…...
AI编程--插件对比分析:CodeRider、GitHub Copilot及其他
AI编程插件对比分析:CodeRider、GitHub Copilot及其他 随着人工智能技术的快速发展,AI编程插件已成为提升开发者生产力的重要工具。CodeRider和GitHub Copilot作为市场上的领先者,分别以其独特的特性和生态系统吸引了大量开发者。本文将从功…...
【HTTP三个基础问题】
面试官您好!HTTP是超文本传输协议,是互联网上客户端和服务器之间传输超文本数据(比如文字、图片、音频、视频等)的核心协议,当前互联网应用最广泛的版本是HTTP1.1,它基于经典的C/S模型,也就是客…...

Redis数据倾斜问题解决
Redis 数据倾斜问题解析与解决方案 什么是 Redis 数据倾斜 Redis 数据倾斜指的是在 Redis 集群中,部分节点存储的数据量或访问量远高于其他节点,导致这些节点负载过高,影响整体性能。 数据倾斜的主要表现 部分节点内存使用率远高于其他节…...

优选算法第十二讲:队列 + 宽搜 优先级队列
优选算法第十二讲:队列 宽搜 && 优先级队列 1.N叉树的层序遍历2.二叉树的锯齿型层序遍历3.二叉树最大宽度4.在每个树行中找最大值5.优先级队列 -- 最后一块石头的重量6.数据流中的第K大元素7.前K个高频单词8.数据流的中位数 1.N叉树的层序遍历 2.二叉树的锯…...
Redis的发布订阅模式与专业的 MQ(如 Kafka, RabbitMQ)相比,优缺点是什么?适用于哪些场景?
Redis 的发布订阅(Pub/Sub)模式与专业的 MQ(Message Queue)如 Kafka、RabbitMQ 进行比较,核心的权衡点在于:简单与速度 vs. 可靠与功能。 下面我们详细展开对比。 Redis Pub/Sub 的核心特点 它是一个发后…...