文章目录
- 1、mybatis-plus分页
- 2、自定义线程池,获取cpu核心线程数
- 2、合理设置线程池大小
- 3、高效线程池设置如下:
- 5、springboot项目初始化执行操作
- 6、JSONArray.parseArray()方法
- 7、@Value使用详情介绍(静态变量如何使用)
- 8、JSONObject解析List、对象,都需要先转为json字符串
- 9、swagger如何通过doc.html访问
- 10、Maven项目中<packaging>pom</packaging>的意思
- 11、springboot项目启动时 初始化操作
- 12、linux或windows下获取resource下的文件路径
- 13、控制台打印sql
- 14、端口占用
- 15、ping命令
1、mybatis-plus分页
@PostMapping("/listDirectSeedingPage")
public Rest<DirectSeedingPageListVo> listDirectSeedingPage(@Validated @RequestBody ListDirectSeedingPageDto req) {
QueryWrapper<DirectSeeding> queryWrapper = new QueryWrapper<>();
queryWrapper.select("unitGuid,liveSelectionTheme,houseName,notaryId,greffierId,CreateDate,houseToken,liveSelectionState,notaryName,greffierName");
queryWrapper.like(StringUtils.isNotEmpty(req.getLiveSelectionTheme()), "LiveSelectionTheme", req.getLiveSelectionTheme())
.like(StringUtils.isNotEmpty(req.getHouseName()), "HouseName", req.getHouseName())
.like(StringUtils.isNotEmpty(req.getGreffierName()), "GreffierName", req.getGreffierName())
.eq(StringUtils.isNotEmpty(req.getNotaryId()), "NotaryId", req.getNotaryId())
.eq(req.getLiveSelectionState() != null, "LiveSelectionState", req.getLiveSelectionState());
queryWrapper.orderByDesc("CreateDate");
int pageSize = req.getPageSize() == 0 ? 10 : req.getPageSize();
Page<DirectSeeding> page = new Page<>(req.getCurrentPage(), pageSize);
IPage<DirectSeeding> pageList = directSeedingService.page(page, queryWrapper);
pageList.setTotal(pageList.getTotal());
//先转为json字符串,再转为对应的类
DirectSeedingPageListVo result = JSONObject.parseObject(JSON.toJSONString(pageList), DirectSeedingPageListVo.class);
return Rest.success(result);
}
此处的
DirectSeedingPageListVo result = JSONObject.parseObject(JSON.toJSONString(pageList), DirectSeedingPageListVo.class);
可以高效的转换分页出参,要保证查询出的分页对象参数与MP的IPage参数一致
2、自定义线程池,获取cpu核心线程数
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import java.util.concurrent.*;
@Configuration
@EnableAsync
public class ExecutorConfig {
private static final Logger logger = LoggerFactory.getLogger(ExecutorConfig.class);
@Bean
public ExecutorService asyncServiceExecutor() {
logger.info("start asyncServiceExecutor");
//cpu 核心线程数
int cpuProcessors = Runtime.getRuntime().availableProcessors();
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("async-service-%d").build();
ThreadPoolExecutor executor = new ThreadPoolExecutor(2,cpuProcessors,30,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(80),threadFactory);
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}
2、合理设置线程池大小
3、高效线程池设置如下:
//获取当前机器的核数
public static final int cpuNum = Runtime.getRuntime().availableProcessors();
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(cpuNum);//核心线程大小
taskExecutor.setMaxPoolSize(cpuNum * 2);//最大线程大小
taskExecutor.setQueueCapacity(500);//队列最大容量
//当提交的任务个数大于QueueCapacity,就需要设置该参数,但spring提供的都不太满足业务场景,可以自定义一个,也可以注意不要超过QueueCapacity即可
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
taskExecutor.setAwaitTerminationSeconds(60);
taskExecutor.setThreadNamePrefix("BCarLogo-Thread-");
taskExecutor.initialize();
return taskExecutor;
}
5、springboot项目初始化执行操作
@Configuration
public class InitProject implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("init...");
String path = this.getClass().getResource("/").getPath();
new File(path+"/uploadfiles").mkdir();
}
}
6、JSONArray.parseArray()方法
将List转换为JSONArray T为实体类
List<T> list = new ArrayList<T>();
JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
JSONArray转List RequestDTO为实体类
JSONArray array = new JSONArray();
List<RequestDTO> list = JSONObject.parseArray(array.toJSONString(), RequestDTO.class);
字符串转List T为实体类
String str = ""; 或 String str = "[{T},{T}]"
List<T> list = JSONObject.parseArray(str,T.class);
7、@Value使用详情介绍(静态变量如何使用)
8、JSONObject解析List、对象,都需要先转为json字符串
List items = JSONObject.parseObject(JSON.toJSONString(resultMap.get("items")), List.class);
AllInOnePageOneVo allInOnePageOneVo = JSONObject.parseObject(JSON.toJSONString(items.get(0)), AllInOnePageOneVo.class);
9、swagger如何通过doc.html访问
导入以下依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>
<!-- swagger3-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
</dependency>
<!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.6.2</version>
</dependency>
10、Maven项目中<packaging>pom</packaging>的意思
11、springboot项目启动时 初始化操作
创建文件
@Configuration
public class InitProject implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
String systemBasePath = BaseUtil.getSystemBasePath();
new File(systemBasePath + "/uploadfiles").mkdirs();
}
}
/**
* 获取当前项目的路径地址
* 用于获取文件地址
* @return
*/
public static String getSystemBasePath(){
String osName = System.getProperties().getProperty("os.name");
if (osName.equals("Linux")) {
return System.getProperty("user.dir");
} else {
String rootPath = Class.class.getClass().getResource("/").getPath();
rootPath = rootPath.substring(1);
rootPath.substring(0,rootPath.lastIndexOf("/"));
return rootPath;
}
}
12、linux或windows下获取resource下的文件路径
Resource resource = new ClassPathResource("/replaceFile/个人借款协议.pdf");
templatePdfPath = resource.getFile().getAbsolutePath();
13、控制台打印sql
logging:
level:
com.sgc.product.dao: debug
14、端口占用
C:\Users\dell>netstat -ano|findstr "10000"
TCP 127.0.0.1:10000 0.0.0.0:0 LISTENING 6684
C:\Users\dell>tasklist|findstr "6684"
yundetectservice.exe 6684 Console 1 12,532 K
C:\Users\dell>taskkill /T /F /PID 6684
成功: 已终止 PID 6684 (属于 PID 8060 子进程)的进程。