本文意在整理开发过程常用或可以提高效率的@注解说明及使用方法, 在后续开发过程中无需四处百度, 文章尚在不断完善中 ,如有写的不好的地方欢迎指正
- @Data
- @Accessors(chain = true)
- @Slf4j
- @SpringBootTest
- @Resource
- @Test
@Data //lombok包提供, 用于默认生成get,set,toString,equals等方法
@Accessors(chain = true)//lombok包提供, 用于将get,set方法返回其对象本身, 使其支持链式调用, 搭配@Data使用
public class FilePathData {
private long id;
private String fileName = "";//文件名
private String filePath = "";//完整路径
private String fileText = "";//文件内容
}
package com.example.esbexceltobundle.service.impl;
import com.example.esbexceltobundle.model.FilePathData;
import com.example.esbexceltobundle.service.IFilePathDataService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
@Slf4j //lombok提供,用于生成日志log对象
@SpringBootTest //测试类启动时自动加载SpringBoot
class FilePathDataServiceImplTest {
@Resource//SpringBoot框架自动注入对象, 获取实例无需使用new
IFilePathDataService filePathDataService;
@Test//标注这是个测试方法
void selectAll() {
List filePathData = filePathDataService.selectAll();
log.info(filePathData.toString());//由@Slf4j产生的影响
}
@Test //标注这是个测试方法
void insertOneData() {
FilePathData filePathData = new FilePathData();
filePathData.setFilePath("测试").setFileName("测试名").setFileText("text");//由@Accessors(chain = true)产生的影响,
filePathDataService.insertOneData(filePathData);//@Resource产生的影响
}
}
- @Transactional
- @Rollback
使用示例
package com.example.esbexceltobundle.service.impl;
import com.example.esbexceltobundle.model.FilePathData;
import com.example.esbexceltobundle.service.IFilePathDataService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Slf4j
@SpringBootTest
class FilePathDataServiceImplTest {
@Resource
IFilePathDataService filePathDataService;
@Test
@Transactional //开启事务处理, 用于回滚
@Rollback //测试完插入后进行回滚
void insertOneData() {
FilePathData filePathData = new FilePathData();
filePathData.setFilePath("测试").setFileName("测试名").setFileText("text");
filePathDataService.insertOneData(filePathData);
}
}



