后端项目开发笔记

Maven打包与JDK版本不对应解决方法

我这里使用jdk8。

复制代码
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

依赖解析

  • 每个SpringBoot项目都需要该父项目

    复制代码
      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.7.10</version>
      </parent>
  • web开发需要该依赖,像RestController,GetMapping,WebMvcConfigurer,org.springframework.web都是该依赖的

    复制代码
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
  • mysql驱动,没有这个的话,配置数据库时driver-class-name会爆红

    复制代码
      	spring:
      	  datasource:
      	    driver-class-name: com.mysql.cj.jdbc.Driver
      	    url: jdbc:mysql://localhost:3306/xxxxx
      	    username: root
      	    password: xxxxxx
    
          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>8.0.23</version>
          </dependency>
  • 添加该依赖后可以使用@Data等注解

    复制代码
          <dependency>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
          </dependency>
  • Spring测试依赖

    复制代码
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-test</artifactId>
          </dependency>
  • Mybatis依赖

    复制代码
          <dependency>
              <groupId>org.mybatis.spring.boot</groupId>
              <artifactId>mybatis-spring-boot-starter</artifactId>
              <version>2.1.2</version>
          </dependency>
  • 可以生成图形验证码

    复制代码
          <!-- 添加图形验证码依赖 -->
          <dependency>
              <groupId>cn.hutool</groupId>
              <artifactId>hutool-captcha</artifactId>
              <version>5.8.6</version>
          </dependency>
  • redis依赖:配置后可以操作redis(StringRedisTemplate)

    配置信息:
    redis:
    database: 0
    timeout: 5000
    host: redis服务器地址
    port: 6379
    lettuce:
    pool:
    max-active: 20 #连接池最大连接数(使用负值表示没有限制)
    max-wait: -1 #连接池最大阻塞等待时间(使用负值表示没有限制)
    min-idle: 0 #连接池中的最大空闲连接
    max-idle: 10 #连接池中的最小空闲连接

    复制代码
          <!--redis-->
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-data-redis</artifactId>
          </dependency>
  • JWT依赖,可以用来生成token

    复制代码
          <!---jwt-->
          <dependency>
              <groupId>com.auth0</groupId>
              <artifactId>java-jwt</artifactId>
              <version>3.4.0</version>
          </dependency>
  • Common IO 是一个工具库,用来帮助开发IO功能

    复制代码
          <dependency>
              <groupId>commons-io</groupId>
              <artifactId>commons-io</artifactId>
              <version>2.6</version>
          </dependency>

拦截器配置

  • 先写一个拦截器

    @Slf4j
    public class JWTInterceptor implements HandlerInterceptor {

    复制代码
      @Override
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws MyException {
          String token = request.getHeader("Authorization");
          if(StringUtils.isEmpty(token)){
              response.setStatus(400);
              throw new MyException("token不能为空");
          }
          try {
              JWTUtil.verify(token);
          }  catch (Exception e) {
              log.error("token无效! 错误 ", e);
              response.setStatus(400);
              return false;
          }
          return true;
      }

    }

  • 配置拦截器

    @Configuration
    public class IntercaptorConfig implements WebMvcConfigurer {

    复制代码
      @Override
      public void addInterceptors(InterceptorRegistry registry) {
          registry.addInterceptor(new JWTInterceptor())
                  //拦截的路径
                  .addPathPatterns("/**")
                  //排除登录接口和一些静态资源
                  .excludePathPatterns("/user/login")
                  .excludePathPatterns("/static/images/**");
      }

    }

相关推荐
闪闪发亮的小星星1 天前
高斯光以及高斯光公式解释
笔记
cqbzcsq1 天前
CellFlow虚拟细胞论文阅读
论文阅读·人工智能·笔记·学习·生物信息
阿米亚波1 天前
【Windows】QEMU 启动 openEuler aarch64/arm64 架构系统 + 离线软件源
linux·windows·经验分享·笔记·架构·arm
自传.1 天前
尚硅谷 Vibe Coding|第三章(1) Claude Code深度使用与进阶技巧 学习笔记
笔记·学习·尚硅谷·vibecoding
.千余1 天前
【C++】模板进阶全解:非类型参数|全特化|偏特化|分离编译完全指南
开发语言·c++·笔记·学习·其他
自传.1 天前
尚硅谷 Vibe Coding|第二章 AI编程工具生态 学习笔记
笔记·学习·ai编程·尚硅谷·vibe coding
秋波。未央1 天前
Java Agent 开发 · Day 1 学习笔记(含作业完整标准答案)
java·笔记·学习
中屹指纹浏览器1 天前
2026指纹浏览器字体指纹、字体渲染偏差检测与全维度虚拟字体池搭建方案
经验分享·笔记
影寂ldy1 天前
WinForm PictureBox控件 + ImageList组件 完整笔记
开发语言·笔记·swift