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

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&#xff08;Spring Boot 的Web开发 springMVC 请求处理 参数绑定 常用注解 数据传递 文件上传&#xff09; 一、Spring Boot的Web开发 1.静态资源映射规则 总结&#xff1a;只要静态资源放在类路径下&#xff1a; called /static (or /public or /resources or …...

成都网安周暨CCS2024 | 大模型安全与产业应用创新研讨活动成功举办

9月11日-12日&#xff0c;作为2024年国家网络安全宣传周成都系列活动的重磅活动之一&#xff0c;CCS 2024成都网络安全系列活动在成都举行。“大模型安全与产业应用创新研讨活动”同期举办&#xff0c;本场活动由百度安全、成都无糖信息联合承办&#xff0c;特邀云安全联盟CSA大…...

React 解释常见的 hooks: useState / useRef / useContext / useReducer

前言 如果对 re-render 概念还不清楚&#xff0c;建议先看 React & 理解 re-render 的作用、概念&#xff0c;并提供详细的例子解释 再回头看本文。 如果对 React 基础语法还不熟练&#xff0c;建议先看 React & JSX 日常用法与基本原则 再回头看本文。 useState useS…...

telnet发送邮件教程:安全配置与操作指南?

telnet发送邮件的详细步骤&#xff1f;怎么用telnet命令发邮件&#xff1f; 尽管现代邮件客户端和服务器提供了丰富的功能和安全性保障&#xff0c;但在某些特定场景下&#xff0c;了解如何使用telnet发送邮件仍然是一项有价值的技能。AokSend将详细介绍如何安全配置和操作tel…...

超强大的 Nginx 可视化管理工具

今天给大家介绍一款 Nginx 可视化管理界面&#xff0c;非常好用&#xff0c;小白也能立马上手。 nginx-proxy-manager 是一个反向代理管理系统&#xff0c;它基于 NGINX&#xff0c;具有漂亮干净的 Web UI。还可以获得受信任的 SSL 证书&#xff0c;并通过单独的配置、自定义和…...

Android 安装应用-提交阶段之后剩下的操作

它的实现代码在executePostCommitSteps(commitRequest)中&#xff0c;看一下它的代码&#xff1a; /*** 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

学习笔记。 开启靶机。 进入靶场&#xff1a; 我们跟进 tips瞅瞅&#xff1a; 额&#xff0c;纯小白&#xff0c;能想到的就是先F12看看&#xff0c;在CTRLu、以及抓包。 得&#xff0c;不会了&#xff0c;看wp呗&#xff0c;不会死磕没脑子0,0&#xff1f; 参考&#xff1a;…...

JS使用MutationObserver接口来监听DOM的更新

在JavaScript中&#xff0c;可以使用MutationObserver接口来监听DOM的更新。以下是一个使用MutationObserver的示例代码&#xff0c;它监听一个DOM节点的变化&#xff0c;并在变化发生时输出信息 // 选择目标节点 const targetNode document.getElementById(some-id);// 创建…...

图解C#高级教程(三):泛型

本讲用许多代码示例介绍了 C# 语言当中的泛型&#xff0c;主要包括泛型类、接口、结构、委托和方法。 文章目录 1. 为什么需要泛型&#xff1f;2. 泛型类的定义2.1 泛型类的定义2.2 使用泛型类创建变量和实例 3. 使用泛型类实现一个简单的栈3.1 类型参数的约束3.2 Where 子句3…...

240930_CycleGAN循环生成对抗网络

240930_CycleGAN循环生成对抗网络 CycleGAN&#xff0c;也算是笔者记录GAN生成对抗网络的第四篇&#xff0c;前三篇可以跳转 240925-GAN生成对抗网络-CSDN博客 240929-DCGAN生成漫画头像-CSDN博客 240929-CGAN条件生成对抗网络-CSDN博客 在第三篇中&#xff0c;我们采用了p…...

ide 使用技巧与插件推荐

ide 使用技巧与插件推荐 一、IDE 使用技巧 1. 快捷键 掌握常用快捷键&#xff1a; Windows: 使用 Ctrl、Alt 和 Shift 的组合。 Mac: 使用 Cmd、Option 和 Shift。 常用快捷键示例&#xff1a; VS Code: Ctrl P: 快速打开文件。 Ctrl Shift P: 打开命令面板。 Ctrl /…...

【node】 cnpm|npm查看、修改镜像地址操作 换源操作

【node】 cnpm|npm查看、修改镜像地址操作 换源操作 安装完node后 npm 1.查看当前npm信息 npm -v2.查看当前的镜像源 npm config get registry3.如果需要淘宝镜像源&#xff0c;修改当前的镜像源为淘宝镜像源 registry https://registry.npm.taobao.org弃用 npm config se…...

大数据-152 Apache Druid 集群模式 配置启动【下篇】 超详细!

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; 目前已经更新到了&#xff1a; Hadoop&#xff08;已更完&#xff09;HDFS&#xff08;已更完&#xff09;MapReduce&#xff08;已更完&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类&#xff0c;并加载自定义fxml 前提步骤1. 普通类继承Application2. 实现main方法3. 写一个controller4. 写一个fxml文件5. 写start方法加载fxml6. 具体代码7. 运行即可 前提 使用自带javafx的jdk&#xff0c;这里使用的是jdk1.834&#xff…...

数据结构之——单循环链表和双向循环链表

一、单循环链表的奥秘 单循环链表是一种特殊的链表结构&#xff0c;它在数据结构领域中具有重要的地位。其独特的循环特性使得它在某些特定的应用场景中表现出强大的优势。 &#xff08;一&#xff09;结构与初始化 单循环链表的结构由节点组成&#xff0c;每个节点包含数据域…...

Git Stash: 管理临时更改的利器

Git 是一个非常强大的版本控制系统&#xff0c;它不仅帮助我们管理代码的版本&#xff0c;还提供了许多实用的功能来优化我们的工作流程。今天&#xff0c;我们要介绍的是 Git 中的一个非常实用的功能——git stash。 什么是 Git Stash&#xff1f; 在开发过程中&#xff0c;…...

ELK--收集日志demo

ELK--收集日志demo 安装ELK日志收集配置启动容器springboot配置测试 之前项目多实例部署的时候&#xff0c;由于请求被负载到任意节点&#xff0c;所以查看日志是开多个终端窗口。后来做了简单处理&#xff0c;将同一项目的多实例日志存入同一个文件&#xff0c;由于存在文件锁…...

Redis的主要特点及运用场景

Redis的主要特点及运用场景 Redis&#xff08;Remote Dictionary Server&#xff09;是一个开源的高性能键值对&#xff08;key-value&#xff09;数据库。它支持多种类型的数据结构&#xff0c;如字符串&#xff08;strings&#xff09;、散列&#xff08;hashes&…...

与我免费ai书童拆解《坚持》创作历程

插科打诨的海侃胡闹&#xff0c;调侃舒展《坚持》诗创的灵魂盛宴之旅。 (笔记模板由python脚本于2024年09月30日 19:11:42创建&#xff0c;本篇笔记适合喜欢python和诗歌的coder翻阅) 【学习的细节是欢悦的历程】 Python 官网&#xff1a;https://www.python.org/ Free&#x…...

(十)学生端搭建

本次旨在将之前的已完成的部分功能进行拼装到学生端&#xff0c;同时完善学生端的构建。本次工作主要包括&#xff1a; 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...

深入浅出:JavaScript 中的 `window.crypto.getRandomValues()` 方法

深入浅出&#xff1a;JavaScript 中的 window.crypto.getRandomValues() 方法 在现代 Web 开发中&#xff0c;随机数的生成看似简单&#xff0c;却隐藏着许多玄机。无论是生成密码、加密密钥&#xff0c;还是创建安全令牌&#xff0c;随机数的质量直接关系到系统的安全性。Jav…...

前端导出带有合并单元格的列表

// 导出async function exportExcel(fileName "共识调整.xlsx") {// 所有数据const exportData await getAllMainData();// 表头内容let fitstTitleList [];const secondTitleList [];allColumns.value.forEach(column > {if (!column.children) {fitstTitleL…...

在四层代理中还原真实客户端ngx_stream_realip_module

一、模块原理与价值 PROXY Protocol 回溯 第三方负载均衡&#xff08;如 HAProxy、AWS NLB、阿里 SLB&#xff09;发起上游连接时&#xff0c;将真实客户端 IP/Port 写入 PROXY Protocol v1/v2 头。Stream 层接收到头部后&#xff0c;ngx_stream_realip_module 从中提取原始信息…...

论文浅尝 | 基于判别指令微调生成式大语言模型的知识图谱补全方法(ISWC2024)

笔记整理&#xff1a;刘治强&#xff0c;浙江大学硕士生&#xff0c;研究方向为知识图谱表示学习&#xff0c;大语言模型 论文链接&#xff1a;http://arxiv.org/abs/2407.16127 发表会议&#xff1a;ISWC 2024 1. 动机 传统的知识图谱补全&#xff08;KGC&#xff09;模型通过…...

leetcodeSQL解题:3564. 季节性销售分析

leetcodeSQL解题&#xff1a;3564. 季节性销售分析 题目&#xff1a; 表&#xff1a;sales ---------------------- | Column Name | Type | ---------------------- | sale_id | int | | product_id | int | | sale_date | date | | quantity | int | | price | decimal | -…...

Android第十三次面试总结(四大 组件基础)

Activity生命周期和四大启动模式详解 一、Activity 生命周期 Activity 的生命周期由一系列回调方法组成&#xff0c;用于管理其创建、可见性、焦点和销毁过程。以下是核心方法及其调用时机&#xff1a; ​onCreate()​​ ​调用时机​&#xff1a;Activity 首次创建时调用。​…...

【Redis】笔记|第8节|大厂高并发缓存架构实战与优化

缓存架构 代码结构 代码详情 功能点&#xff1a; 多级缓存&#xff0c;先查本地缓存&#xff0c;再查Redis&#xff0c;最后才查数据库热点数据重建逻辑使用分布式锁&#xff0c;二次查询更新缓存采用读写锁提升性能采用Redis的发布订阅机制通知所有实例更新本地缓存适用读多…...

LLMs 系列实操科普(1)

写在前面&#xff1a; 本期内容我们继续 Andrej Karpathy 的《How I use LLMs》讲座内容&#xff0c;原视频时长 ~130 分钟&#xff0c;以实操演示主流的一些 LLMs 的使用&#xff0c;由于涉及到实操&#xff0c;实际上并不适合以文字整理&#xff0c;但还是决定尽量整理一份笔…...

莫兰迪高级灰总结计划简约商务通用PPT模版

莫兰迪高级灰总结计划简约商务通用PPT模版&#xff0c;莫兰迪调色板清新简约工作汇报PPT模版&#xff0c;莫兰迪时尚风极简设计PPT模版&#xff0c;大学生毕业论文答辩PPT模版&#xff0c;莫兰迪配色总结计划简约商务通用PPT模版&#xff0c;莫兰迪商务汇报PPT模版&#xff0c;…...