————————————欢迎大家分享补充常用的一些内容————————————————
—————————作为个新人,如有不足之处或更多经验请大佬指出————————————
1.常用注解
1.1 参数校验
// 关于时间
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd")
@ApiModelProperty(value = "检验开始时间") //swagger所用
private Date startTimeOfSurvey;
// 判空 String 常用@NotBlan(notnull 和 去除首尾空格长度>0)
//id Integer类型常用@NotNull
@NotBlank
@ApiModelProperty(value = "样品编码")
private String sampleCode;
//Lombok 大家可以看一看 几个常用的
//get和set
@Data
@Getter
@Setter
//构造相关 无参构造 有参构造注解
@NoArgsConstructor
@AllArgsConstructor
controller 层的4种提交方式 get post delete put
1.2 跨域问题解决注解
加在controller层头上就OK
@CrossOrigin(
allowCredentials = "true",
allowedHeaders = {"*"},
methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE, RequestMethod.OPTIONS, RequestMethod.HEAD, RequestMethod.PUT, RequestMethod.PATCH},
origins = {"*"}
)
2.常用工具类
2.1 分页
//sampleParametersQueryDto 传入
PageHelper.startPage(QueryDto.getPageNum(), QueryDto.getPageSize());
List<Parameters> parametersList = this.parametersMapper.queryAll(QueryDto);
PageInfo<Parameters> pageInfo = new PageInfo<>(parametersList );
return pageInfo;
2.2 赋值工具类
// 一般用的工具类 会把对象1的成员变量直接赋值给对象2相同名称的成员变量(对象可以不同 成员名称相同就好)
BeanUtils.copyProperties(对象1, 对象2);
2.3 二维码生成工具类
public class BarcodeUtils {
/**
* 生成条形码文件
*
* @param msg 条形码的文本内容
* @param path 生成条形码的文件路径
* @return
*/
public static File generateFile(String msg, String path) {
File file = new File(path);
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
generateBarCode128(msg, 10.0, 0.3, true, false, outputStream);
} catch (Exception e) {
throw new RuntimeException(e);
}
return file;
}
/**
* 生成code128条形码
*
* @param message 要生成的文本
* @param height 条形码的高度
* @param width 条形码的宽度
* @param withQuietZone 是否两边留白
* @param hideText 隐藏可读文本
* @param outputStream 输出流
*/
public static void generateBarCode128(String message, Double height, Double width, boolean withQuietZone,
boolean hideText, OutputStream outputStream) {
Code128Bean bean = new Code128Bean();
// 分辨率,越大条形码就越大
int dpi = 150;
// 设置两侧是否留白
bean.doQuietZone(withQuietZone);
// 设置条形码高度和宽度
bean.setBarHeight((Double) ObjectUtils.defaultIfNull(height, 9.0D));
if (width != null) {
bean.setModuleWidth(width);
}
// 设置文本位置(包括是否显示)
if (hideText) {
bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
}
// 设置图片类型
String format = "image/png";
BitmapCanvasProvider canvas = new BitmapCanvasProvider(outputStream, format, dpi,
BufferedImage.TYPE_BYTE_BINARY, false, 0);
// 生产条形码
bean.generateBarcode(canvas, message);
try {
canvas.finish();
} catch (IOException e) {
//ByteArrayOutputStream won't happen
}
}
}
2.4 流水号生成工具类
不是特别严谨 小项目可以用 依赖缓存生成流水号 格式:开头+日期+流水 大佬可以优化一下
public class SerialNumberUtil {
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 自动生成每日递增编号
* @param titleText (编码头部默认字符 如:PO20220101001 则为PO)
* @return 生成编码
*/
public String contextLoads(String titleText) {
RedisAtomicLong entityIdCounter = new RedisAtomicLong(titleText, redisTemplate.getConnectionFactory());
Long counter = entityIdCounter.incrementAndGet();
DecimalFormat format=new DecimalFormat("000");
entityIdCounter.expire(1, TimeUnit.DAYS);
return titleText + new SimpleDateFormat("yyyyMMdd").format(new Date()) + format.format(counter);
}
}
3.代码精简
除了前面的注解判断参数、一些工具类之外,我就说说我常用的
1.foreach 循环(比单纯的for 少写了不少)
for(double value : values) {
一般用来遍历List<对象> 或者迭代器啥的 对对象进行一些操作
}
2.String sex = lalala(peo.getsex()) ? "男" : "女"; //三元表达式
3. Assert.notNull(userId, "用户标识不能为空") 判空 int
StringUtils.isEmpty(String字符串); String类型
4.
4.踩坑
在mybatis中 时间比较判空startTimeOfSurvey != null 不能去 ==" " 不然会爆 sting不能与date相比较错误,其实不是代码的问题
<if test="startTimeOfSurvey != null">
and date_format ( quality_time_actual, '%Y-%m-%d' ) >=
date_format (to_date( #{startTimeOfSurvey}, 'yyyy-mm-dd'), '%Y-%m-%d')
</if>
>大于号 <小于号