使用Spring Boot按部分(分页)发送电子邮件?

huangapple go评论80阅读模式
英文:

Sending email with Spring Boot by parts (with pagination)?

问题

我想通过电子邮件发送来自数据库的用户信息。

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    @Query(value = "SELECT user from User user where user.reportDate >= :ago order by user.reportDate desc")
    List<User> findAllWithDateAfter(@Param("ago") LocalDate ago);
}

经典的用户实体:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private String name;

    // 获取器和设置器等等

}

服务:

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public List<User> getUsersForRequiredDays(int days) {
        LocalDate daysAgoDate = LocalDate.now().minusDays(days);
        return userRepository.findAllWithDateAfter(daysAgoDate);
    }
}

但我想限制此查询。如果从此查询中检索超过30行,我想将其分成几部分。而不是发送包含100行的一封电子邮件,我想发送四封电子邮件(30/30/30/10)。我听说过分页,但我不知道如何应用它到邮件。

我按计划发送电子邮件:

public class ScheduledMailSenderService {

    private final MailSenderService mailSender;

    @Scheduled(cron = "${schedule.cron.update}")
    public void send() {
        log.info("Scheduled sending started");
        try {
            mailSender.send();
        } catch (MessagingException | IOException | TemplateException e) {
            log.error("Error occurred while sending email message:: {}", e.toString());
        }
        log.info("Scheduled sending finished");
    }

}

发送者服务中的 send 方法(在上面的 try 块中):

public void send() throws MessagingException, IOException, TemplateException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        List<User> users = userService.getUsersForRequiredDays(interval); //在这里使用来自数据库的信息
        setMimeMessageSettings(mimeMessage, subject, emailTo, reportTable, from);
        mailSender.send(mimeMessage);
    }
英文:

I want to send infomation about users from database through email.

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    @Query(value = "SELECT user from User user where user.reportDate >= :ago order by user.reportDate desc")
    List<User> findAllWithDateAfter(@Param("ago") LocalDate ago);
}

Classic User entity:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private String name;

    /getters and setters, etc

}

Service:

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public List<User> getUsersForRequiredDays(int days) {
        LocalDate daysAgoDate = LocalDate.now().minusDays(days);
        return badUtmMarksRepository.findAllWithDateAfter(daysAgoDate);
    }
}

But I want to limit this query. If i'll retrieve more than 30 rows from this query, I want to divide it into parts. Instead of sending one email with 100 rows, I want to send four emails (30/30/30/10). I heard about pagination, but I don't know how to apply it for mail.

I'm sending emails on schedule:

public class ScheduledMailSenderService {

    private final MailSenderService mailSender;

    @Scheduled(cron = "${schedule.cron.update}")
    public void send() {
        log.info("Scheduled sending started");
        try {
            mailSender.send();
        } catch (MessagingException | IOException | TemplateException e) {
            log.error("Error occurred while sending email message:: {}", e.toString());
        }
        log.info("Scheduled sending finished");
    }

}

Method send from senderService (in try block above):

public void send() throws MessagingException, IOException, TemplateException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        List<User> users = userService.getUsersForRequiredDays(interval); //im using info from db here
        setMimeMessageSettings(mimeMessage, subject, emailTo, reportTable, from);
        mailSender.send(mimeMessage);
    }

答案1

得分: 1

你的 repo 方法应该是:

List<User> findAllWithDateAfter(Pageable pageable, @Param("ago") LocalDate ago);

在调用该方法时,你应该像这样做:

@Autowired
UserRepository userRepository;
// ...
int size;
int index;

*** 填充 size 和 index 变量 ***
PageRequest pageRequest = PageRequest.of(index, size);

userRepository.findAllWithDateAfter(pageRequest, your-other-parameter);
英文:

Your repo method should be:

List&lt;User&gt; findAllWithDateAfter(Pageable pageable, @Param(&quot;ago&quot;) LocalDate ago);

and when calling the method you should do something like :

@Autowired
UserRepository userRepository; 
...
int size;
int index;

*** fill the size and index variables ***
PageRequest pageRequest = PageRequest.of(index, size);

userRepository.findAllWithDateAfter(pageRequest, your-other-parameter);

huangapple
  • 本文由 发表于 2020年10月13日 16:11:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64331199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定