一,定时任务插件Quartz
二,计算日期之家相差天数
public static Date getBeforeOrAfterDate(Date date, int num) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();//获取日历
calendar.setTime(date);//当date的值是当前时间,则可以不用写这段代码。
calendar.add(Calendar.DATE, num);
Date d = calendar.getTime();//把日历转换为Date
return d;
}
三, MySQL(String类型转换日期)
<if test="faultMaxData != null and faultMaxData != '' and faultMinData != null and faultMinData !=''">
and fault_date between DATE(DATE_FORMAT('${faultMaxData}', '%Y-%m-%d'))
AND DATE(DATE_FORMAT('${faultMinData}', '%Y-%m-%d'))
四,插件
@PreAuthorize权限控制
| @PreAuthorize(hasPermi = "system:dealwith:list") | @Log(title = "粪污集散转运处理", businessType = BusinessType.EXPORT) |
五,MYSQL获取自增ID
<insert id="insertSysExcrementDealwith"
parameterType="cn.chinaunicom.smartpigfarm.tenant.system.domain.SysExcrementDealwith"
useGeneratedKeys="true" keyProperty="id">
<selectKey keyProperty="id" resultType="Integer" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
</inster>
六,无法自动装配。找不到 ‘EhBaseUserService’ 类型的 Bean。
@Repository
七,注入到Spring中 (用在方法上)交给Spring管理 (比较中立的注解,不知用什么注解的情况下)
@component
八,时间工具类
/**
* 时间工具类
*
* @author jsa
*/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
public static String YYYY = "yyyy";
public static String YYYY_MM = "yyyy-MM";
public static String YYYY_MM_DD = "yyyy-MM-dd";
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
/**
* 获取当前Date型日期
*
* @return Date() 当前日期
*/
public static Date getNowDate()
{
return new Date();
}
/**
* 获取当前日期, 默认格式为yyyy-MM-dd
*
* @return String
*/
public static String getDate()
{
return dateTimeNow(YYYY_MM_DD);
}
public static final String getTime()
{
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
}
public static final String dateTimeNow()
{
return dateTimeNow(YYYYMMDDHHMMSS);
}
public static final String dateTimeNow(final String format)
{
return parseDateToStr(format, new Date());
}
public static final String dateTime(final Date date)
{
return parseDateToStr(YYYY_MM_DD, date);
}
public static final String parseDateToStr(final String format, final Date date)
{
return new SimpleDateFormat(format).format(date);
}
public static final Date dateTime(final String format, final String ts)
{
try
{
return new SimpleDateFormat(format).parse(ts);
}
catch (ParseException e)
{
throw new RuntimeException(e);
}
}
/**
* 日期路径 即年/月/日 如2018/08/08
*/
public static final String datePath()
{
Date now = new Date();
return DateFormatUtils.format(now, "yyyy/MM/dd");
}
/**
* 日期路径 即年/月/日 如20180808
*/
public static final String dateTime()
{
Date now = new Date();
return DateFormatUtils.format(now, "yyyyMMdd");
}
/**
* 日期型字符串转化为日期 格式
*/
public static Date parseDate(Object str)
{
if (str == null)
{
return null;
}
try
{
return parseDate(str.toString(), parsePatterns);
}
catch (ParseException e)
{
return null;
}
}
/**
* 获取服务器启动时间
*/
public static Date getServerStartDate()
{
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
return new Date(time);
}
/**
* 计算两个时间差
*/
public static String getDatePoor(Date endDate, Date nowDate)
{
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟";
}
/**
* 获取当前时间几分钟后的时间
* @param time 当前时间
* @param min 分钟
* @return
*/
public static Date getFiveMinAfter(String time,int min) throws ParseException {
Date date = null;
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
Calendar now = Calendar.getInstance();
now.setTime(date);
now.add(Calendar.MINUTE,min);
return now.getTime();
}
}
九 ,计算保质期
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
//开始保质期时间
String s = "2021-08-09 10:12:05";
LocalDateTime ldt = LocalDateTime.parse(s,df);
LocalDateTime localDateTime = ldt.plusDays(60L);
System.out.println("保质期时间"+localDateTime);
long betweenDays = ChronoUnit.DAYS.between(ldt, localDateTime);
System.out.println("保质期天数"+betweenDays);
十,Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
spring
main:
allow-bean-definition-overriding: true
十一, eureka 报错org.springframework.cloud.context.environment.EnvironmentChangeEvent
新版springcloud整合eureka 必须添加熔断器依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
</dependencies>
十二 无法注入bean
十三,Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘mxController’ method
com.lnsoft.system.controller.smartfinance.MxController#saveHzn(HttpServletRequest, DimFinDwfxgrgys5WDf)
to {GET /system/user/saveHzn}: There is already ‘hzController’ bean method
此报错是controller 的方法地址相同 了
十四 ,ailed to req API:/nacos/v1/ns/instance after all servers([jeecg-boot-nacos:8848])
- 可能是C盘下的host没有配置,host文件内容会丢失
- 可能是配置文件中IP没配置正确
十五
问题:org.springframework.boot.SpringApplication - Application run failed
org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character ‘@’ that cannot start any token. (Do not use @ for indentation)
in ‘reader’, line 9, column 22:
server-addr: @config.server-addr@
解决:1看yml配置文件是否正确,
2刷新一下maven.
十六 自动获取字符串的数字
public static void main(String[] args) {
String str = "'1','12'";
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
十七 Linux下启动nacos报错Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
1.解决方法
使用standalone模式启动
./startup.sh -m standalone
2.查看Linux内存与磁盘
free -h
total used free shared buff/cache available
Mem: 3.7G 3.6G 183M 1.1M 195M 1.2G
Swap: 3.9G 55M 3.9G
命令释义
第一行Mem: 系统物理内存的使用情况
第二行Swap: swap交换内存的使用情况
total: 系统中内存的总量,
used: 已用内存总量(used = total-free-buffers-cache)
free: 空闲内存容量(真正尚未被使用的物理内存数)
shared: 共享内存使用的容量
buff/cache: buffers和cache所用总量的总和(buffers为内核缓冲区所用的内存,cache为页缓存和slabs所用的内存容量)
available:为估算值,是在不需要swapping内存的情况下,可用物理内存容量。它是从应用程序的角度看到的可用内存数量。
df -h
Filesystem:文件系统 Size: 分区大小 Used: 已使用容量 Avail: 还可以使用的容量 Use%: 已用百分比 Mounted on: 挂载点
十八,获取IP地址
package org.jeecg.common.util;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* IP地址
*
*/
public class IpUtils {
private static Logger logger = LoggerFactory.getLogger(IpUtils.class);
/**
* 获取IP地址
*
* 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址
*/
public static String getIpAddr(HttpServletRequest request) {
String ip = null;
try {
ip = request.getHeader("x-forwarded-for");
// CommonConstant.UNKNOWN(此方法是自己封装的状态码)
if (StringUtils.isEmpty(ip) || CommonConstant.UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isEmpty(ip) || ip.length() == 0 ||CommonConstant.UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isEmpty(ip) || CommonConstant.UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isEmpty(ip) || CommonConstant.UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (StringUtils.isEmpty(ip) || CommonConstant.UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
} catch (Exception e) {
logger.error("IPUtils ERROR ", e);
}
// //使用代理,则获取第一个IP地址
// if(StringUtils.isEmpty(ip) && ip.length() > 15) {
// if(ip.indexOf(",") > 0) {
// ip = ip.substring(0, ip.indexOf(","));
// }
// }
return ip;
}
}
//调用
//获取request
HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
//设置IP地址
String ipAddr = IpUtils.getIpAddr(request);