SpringBoot2(Spring Boot 的Web开发 springMVC 请求处理 参数绑定 常用注解 数据传递 文件上传)

news2024/10/1 11:11:23

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;

    @Override
    public 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.Driver
    url: jdbc:mysql://localhost:3306/mavendb?serverTimezone=Asia/Shanghai
    username: root
    password: 12345678
mybatis:
  configuration:
    map-underscore-to-camel-case: true
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #日志

mybatis-plus的增删改查:

@SpringBootTest
public class Test01 {
    @Autowired(required = false)
    StudentMapper studentMapper;

    //新增
    @Test
    public void add() {
        Student student = new Student("一猫人", "大熊猫本猫", 3);
        int count = studentMapper.insert(student);
        System.out.println("主键回填" + student.getStuId());
        System.out.println("影响的行数" + count);
    }

    //根据学生id修改
    @Test
    public void modifyById() {
        Student student = new Student();
        student.setStuId(12);
        student.setStuName("李前");
        student.setStuAge(18);
        int count = studentMapper.updateById(student);
        System.out.println("影响行数" + count);
    }

    //根据姓名修改,条件
    @Test
    public void modifyByName() {
        //1.修改数据
        Student student = new Student();
        student.setNickName("猫眼电影");
        student.setStuAge(18);
        //2.创建条件
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_name", "猫猫");
        //3.执行sql
        studentMapper.update(student, wrapper);
    }

    //根据学生id查询
    @Test
    public void selectById() {
        Student student = studentMapper.selectById(10);
        System.out.println(student);
    }

    //根据多个学生id查询
    @Test
    public 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
    @Test
    public 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);
    }

    //条件查询
    @Test
    public 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拦截器
    @Bean
    public 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不用
     */
    //这里为逻辑分页
    @Test
    public 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条记录
    @Test
    public void deleteById() {
        int count = studentMapper.deleteById(13);
        System.out.println("影响的行数" + count);
    }

    //这里用mp查询时只有12条记录
    @Test
    public 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条记录
    @Test
    void test01() {
        List<Student> students = studentMapper.selectAll();
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println(student);
        }
    }

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2182196.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

启动服务并登录MySQL9数据库

【图书推荐】《MySQL 9从入门到性能优化&#xff08;视频教学版&#xff09;》-CSDN博客 《MySQL 9从入门到性能优化&#xff08;视频教学版&#xff09;&#xff08;数据库技术丛书&#xff09;》(王英英)【摘要 书评 试读】- 京东图书 (jd.com) Windows平台下安装与配置MyS…

第168天:应急响应-ELK 日志分析系统Yara规则样本识别特征提取规则编写

目录 案例一&#xff1a;ELK 搭建使用-导入文件&监控日志&语法筛选 案例二&#xff1a;Yara 规则使用-规则检测&分析特征&自写规则 案例一&#xff1a;ELK 搭建使用-导入文件&监控日志&语法筛选 该软件是专业分析日志的工具&#xff0c;但是不支持安…

带你0到1之QT编程:二十一、QChart类图表及曲线图的实战指南

此为QT编程的第二十一谈&#xff01;关注我&#xff0c;带你快速学习QT编程的学习路线&#xff01; 每一篇的技术点都是很很重要&#xff01;很重要&#xff01;很重要&#xff01;但不冗余&#xff01; 我们通常采取总-分-总和生活化的讲解方式来阐述一个知识点&#xff01;…

华为OD机试 - 最长元音子串的长度(Python/JS/C/C++ 2024 E卷 100分)

华为OD机试 2024E卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试真题&#xff08;Python/JS/C/C&#xff09;》。 刷的越多&#xff0c;抽中的概率越大&#xff0c;私信哪吒&#xff0c;备注华为OD&#xff0c;加入华为OD刷题交流群&#xff0c;…

Github 2024-10-01 开源项目月报 Top20

根据Github Trendings的统计,本月(2024-10-01统计)共有20个项目上榜。根据开发语言中项目的数量,汇总情况如下: 开发语言项目数量TypeScript项目6Python项目6C项目2JavaScript项目2Rust项目1Shell项目1Ruby项目1HTML项目1Go项目1Jupyter Notebook项目1Lobe Chat: 开源ChatGP…

【C语言】字符和字符串函数(2)

文章目录 一、strncpy函数的使用二、strncat函数的使用三、strncmp函数的使用四、strstr的使用和模拟实现五、strtok函数的使用六、strerr函数的使用 一、strncpy函数的使用 我们之前学习的strcpy的作用是把源字符串拷贝到目标空间内&#xff0c;而且经过我们的模拟实现&#x…

智能招聘系统小程序的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;用户管理&#xff0c;企业管理&#xff0c;招聘信息管理&#xff0c;应聘信息管理&#xff0c;系统管理 微信端账号功能包括&#xff1a;系统首页&#xff0c;招聘信息&#xff0c;我的 开发系统&#…

企望制造ERP系统存在RCE漏洞

漏洞描述 企望制造纸箱业erp系统由深知纸箱行业特点和业务流程的多位IT专家打造&#xff0c;具有国际先进的管理方式&#xff0c;将现代化的管理方式融入erp软件中&#xff0c;让企业分分钟就拥有科学的管理经验。erp的功能包括成本核算、报价定价、订单下达、生产下单、现场管…

鸿蒙NEXT开发环境搭建(基于最新api12稳定版)

注意&#xff1a;博主有个鸿蒙专栏&#xff0c;里面从上到下有关于鸿蒙next的教学文档&#xff0c;大家感兴趣可以学习下 如果大家觉得博主文章写的好的话&#xff0c;可以点下关注&#xff0c;博主会一直更新鸿蒙next相关知识 专栏地址: https://blog.csdn.net/qq_56760790/…

【设计模式-命令】

定义 命令模式&#xff08;Command Pattern&#xff09;是一种行为设计模式&#xff0c;它将请求封装为一个对象&#xff0c;从而使您能够使用不同的请求、排队请求或记录请求&#xff0c;并支持可撤销的操作。该模式通过将请求与其执行分离&#xff0c;使得请求者和接收者之间…

养生之道,首先在于饮食!

在快节奏的现代生活中&#xff0c;养生健康成为了人们日益关注的话题。良好的生活习惯和科学的养生方式&#xff0c;不仅能够提升我们的生活质量&#xff0c;还能有效预防疾病&#xff0c;让我们拥有更加充沛的精力和更长久的生命力。 养生之道&#xff0c;首先在于饮食。均衡…

cpp,git,unity学习

c#中的? 1. 空值类型&#xff08;Nullable Types&#xff09; ? 可以用于值类型&#xff08;例如 int、bool 等&#xff09;&#xff0c;使它们可以接受 null。通常&#xff0c;值类型不能为 null&#xff0c;但是通过 ? 可以表示它们是可空的。 int? number null; // …

如何使用 Gradio 创建聊天机器人

如何使用 Gradio 创建聊天机器人 文章目录 如何使用 Gradio 创建聊天机器人一、介绍二、简单示例与实战1、定义聊天功能2、示例&#xff1a;回答“是”或“否”的聊天机器人3、另一个使用用户输入和历史记录的示例4、流式聊天机器人 三、定制化聊天机器人1、为您的机器人添加更…

docker-compose 快速部署clickhouse集群

在本教程中&#xff0c;我们将学习如何使用 Docker Compose 部署一个带有三节点的 ClickHouse 集群&#xff0c;并使用 ZooKeeper 作为分布式协调服务。 前提条件 注意事项&#xff1a; 镜像版本号注意保持一致 [zookeeper:3.7, clickhouse/clickhouse-server:22.5.4]config…

清华大学、腾讯联合推全开源多模态架构Oryx 支持超长视频输入

在人工智能快速发展的今天&#xff0c;一个名为ORYX的多模态大型语言模型正在悄然改变我们对AI理解视觉世界能力的认知。这个由清华大学、腾讯和南洋理工大学研究人员联合开发的AI系统&#xff0c;堪称视觉处理领域的"变形金刚"。 ORYX&#xff0c;全称Oryx Multi-M…

Kotlin:2.0.20 的新特性

一、概述 Kotlin 2.0.20英文版官方文档 Kotlin 2.0.20发布了!这个版本包括对Kotlin 2.0.0的性能改进和bug修复&#xff0c;我们在其中宣布Kotlin K2编译器为Stable。以下是本次发布的一些亮点: 数据类复制函数将具有与构造函数相同的可见性来自默认目标层次结构的源集的静态访…

Windows下载安装Minio超详细

1.下载地址 服务端文件:minio.exe 用于接收文件信息。 客户端文件:mac.exe 用于上传文件 &#xff0c;如果用程序代码操作文件存储&#xff0c;只启动服务端即可。 #企业版 https://min.io/download?licenseenterprise&platformkubernetes#/windows #社区版&#xff08;…

TiDB 性能测试的几个优化点

作者&#xff1a; 数据源的TiDB学习之路 原文来源&#xff1a; https://tidb.net/blog/513a4eef 背景 前段时间参与了一个 TiDB 的性能测试&#xff0c;具体是在三台海光服务器&#xff08;512G内存、128 core 分8个NUMA、4块3.5T SSD&#xff09;搭建一个混合部署的 TiDB …

嵌入式中C语言小项目的具体实现

大家好&#xff0c;今天主要给大家分享一下&#xff0c;如何使用C语言来实现对应的小项目。 第一&#xff1a;C语言计算器实现 第二&#xff1a;C项目中猜字游戏实现 第三&#xff1a;C语言简单的日历实现 第四&#xff1a;C语言中每日定投债券基金一年能赚多少

浮动与网格系统

控制页面布局的工具有浮动、Flexbox 和定位等&#xff0c;这些工具本身没有优劣支付&#xff0c;只不过实现布局的方式略有不同。 1 浮动 浮动元素会脱离正常的文档流&#xff0c;并向左或向右移动&#xff0c;直到它的边缘碰到包含框或另一个浮动元素的边框为止。 文本和内联…