@RequiredArgsConstructor注解使用教程

在我们写controller或者Service层的时候,需要注入很多的mapper接口或者另外的service接口,这时候就会写很多的@Autowired注解,代码看起来很乱
lombok提供了一个注解:

@RequiredArgsConstructor(onConstructor =@_(@Autowired))
写在类上可以代替@Autowired注解,需要注意的是在注入时需要用final定义,或者使用@notnull注解

@Cacheable

是 Spring Framework 中用于声明方法的返回结果可以被缓存的注解。这个注解会在方法被调用时首先检查缓存中是否存在对应的缓存项,如果存在则直接返回缓存的结果,如果不存在则执行方法并将结果缓存起来。

@Cacheable(key = "'id:' + #p0")

@Cacheable 注解用于声明某个方法的返回结果需要被缓存。
key 属性指定缓存的键。
"'id:' + #p0" 表示缓存的键由一个固定的字符串 'id:' 和方法的第一个参数(#p0)拼接而成。
假设这个注解用于某个方法,比如:

@Cacheable(key = "'id:' + #p0")
public User getUserById(Long id) {
    // 方法体
}

他是使用在方法上面的但是下面这个可以使用在类上面

@CacheConfig
@CacheConfig注解用于配置缓存的公共设置,例如缓存名称。它通常放在类级别上,表示这个类中的所有缓存操作都会使用这个配置。

@CacheConfig(cacheNames = "user")
public class UserService {
    // 这个类中的所有缓存操作都会使用缓存名称 "data"
}

这时他们缓存时的key为user::id:【参数1】
当然

@Cacheable(key = "'user:' + #p0.id")
public List<Long> getIds(User user) {}

它也可以为一个对象的获取对象的方法

使用SpEL来获取方法的参数

@Cacheable(key = "'list:' + T(your.package.SecurityUtils).getCurrentUserId()")
public List<Test> getList() {
    QueryWrapper<Test> queryWrapper = new QueryWrapper<>();
    Long userId = SecurityUtils.getCurrentUserId();
    System.out.println("用户信息");
    System.out.println(userId);
    queryWrapper.lambda().eq(Test::getId, 1);
    return mapper.selectList(queryWrapper);
}

@Transactional(rollbackFor = Exception.class)

是 Spring 框架中的一个注解,通常用于声明事务管理。具体来说,这个注解表示如果在被注解的方法中抛出了 Exception 类型的异常或其子类异常,事务将会回滚。
以下是这个注解的作用和常见使用场景的解释:

事务管理:Spring 提供了声明式事务管理,通过使用 @Transactional 注解,可以在方法或类上声明事务。事务管理器会在方法执行前开启一个事务,并在方法执行完成后根据执行情况提交或回滚事务。

回滚机制:默认情况下,@Transactional 注解只会在遇到 RuntimeException 类型的异常时才会回滚事务。如果你希望在遇到其他类型的异常(如 Exception)时也回滚事务,可以使用 rollbackFor 属性来指定。

代码示例:

@Service
public class MyService {
    
    @Transactional(rollbackFor = Exception.class)
    public void myTransactionalMethod() throws Exception {
        // 业务逻辑
        // 如果这里抛出 Exception 或其子类异常,事务将会回滚
    }
}

属性解释:

rollbackFor:指定哪些异常会触发事务回滚,可以指定一个或多个异常类。例如,rollbackFor = {Exception.class, CustomException.class}。
使用 @Transactional(rollbackFor = Exception.class) 注解可以确保在方法执行过程中发生任何异常时,事务都能正确回滚,避免数据的不一致性。

设置springboot扫描包文件夹

启动类新增注解
@ComponentScan(basePackages = {"com.demo1", "com.demo2"})

设置mybatisplus扫描的包文件夹

新增配置类
@Configuration
@MapperScan(value = {"com.demo1.mapper","com.demo2.mapper")
public class MyBatisPlusConfig {
  //这里什么都没有不用写东西
}

Java的Collections使用案例

Collections.min(getRoleList(getJson.getId).stream().map(role -> role.getString("level")).map(Integer::parseInt).collect(Collectors.toList()));

@ConditionalOnProperty注解使用

@ConditionalOnProperty(value = "backups.enabled", havingValue = "true",matchIfMissing = true)

value = 定义值
havingValue = 当value值和havingValue一样时注册这个bean
matchIfMissing = 默认是否注册 如果value不存在就默认value是true

MybatisPlus设置使用批量添加数据

这里正常创建好mapper信息

sevice里面 接口需要extends IService<YourEntity>
实现需要extends ServiceImpl<YourEntityMapper, YourEntity> implements YourEntityService
----------------------------------------------------
List<YourEntity> entityList = new ArrayList<>();
// 创建一些实体对象
entityList.add(new YourEntity(1, 1, 100, 1)); // 示例数据
entityList.add(new YourEntity(2, 2, 0, 2));   // 示例数据
// 批量插入
yourEntityService.saveBatch(entityList);
MyBatis-Plus 的 saveBatch 方法默认一次提交全部数据,你可以通过指定批次数量来分批插入数据,避免单次提交过多数据导致的性能问题
yourEntityService.saveBatch(entityList, 1000); // 每1000条数据提交一次

项目测试时使用@SpringBootTest和@RunWith(SpringRunner.class)

@SpringBootTest
@RunWith(SpringRunner.class)
public class Test {}

swagger拦截登录

<!--swagger拦截登录-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.3</version>
        </dependency>