基于 Spring Boot 博客系统开发(十一)

基于 Spring Boot 博客系统开发(十一)

本系统是简易的个人博客系统开发,为了更加熟练地掌握 SprIng Boot 框架及相关技术的使用。🌿🌿🌿
基于 Spring Boot 博客系统开发(十)👈👈

定时邮件发送实现

邮件服务依赖

 <!-- mail邮件服务启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

邮件服务配置

# 邮箱邮件发送服务配置
spring.mail.host=smtp.163.com

# 配置个人邮件账户和密码(密码是加密后的授权码)
spring.mail.username=xxxxxx@163.com
spring.mail.password=xxxxxx

163邮箱授权码获取:
登录163邮箱账号,进入设置开启 IMAP/SMTP服务和POP3/SMTP服务,然后点击新增授权码获取授权码。
在这里插入图片描述

邮件服务工具类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

@Component
public class MailUtils {
    @Autowired
    private JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    private String mailfrom;

    // 发送简单邮件
    public void sendSimpleEmail(String mailto, String title, String content) {
        //  定制邮件发送内容
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(mailfrom);
        message.setTo(mailto);
        message.setSubject(title);
        message.setText(content);
        // 发送邮件
        mailSender.send(message);
    }
}

发送邮件测试

@SpringBootTest
class BlogSystem01ApplicationTests {

    @Autowired
    private MailUtils mailUtils;
    @Value("${spring.mail.username}")
    private String mailto;

    @Test
    void contextLoads() {
        StringBuffer content = new StringBuffer();
        content.append("博客系统总访问量为:1人次").append("\n");
        content.append("博客系统总评论量为:1人次").append("\n");
        mailUtils.sendSimpleEmail(mailto,"个人博客系统流量统计情况",content.toString());
    }

}

定时器实现

编写定时器类

@Component
public class ScheduleTask {

    @Autowired
    private MailUtils mailUtils;

    @Value("${spring.mail.username}")
    private String mailto;

    @Autowired
    private StatisticMapper statisticMapper;

    /**
     * cron 一共可以有7个参数 以空格分开 其中年不是必须参数
     * [秒] [分] [小时] [日] [月] [周] [年],
     */
    @Scheduled(cron = "0 0 12 1 * ?")//定时邮件发送任务,每月1日中午12点整发送邮件
    //@Scheduled(cron = "0 */3 * * * ? ")//定时邮件发送任务,每三分钟执行一次
    public void sendEmail(){
        long totalvisit = statisticMapper.getTotalVisit();
        long totalComment = statisticMapper.getTotalComment();
        StringBuffer content = new StringBuffer();
        content.append("博客系统总访问量为:"+totalvisit+"人次").append("\n");
        content.append("博客系统总评论量为:"+totalComment+"人次").append("\n");
        mailUtils.sendSimpleEmail(mailto,"个人博客系统流量统计情况",content.toString());
    }
}

应用入口函数添加配置注解

@EnableScheduling
@SpringBootApplication
public class BlogSystem01Application {

    public static void main(String[] args) {
        SpringApplication.run(BlogSystem01Application.class, args);
    }
    
}

其中cron表达式格式:
{秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)}
*:表示所有值 比如用在日 表示每一天。
?:表示不指定值 比如周配置 表示不指定星期几执行。
/:表示递增触发 比如 用在分 5/20 从第五分钟开始 每增加20分钟执行一次。
-:表示区间 比如用在 1-6 表示一月到六月执行。

统计博客文章总访问量/总评论量,使用注解自定义SQL,StatisticMapper.java

@Mapper
public interface StatisticMapper extends BaseMapper<Statistic> {

	// 统计博客文章总访问量
    @Select("SELECT SUM(hits) FROM t_statistic")
    public long getTotalVisit();

    // 统计博客文章总评论量
    @Select("SELECT SUM(comments_num) FROM t_statistic")
    public long getTotalComment();

}

实现效果,每月1日中午12点整定时发送邮件

在这里插入图片描述

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值