All posts by 龙生
SpringBoot定时任务 @Scheduled详解
博主在最近的开发中又遇到了关于定时调度的开发任务,在定时调度其实有很多的第三方平台可以接入,但是其实在SpringBoot有自带的定时任务注解@Scheduled。@Scheduled可以通过注解配置快速实现方法的定时调度,直接在方法加上@Scheduled注解即可。 一.@Scheduled注解参数 1.cron参数 这个参数是最经常使用的参数,表示接收一个cron参数,cron它是一个表达式,最多接收7个参数,从左到右分别表示:秒 分 时 天 月 周 年;参数以空格隔开,其中年不是必须参数,可以省略。
1 2 3 4 5 6 7 8 |
/** * cron 一共可以有7个参数 以空格分开 其中年不是必须参数 * [秒] [分] [小时] [日] [月] [周] [年] * 一下表示 */ @Scheduled(cron ="0 0 0 * * * ?") public void testScheduledCron(){ } |
注意!!! 在使用时需要在类上添加注解@EnableScheduling,表示开启定时任务。 cron参数意义: 序号 含义 是否必填 入参范围 可填通配符 1 秒 是 0-59 , – * / 2 分 是 0-59 , – * / 3 时 是 0-23 , – * / 4 日 是 1-31 , – * ? / L W 5 月 是 1-12 , – * / 6 周(周一 ~ 周日) 是 1-7 , – * ? / L # 8 年 否 1970-2099 , – * / 常用通配符: *:表示所有值 比如用在日 表示每一天。 ?:表示不指定值 比如周配置 […]
View DetailsJava中QueryWrapper的基本使用
1.单表查询
1 2 3 4 5 6 7 8 9 |
@GetMapping("/list") public TableDataInfo list(Student student){ LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<Student>(); lqw.eq(Student::getName, student.getName()); lqw.like(Student::getClass,student.getClass()); lqw.between("age",student.getAge1(),student.getAge2()); lqw.orderByAsc("age"); List<Student> list = studentService.list(lqw); } |
对应的sql语句为:
1 |
select * from student where name = '?' and class like '%?%' and age between '?' and '?' order by '?' asc |
1.1、单表查询的基本用法 函数名 说明 例子 eq 等于 例:eq(“name”,“张子”) ===> name = ‘张子’ ne 不等于 例:ne(“name”,“张子”) ===> name <> ‘张子’ gt 大于 例:gt(“age”,“18”) ===> age>18 lt 小于 例:lt(“age”,“18”) ===> age<18 between 在值1到值2之间 例:between(“age”,18,30) ===> 18<age<30’ like 模糊查询 例:like(“name”,“张”) ===> name like ‘%张%’ isNull 字段为NULL 例:isNull(“name”) ===> name is null 2、多表查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
//Controller @GetMapping("/listAndClass") public TableDataInfo listAndClass(Student student) { QueryWrapper<Student > qw = new QueryWrapper<Student >(); if(StringUtils.isNotBlank(student.getName())){ qw.eq("s.name",student.getName()); } if(StringUtils.isNotBlank(student.getClassName())){ qw.like("c.name",student.getClassName()); } startPage(); List<Student > list = studentService.listAndClass(qw); return getDataTable(list); } //Service List<Student> listAndClass(QueryWrapper<Student> qw); //Service impl @Override public List<Student> listAndClass(QueryWrapper<Student> qw) { return this.getBaseMapper().listAndClass(qw); } //Mapper @Select("select s.*,c.name from student s left join class c on s.id = c.student_id "+ "${ew.customSqlSegment}") List<YwSpaqjgDj> listAndClass(@Param(Constants.WRAPPER) QueryWrapper<Student> qw); |
from:https://www.cnblogs.com/gongss/p/16727090.html
View Detailsfastjson的日期格式化
//SerializerFeature.WriteDateUseDateFormat 使用日期字段格式序列化(2017-01-01),而不是用时间戳表示日期 JSON.toJSONString(data, SerializerFeature.WriteDateUseDateFormat)); from:https://www.cnblogs.com/gossip/p/6963957.html
View DetailsMaven单、多仓库配置
1. 单独仓库配置 打开Maven配置文件 apache-maven-3.6.3\conf\settings.xml 在<mirrors></mirrors>标签里新增一个mirror配置即可。
1 2 3 4 5 6 7 8 |
<mirrors> <mirror> <id>aliyun</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> |
标签 作用 id 镜像的唯一标识 name 名称描述 url 地址 mirrorOf 指定镜像规则,什么情况下从镜像仓库拉取 mirrorOf规则 作用 * 匹配所有,所有内容都从镜像拉取 external:* 除了本地缓存的所有从镜像仓库拉取 central 覆盖默认的仓库 repo1,repo2 匹配仓库repo1和repo2,使用逗号分割多个远程仓库 比如:镜像配置的规则 <mirrorOf>repo1</mirrorOf>,意为:匹配到目标仓库 <id>repo1</id>; 所以maven认为目标仓库central被镜像了; 不再去 https://repo.maven.apache.org/maven2/ 地址下载jar包; 而是去镜像仓库 http://maven.aliyun.com/nexus/content/groups/public/ 下载jar包。 2. 多仓库配置 有时候我们需要的jar包公有仓库没有,此时就必须启用多仓库配置,使maven同时使用私有仓库和公有仓库。 在<profiles></profiles>标签里配置多个profile配置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<profiles> <profile> <id>aliyun-repo</id> <repositories> <repository> <id>aliyun</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> <profile> <id>jboss-repo</id> <repositories> <repository> <id>jboss</id> <url>http://repository.jboss.org/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> <profile> <id>maven-repo</id> <repositories> <repository> <id>maven2</id> <url>http://central.maven.org/maven2/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> <profiles> |
在<activeProfiles></activeProfiles>标签里使用<activeProfile></activeProfile>标签启用配置。
1 2 3 4 5 |
<activeProfiles> <activeProfile>aliyun-repo</activeProfile> <activeProfile>jboss-repo</activeProfile> <activeProfile>maven-repo</activeProfile> </activeProfiles> |
from:https://www.aliang.link/pages/df2aeb/#_2-%E5%A4%9A%E4%BB%93%E5%BA%93%E9%85%8D%E7%BD%AE
View DetailsCentOS7 执行yum 命令出错 One of the configured repositories 如何解决
新安装的一台云服务器, 执行yum命令出现了错误, One of the configured repositories failed (Unknown), 最后是Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again 。完整的错误信息是: 这里重点是最后一行 for repository: epel/x86_64. 说明是这个仓储出了问题。 解决办法1 删除这个仓库
1 |
rm -f mv /etc/yum.repos.d/epel.repo |
这样执行yum命令就可以正确的执行。不过少了 epel 仓库好多软件包不能用。 这里介绍 第二种解决方法 查看了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
cat /etc/yum.repos.d/epel.repo [epel] name=Extra Packages for Enterprise Linux 7 - $basearch #baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch failovermethod=priority enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 [epel-debuginfo] name=Extra Packages for Enterprise Linux 7 - $basearch - Debug #baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch/debug mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch failovermethod=priority enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 gpgcheck=1 [epel-source] name=Extra Packages for Enterprise Linux 7 - $basearch - Source #baseurl=http://download.fedoraproject.org/pub/epel/7/SRPMS mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch failovermethod=priority enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 gpgcheck=1 |
然后测试了一下网络
1 2 3 4 |
[root@lampfree.com ~]# ping download.fedoraproject.org ping: download.fedoraproject.org: Name or service not known [root@lampfree.com ~]# ping mirrors.fedoraproject.org ping: mirrors.fedoraproject.org: Name or service not known |
发现网络不通,这里就需要解决网络问题。 修改dns cat /etc/resolv.conf
1 2 |
nameserver 8.8.8.8 nameserver 8.8.4.4 |
from:https://blog.csdn.net/b_o_n_z_t_f/article/details/121195667
View DetailsName or service not known, ping域名报错
一. 问题现象
ping公网域名失败,提示Name or service not known,但可以ping通弹性公网IP。
二. 问题根因
出现该问题通常有三个原因:
/etc/resolv.conf未配置DNS地址或者DNS地址错误导致。
/etc/nsswitch.conf文件删除DNS解析记录导致。
/lib64/libnss_dns.so.2库文件丢失导致无法解析域名。
安装JDK出现内部错误61003
问题:安装java出现内部错误61003,安装成功输入java -version提示无法打开dll文件
原因:没有装visual c++2015 redistributable
解决方案 下载并安装该插件
安装 visual c++2015 redistributable
链接 https://www.microsoft.com/en-us/download/details.aspx?id=48145
View DetailsJava获取毫秒值
1.java.lang.System类 该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。
1 |
System.currentTimeMillis(); |
2.Calendar类 先由getInstance获取Calendar对象,然后用clear方法将时间重置为(1970.1.1 00:00:00),接下来用set方法设定指定时间,最后用getTimeMillis获取毫秒值。
1 2 3 4 5 6 |
Calendar calendar = Calendar.getInstance(); calendar.clear(); //指定时间的毫秒数 calendar.set(2021, 0, 1); long millis = calendar.getTimeInMillis(); |
※ Calendar 的 month 从 0 开始,也就是全年 12 个月由 0 ~ 11 进行表示。 3.java.util.Date类、SimpleDateFormat类 先由时间格式创建SimpleDateFormat对象,然后通过parse方法由指定时间创建Date对象,最后由Date对象的getTime方法获取毫秒值。
1 2 3 4 5 6 7 8 9 10 11 |
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd"); Date date = null; try { //指定时间的毫秒数 date = format.parse("2021-01-01"); } catch (ParseException e) { e.printStackTrace(); } long millis = date.getTime(); |
测试
1 2 3 4 5 6 7 8 9 10 |
public static void test() { long curTime1 = System.nanoTime(); System.out.println("System.currentTimeMillis() " + System.currentTimeMillis() + " 耗时(ns):" + (System.nanoTime() - curTime1)); long curTime2 = System.nanoTime(); System.out.println("new Date().getTime() " + new Date().getTime() + " 耗时(ns):" + (System.nanoTime() - curTime2)); long curTime3 = System.nanoTime(); System.out.println("Calendar.getInstance().getTimeInMillis() " + Calendar.getInstance().getTimeInMillis() + " 耗时(ns):" + (System.nanoTime() - curTime3)); } |
结果 System.currentTimeMillis() 1630315030949 耗时(ns):39600 new Date().getTime() 1630315030949 耗时(ns):500300 Calendar.getInstance().getTimeInMillis() 1630315030956 耗时(ns):19424400 参考: https://www.cnblogs.com/jpfss/p/10455524.html 转载请注明出处:BestEternity亲笔。 from:https://blog.csdn.net/BestEternity/article/details/119998914
View Details@Transactional 详解 示例
Spring 事务管理分为编码式和声明式的两种方式。编程式事务指的是通过编码方式实现事务;声明式事务基于AOP,将具体业务逻辑与事务处理解耦。
声明式事务管理使业务代码逻辑不受污染, 因此在实际使用中声明式事务用的比较多。
声明式事务有两种方式,一种是在配置文件中做相关的事务规则声明,另一种是基于@Transactional 注解的方式。