有用代码笔记

线程状态

 

 


synchronized
锁方法的时候,这个方法最多只能一个线程进行访问,如
public synchronized void run() {
锁代码块的时候,这个代码块最多只能用一个线程访问。如synchronized (this) {


@SneakyThrows
源码中是try catch之后,再抛出异常



用inputStream读取json请求参数
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
ServletInputStream inputStream = httpServletRequest.getInputStream();
byte[] byteArr = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int length;
while ((length = inputStream.read(byteArr)) != -1) {
out.write(byteArr, 0, length);
}
String requestBody = new String(out.toByteArray(), "UTF-8");

 

@RequiredArgsConstructor 替代@Autowrired

导入lombok依赖

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

类上加上@RequiredArgsConstructor,需要注入的类要用final声明,或者使用@NonNull

@RestController// 作用在类上
@RequiredArgsConstructor
public class TestController {
    // 必须声明为final类型
    private final TestService testService;
    // 或者使用Lombok的注解
    @NonNull
    private TestMapper testMapper;
     
    @GetMapping("/hello3")
    public int hello3(){
        Test test = new Test();
        test.setName("sss");
        test.setId("0");
        return testMapper.insert(test);
    }
}

 参考文档:https://www.jb51.net/article/244602.htm

 

posted on 2022-08-06 10:50  周公  阅读(21)  评论(0编辑  收藏  举报

导航