1、springboot重试注解
常见的场景:发送消息失败,调用远程服务失败,争抢锁失败,等等。这些错误可能是因为网络波动造成的,等待过后重处理就能成功。一般会用try/catch,while循环之类的语法来进行重处理,但是这样的做法缺乏统一性,并且不是很方便,要多写很多代码。然而spring-retry却可以通过注解,在不入侵原有业务逻辑代码的方式下,优雅的实现重处理功能。
<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> @EnableRetry //主启动类加注解 @SpringBootApplication public class SpringBootTestApplication { public static void main(String[] args) { SpringApplication.run(SpringBootTestApplication.class, args); } } /** * interceptor:方法拦截器的名称 * include:当抛出指定的异常时,才会重试。默认为空,为空时表示抛出任何异常都会重试 * value:作用同include * exclude:指定被忽略的异常,当抛出指定异常时,不会重试 * maxAttempts:最大重试次数,默认3次 * backoff: 重试等待策略,默认使用@Backoff,@Backoff默认的delay值为1000L,即1秒后重试;multiplier默认为0,若设为1.5,则表示下一次重试等待的时间为上一次的1.5倍 * @throws Exception */ @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000,multiplier = 1.5)) @Override public void testReTry() { int i = 1; System.out.println("测试重试"); i++; int j = 10/0; throw new RetryException("重试失败"); } @Recover public void recover(Exception e) { System.out.println("重试异常"); }
2、mybatis模糊查询
where username like CONCAT(CONCAT('%', #{userName}), '%');3、mysql自动插入当前日期
`creat_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
4、git常用命令
git init git clone url git add . git commit -m "提交说明" git push Git删除已经推送的内容 1. 查看历史记录,选择需要回退的版本,复制修订号 2. Git重置HEAD,选择硬,粘贴修订号 3. 强制推送
5、过滤系统无法识别字符
public static void main(String[] args) { String str = "(java) 版本 (1 1)。字符(!@#$%^&*()_+) 中文(!@#¥%……&**)"; str = str.replaceAll("[^a-zA-Z0-9!@#$%^&*()_+=!¥…()—·~` \\u4E00-\\u9FA5]", ""); System.out.println(str); }
6、精度分转元,元转分
/** * 元转分,确保price保留两位有效数字 * @return */ public static int changeY2F(double price) { Double price = 0.56; DecimalFormat df = new DecimalFormat("#.00"); int money = (int)(Double.parseDouble(df.format(price)) * 100); return money; } /** * 分转元,转换为bigDecimal在toString * @return */ public static String changeF2Y(int price) { return BigDecimal.valueOf(Long.valueOf(price)).divide(new BigDecimal(100)).toString(); }
联系客服