目录
- XML配置
-
- Mybatis
- [Maven XML配置](#Maven XML配置)
- Lombok
- JWT令牌
- 参数校验:jakarta.validation
- [MyBatis-Plus Boot3](#MyBatis-Plus Boot3)
- [yml 配置](#yml 配置)
-
- 应用名称配置
- 数据库连接配置(MySQL)
- [MyBatis 核心配置](#MyBatis 核心配置)
- 日志配置
XML配置
Mybatis
namespace: 替换为自己的Mapper接口全类名,例如com.hr.mybatis.mapper.UserInfoXmlMapper
xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="你的Mapper接口全类名">
<!-- 中间放可根据需求增删/修改SQL标签(如动态SQL、联表查询等)-->
</mapper>
Maven XML配置
Lombok
Lombok的核心作用是通过注解简化冗余代码编写
- 自动生成通用方法:如
@Getter/@Setter生成属性的getter/setter方法,@ToString生成 toString (),@EqualsAndHashCode生成相等性判断方法,@NoArgsConstructor/@AllArgsConstructor生成无参 / 全参构造器; - 简化特殊场景代码:@Data 整合上述常用注解(
Getter/Setter/ToString/EqualsAndHashCode/RequiredArgsConstructor),一键生成核心方法;@Slf4j自动创建日志对象,无需手动声明;@Builder快速实现建造者模式,简化对象构建;
xml
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
JWT令牌
JWT令牌的核心作用:实现跨域 / 分布式系统中的身份认证、信息传递(如用户身份、权限);
xml
<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-api -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-impl -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is
preferred -->
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
参数校验:jakarta.validation
标准化的参数校验框架,核心作用是通过注解快速实现参数合法性校验,替代手动 if-else 判断
- 核心方式: 通过注解标注在实体类属性、方法参数 / 返回值上(如 @NotNull、@NotBlank、@Min、@Max、@Email 等),定义校验规则;
- 执行校验: 结合 Spring 等框架时,通过 @Valid/@Validated 触发校验,校验不通过时抛出 ConstraintViolationException 异常,可统一捕获返回提示;
- 核心优势: 标准化、注解化、可扩展(支持自定义校验注解),简化参数校验逻辑,提升代码整洁度;
- 常用注解: @NotNull(非 null)、@NotBlank(字符串非空且非空白)、@Size(字符串 / 集合长度范围)、@Pattern(正则匹配)等。
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
MyBatis-Plus Boot3
MyBatis-Plus(简称 MP)是 MyBatis 的增强工具,MyBatis-Plus Boot3 是适配 Spring Boot 3.x 生态的版本(基于 Jakarta EE 规范,替代原 Java EE),核心是在 MyBatis 基础上做无侵入增强,大幅简化 CRUD 开发。
xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.15</version>
</dependency>
yml 配置
yaml
spring:
application:
name: xxx-xxx-xxx
datasource: # 配置数据库
url: jdbc:mysql://127.0.0.1:3306/java_blog_spring?characterEncoding=utf8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis: # 如果是使用Mybatis-Plus更改为mybatis-plus
mapper-locations: classpath:mapper/*.xml
configuration: # 配置打印 MyBatis 执行的 SQL
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true #自动驼峰转换
logging:
file:
name: spring-blog.log
应用名称配置
yaml
spring:
application:
name: xxxx
作用: 定义 Spring Boot 应用的名称,核心用途包括:
- 微服务架构中(如 Spring Cloud)作为服务注册的标识;
- 日志、监控等组件中区分不同应用;
- 配置加载、多环境隔离时的基础标识。
数据库连接配置(MySQL)
yaml
datasource: # 配置数据库
url: jdbc:mysql://127.0.0.1:3306/java_blog_spring?characterEncoding=utf8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
注意:数据库登录账号 / 密码,示例为本地默认的 root/123456,生产环境需替换为安全的账号密码
java_blog_spring:要连接的数据库名
127.0.0.1:3306:数据库服务器地址 + 端口(本地默认 3306);
MyBatis 核心配置
yaml
mybatis: # 如果是使用MyBatis-Plus更改为mybatis-plus
mapper-locations: classpath:mapper/*.xml #指定 MyBatis Mapper XML 文件的存放路径
configuration: # 配置打印 MyBatis 执行的 SQL
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #指定 SQL 日志实现:
map-underscore-to-camel-case: true #自动驼峰转换
日志配置
作用:指定 Spring Boot 日志的输出文件名称,默认生成在项目根目录下(也可指定绝对路径,如 name: /logs/spring-blog.log)。
yaml
logging:
file:
name: spring-blog.log