java学习总结(八):Spring boot

一、SpringBoot简介

传统Spring开发缺点:
1、导入依赖繁琐
2、项目配置繁琐
Spring Boot是全新框架(更像是一个工具, 脚手架),是Spring提供的一个子项目, 用于快速构建Spring应用程序。
随着Spring 3.0的发布,Spring 团队逐渐开始摆脱XML配置文件,并且在开发过程中大量使用“约定优先配置”(convention over configuration)的思想来摆脱Spring框架中各类繁复纷杂的配置。
SpringBoot的特性:
  1. 起步依赖 jar包的管理 starter
  2. 自动配置 SpringBoot做了很多默认的配置
  3. 内置tomcat

二、创建SpringBoot项目

1、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.situ</groupId><artifactId>springboot</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot</name><description>springboot</description><properties><java.version>17</java.version></properties><dependencies><!--Spring相关依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--SpringMVC相关依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- mysql驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.31</version></dependency><!-- mybatis --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId>f<version>3.0.3</version></dependency><!--thymeleaf相关依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!--lombok帮助我们生成实体类的构造方法、get、set、toString--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.14</version><scope>provided</scope></dependency><!-- 分页插件 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2、SpringbootApplication启动类

//SpringBoot项目启动类
@SpringBootApplication
@MapperScan("com.situ.springboot.mapper")
public class SpringbootApplication {public static void main(String[] args) {SpringApplication.run(SpringbootApplication.class, args);}
}
@Controller
@RequestMapping("/admin")
public class AdminController {@RequestMapping("/selectAll")@ResponseBodypublic List<Admin> selectAll() {System.out.println("AdminController.selectAll");List<Admin> list = new ArrayList<>();Admin admin1 = new Admin();admin1.setId(1);admin1.setName("zhangsan");Admin admin2 = new Admin();admin2.setId(2);admin2.setName("list");Admin admin3 = new Admin();admin3.setId(3);admin3.setName("wangwu");list.add(admin1);list.add(admin2);list.add(admin3);return list;}
}

3、application.properties

server.port=8080#DB Configuration
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/blog?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8&zeroDateTimeBehavior=CONVERT_TO_NULL
spring.datasource.username=root
spring.datasource.password=1234spring.mvc.static-path-pattern=/static/**#Spring MyBatis
mybatis.type-aliases-package=com.situ.springboot.pojo
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.configuration.map-underscore-to-camel-case=true
#log
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

4、AdminMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.situ.springboot.mapper.AdminMapper"><select id="selectAll" resultType="Admin">SELECT id,name,password,nick_name,role,image FROM admin</select>
</mapper>

三、Thymeleaf

<!--thymeleaf相关依赖-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

application.properties:

spring.mvc.static-path-pattern=/static/**
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head><title>主页 </title><link rel="stylesheet" type="text/css" th:href="@{/static/layui/css/layui.css}"/><script th:src="@{/static/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
</head>
@Controller
@RequestMapping("/page")
public class PageController {/*** /page/user/add     /page/login** @return*/@RequestMapping("/**")public String path(HttpServletRequest request) {String requestURI = request.getRequestURI();System.out.println("requestURI: " + requestURI);String[] paths = requestURI.split("/");//["","page","user","list"]//["","page","login"]if (paths.length == 4) {return paths[2] + "_" + paths[3];} else if (paths.length == 3) {return paths[2];} else {return "index";}}
}

四、登录拦截器

//拦截器的作用:浏览器访问服务器的请求,都要首先经过拦截器
public class LoginInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {////1.判断用户有没有登录//2.如果登录了,就放行,可以访问后台的资源//3.如果没有登录,跳转到登录界面HttpSession session = request.getSession();Admin admin = (Admin) session.getAttribute("admin");if (admin == null) {//没有登录,跳转到登录界面response.sendRedirect("/page/login");return false;}//已经登录,放行return true;}
}

拦截器配置

WebMvcConfigurer提供了配置SpringMVC底层的所有组件入口

// @Component    <bean>
// @Controller  @Service @Repository
// 这四个注解的作用是一样的,下面三个不一样主要是为了区分不同层// @Configuration用于定义配置类,可以替换xml配置文件,
// 加了这个注解的类的内部包含一个或多个被@Bean注解的方法
@Configuration
public class WebConfig implements WebMvcConfigurer {/* <bean name="admin" class="com.situ.springboot.pojo.Admin"/>*/@Beanpublic Admin createAdmin() {return new Admin();}//配置虚拟路径@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/pic/**").addResourceLocations("file:/D:/mypic/");WebMvcConfigurer.super.addResourceHandlers(registry);}/*<!-- 配置拦截器 --><mvc:interceptors><mvc:interceptor><mvc:mapping path="/**"/><mvc:exclude-mapping path=""/><bean class="com.situ.mvc.interceptor.MyInterceptor1"></bean></mvc:interceptor></mvc:interceptors>*/// 这个方法用来注册拦截器,我们写的拦截器需要在这里配置才能生效@Overridepublic void addInterceptors(InterceptorRegistry registry) {//把登录的拦截器配置上才能起作用// addPathPatterns("/**") 拦截器拦截所有的请求,静态资源也拦截了,需要放行// excludePathPatterns 代表哪些请求不需要拦截registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/page/login", "/admin/login", "/admin/logout", "/static/**");}
}

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

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

相关文章

vue-router实现

实现一个简化版的 vue-router 可以帮助我们更好地理解 Vue 路由是如何工作的。Vue Router 主要的功能是基于浏览器的 URL 来管理组件的显示&#xff0c;能够根据 URL 变化切换不同的视图。下面是一个简化版的实现&#xff0c;用于帮助你理解基本的路由机制。 创建一个简单的 V…

Redis 服务器:核心功能与优化实践

Redis 服务器:核心功能与优化实践 引言 Redis(Remote Dictionary Server)是一款高性能的键值对存储系统,广泛用于缓存、消息队列、实时排行榜等场景。本文将深入探讨Redis服务器的核心功能,并提供一些优化实践的技巧,以帮助读者更好地理解和运用Redis。 Redis的核心功…

openEuler24.03 LTS下安装MySQL8

前提条件 拥有openEuler24.03 LTS环境&#xff0c;可参考&#xff1a;Vmware下安装openEuler24.03 LTS 步骤 卸载原有mysql及mariadb sudo systemctl stop mysql mysqld 2>/dev/null sudo rpm -qa | grep -i mysql\|mariadb | xargs -n1 sudo rpm -e --nodeps 2>/dev/…

GLOW-TTS

我首先需要理解用户的指令&#xff0c;用户希望我翻译文章的3.1节“Training and Inference Procedures”部分。为了完成这个任务&#xff0c;我需要仔细阅读文章的3.1节&#xff0c;理解其中的技术细节和概念&#xff0c;然后将这些内容准确地翻译成中文。 在阅读3.1节时&…

【算法思想】高精度

引入 首先了解&#xff1a; 1. int 范围为10^9 2. long long 范围数量级为10^18 如果超过该数量级&#xff0c;该怎么办&#xff1f; ——这就是高精度、大数的算法问题 加法 输入两个整数a,b,输出他们的和&#xff08;<10的500次方&#xff09; 核心是加法的核心——》每…

【失败了】LazyGraphRAG利用本地ollama提供Embedding model服务和火山引擎的deepseek API构建本地知识库

LazyGraphRAG测试结果如下 数据&#xff1a; curl https://www.gutenberg.org/cache/epub/24022/pg24022.txt -o ./ragtest/input/book.txt 失败了 气死我也&#xff01;&#xff01;&#xff01;对deepseek-V3也不是很友好啊&#xff0c;我没钱prompt 微调啊&#xff0c;晕死…

ccfcsp3402矩阵重塑(其二)

//矩阵重塑&#xff08;其二&#xff09; #include<iostream> using namespace std; int main(){int n,m,t;cin>>n>>m>>t;int c[10000][10000];int s0,sum0;int d[10000],k[100000];for(int i0;i<n;i){for(int j0;j<m;j){cin>>c[i][j];d[s…

算法-除自身以外数组的乘积

力扣题目&#xff1a;238. 除自身以外数组的乘积 - 力扣&#xff08;LeetCode&#xff09; 题目描述&#xff1a; 给你一个整数数组 nums&#xff0c;返回 数组 answer &#xff0c;其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。 题目数据 保证 数组 nums…

Unity Shader - UI Sprite Shader之简单抠图效果

Sprite抠图效果&#xff1a; 前言 在PhotoShop中我们经常会用到抠图操作&#xff0c;现在就用Shader实现一个简单的抠图效果。 实现原理&#xff1a; 使用当前像素颜色与需要抠掉的颜色相减作比较&#xff0c;然后与一个指定的阈值比较以决定是否将其显示出来&#xff1b; U…

【Mac】安装 Parallels Desktop、Windows、Rocky Linux

一、安装PD 理论上&#xff0c;PD只支持试用15天&#xff01;当然&#xff0c;你懂的。 第一步&#xff0c;在 Parallels Desktop for Mac 官网 下载 Install Parallels Desktop.dmg第二步&#xff0c;双击 Install Parallels Desktop.dmg 第三步&#xff0c;双击安装Paralle…

学习单片机需要多长时间才能进行简单的项目开发?

之前有老铁问我&#xff0c;学单片机到底要多久&#xff0c;才能进行简单的项目开发&#xff1f;是三个月速成&#xff0c;还是三年磨一剑&#xff1f; 今天咱们就来聊聊这个话题&#xff0c;我不是什么高高在上的专家&#xff0c;就是个踩过无数坑、烧过几块板子的“技术老友”…

pyqt 上传文件或者文件夹打包压缩文件并添加密码并将密码和目标文件信息保存在json文件

一、完整代码实现 import sys import os import json import pyzipper from datetime import datetime from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout,QPushButton, QLineEdit, QLabel, QFileDialog,QMessageBox, QProgressBar) from PyQt5.…

centos操作系统上传和下载百度网盘内容

探序基因 整理 进入百度网盘官网百度网盘 客户端下载 下载linux的rpm格式的安装包 在linux命令行中输入&#xff1a;rpm -ivh baidunetdisk_4.17.7_x86_64.rpm 出现报错&#xff1a; 错误&#xff1a;依赖检测失败&#xff1a; libXScrnSaver 被 baidunetdisk-4.17.7-1.x8…

LeetCode134☞加油站

关联LeetCode题号134 本题特点 贪心局部最优解-部分差值 如果小于0&#xff08;消耗大于油站油量&#xff09; 就从下一个加油站开始&#xff0c;因为如果中间有小于0的情况 当前站就不可能是始发站&#xff0c;整体最优解-整体差值 如果小于0 &#xff0c;那么就是不能有始发…

基于 Verilog 的时序设计:从理论到实践的深度探索

在数字电路设计领域,时序设计是一个至关重要的环节,它涉及到组合逻辑电路与时序逻辑电路的设计差异、时钟信号的运用以及触发器的工作原理等多个方面。本文将围绕基于 Verilog 的时序设计实验展开,详细阐述实验过程、代码实现以及结果分析,帮助读者深入理解时序设计的核心概…

蓝牙系统的核心组成解析

一、硬件层&#xff1a;看得见的物理载体 1. 射频模块&#xff08;Radio Frequency Module&#xff09; 专业描述&#xff1a;工作在2.4GHz ISM频段&#xff0c;支持GFSK/π/4 DQPSK/8DPSK调制方式 功能类比&#xff1a;相当于人的"嘴巴"和"耳朵" 发射端…

猎豹移动(Cheetah Mobile)

本文来自腾讯元宝 公司背景与发展历程 ​成立与早期定位 猎豹移动成立于2010年11月&#xff0c;由金山安全与可牛影像合并而成&#xff0c;初期以移动安全工具和清理软件为核心业务。其明星产品包括《猎豹清理大师》&#xff08;Clean Master&#xff09;和《猎豹浏览器》&…

go的gmp

参考链接&#xff1a;https://www.bilibili.com/video/BV19r4y1w7Nx Golang的GMP调度模型(协程调度器)是其并发编程的核心。GMP代表Goroutine、Machine和Processor三个关键组成部分。Goroutine是Go语言中的轻量级线程&#xff0c;Machine是操作系统的线程&#xff0c;Processor…

Vue3-高级特性

一、Vue中自定义指令 1.认识自定义指令 在Vue的模板语法中我们学习过各种各样的指令&#xff1a;v-show、v-for、v-model等等&#xff0c;除了使用这些指令之外&#xff0c;Vue也允许我们来 自定义自己的指令。 注意&#xff1a;在Vue中&#xff0c;代码的复用和抽象主要还是…

【量化策略】动量突破策略

【量化策略】动量突破策略 &#x1f680;量化软件开通 &#x1f680;量化实战教程 技术背景与应用场景 动量突破策略是一种基于市场趋势的量化交易策略&#xff0c;它通过识别资产价格的动量变化来预测未来的价格走势。这种策略适用于那些价格波动较大、趋势明显的市场环境…