一.手动分页
写完分页产品又要加功能??
上代码
/**
* @Author xunChengCheng
* 手动分页封装工具类
* @Date 2023 11 07 10 55
**/
public class PageUtil {
public static <T> Page<T> manualPaginate(@RequestParam List<T> dataList,
@RequestParam(required = false,defaultValue = "1") Integer pageNumber,
@RequestParam(required = false,defaultValue = "10") Integer pageSize){
int total = dataList.size(); // 总记录数
int startIndex = (pageNumber - 1) * pageSize; // 起始索引
int endIndex = Math.min(startIndex + pageSize, total); // 结束索引
List<T> records = dataList.subList(startIndex, endIndex); // 获取当前页的数据
return new Page<T>(pageNumber, pageSize, total).setRecords(records);
}
}
导完这个分页请直接用List<dto>放进来 直接就是一手无缝衔接 return PageUtil.manualPaginate(dto,pageNumber,pageSize);
二.多选项过滤
如果是一对多的属性,在dto里面是List<String>,需要去传入一个值去找匹配关系的
public static <T extends A> List<T> filterByA(List<T> list, String a) {
if (StrUtil.isNotEmpty(a) && CollectionUtil.isNotEmpty(list)) {
list = list.stream().filter(
e -> {
if (CollectionUtil.isNotEmpty(e.getA())) {
return e.getA().contains(a);
}
return false;
}
).collect(Collectors.toList());
}
return list;
}
A是我们自定义的一个属性,类似于
/**
* @Author xunChengCheng
* @Time [2023-11-24][14:24]
**/
@Getter
@Setter
public class A{
private List<String> a;
}
让需要用到的类去继承A再去过滤
三.网址转二维码
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.hzsx.rent.common.util.DiyFileUtil;
import com.hzsx.rent.lang.fs.util.OSSFileUtils;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.FileUtils;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @Author xunChengCheng
* @Detail
* 输入一个网址生成对应的二维码
* 最好是 http格式
* https到150长度解析有问题
* @Time [2023-11-30][17:50]
**/
public class CodeUtils {
/**
* 拿链接转化二维码
* @param html
* @return
*/
public static String getCodeUrl(String html){
File userHomeDirFile = new File(System.getProperty("user.home"));
Path tmpUploadFolder = Paths.get(userHomeDirFile.getAbsolutePath()+"/home/xxxx/receipt/qrCode/", "tmpUploadFolder_");
File tmpUploadFolderDir = tmpUploadFolder.toFile();
if(!tmpUploadFolderDir.exists()){
boolean mkdirs = tmpUploadFolderDir.mkdirs();
}
File fileOfJpgFile = new File(Paths.get(tmpUploadFolderDir.getAbsolutePath(),DiyFileUtil.getUUIDFileName("11.jpg")).toFile().getAbsolutePath());
File generate = QrCodeUtil.generate(html, 300, 300,fileOfJpgFile);
DiskFileItem item = new DiskFileItem("file"
, MediaType.MULTIPART_FORM_DATA_VALUE
, true
, generate.getName()
, (int)generate.length()
, generate.getParentFile());
try {
OutputStream os = item.getOutputStream();
os.write(FileUtils.readFileToByteArray(generate));
} catch (IOException e) {
e.printStackTrace();
}
MultipartFile cMultiFile = new CommonsMultipartFile(item);
String s = OSSFileUtils.uploadByMultipartFile(cMultiFile);
if(fileOfJpgFile!=null&&fileOfJpgFile.exists()){
fileOfJpgFile.delete();
}
return s;
}
}
不知道为什么有时候稍微长点的网址解析不是很成功,大多数时候还是能用的