东莞有哪些做网站,做电影网站心得体会,吕梁网站建设公司,网络管理系统包括哪五大功能### 使用Spring Boot实现OA通知公告模块
使用Spring Boot框架实现一个支持多种形式公告发布、设置发布时间和有效期#xff0c;以及公告发布后推送通知的模块。
#### 项目结构
结构组织项目#xff1a; OA_Notification_Module/ ├── src/ │ ├── main/ │ │ …### 使用Spring Boot实现OA通知公告模块
使用Spring Boot框架实现一个支持多种形式公告发布、设置发布时间和有效期以及公告发布后推送通知的模块。
#### 项目结构
结构组织项目 OA_Notification_Module/ ├── src/ │ ├── main/ │ │ ├── java/ │ │ │ └── com/ │ │ │ └── example/ │ │ │ └── oamodule/ │ │ │ ├── controller/ │ │ │ │ └── AnnouncementController.java │ │ │ ├── model/ │ │ │ │ └── Announcement.java │ │ │ ├── repository/ │ │ │ │ └── AnnouncementRepository.java │ │ │ ├── service/ │ │ │ │ ├── AnnouncementService.java │ │ │ │ └── NotificationService.java │ │ │ └── OaNotificationModuleApplication.java │ │ └── resources/ │ │ ├── application.properties │ └── test/ │ └── java/ └── pom.xml
#### 1. Announcement 模型类
java package com.example.oamodule.model;
import javax.persistence.Entity; import javax.persistence.Id; import java.time.LocalDateTime;
Entity public class Announcement { Id private String id; private String type; // TEXT, IMAGE, VIDEO private String content; private LocalDateTime publishTime; private LocalDateTime expiryTime; public Announcement() {} public Announcement(String id, String type, String content, LocalDateTime publishTime, LocalDateTime expiryTime) { this.id id; this.type type; this.content content; this.publishTime publishTime; this.expiryTime expiryTime; } // Getters and Setters public String getId() { return id; } public void setId(String id) { this.id id; } public String getType() { return type; } public void setType(String type) { this.type type; } public String getContent() { return content; } public void setContent(String content) { this.content content; } public LocalDateTime getPublishTime() { return publishTime; } public void setPublishTime(LocalDateTime publishTime) { this.publishTime publishTime; } public LocalDateTime getExpiryTime() { return expiryTime; } public void setExpiryTime(LocalDateTime expiryTime) { this.expiryTime expiryTime; } public boolean isExpired() { return LocalDateTime.now().isAfter(expiryTime); } }
#### 2. AnnouncementRepository 接口
java package com.example.oamodule.repository;
import com.example.oamodule.model.Announcement; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;
import java.time.LocalDateTime; import java.util.List;
Repository public interface AnnouncementRepository extends JpaRepositoryAnnouncement, String { ListAnnouncement findByExpiryTimeAfter(LocalDateTime now); }
#### 3. AnnouncementService 服务类
java package com.example.oamodule.service;
import com.example.oamodule.model.Announcement; import com.example.oamodule.repository.AnnouncementRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import java.time.LocalDateTime; import java.util.List;
Service public class AnnouncementService { Autowired private AnnouncementRepository announcementRepository; public void createAnnouncement(String id, String type, String content, LocalDateTime publishTime, LocalDateTime expiryTime) { Announcement announcement new Announcement(id, type, content, publishTime, expiryTime); announcementRepository.save(announcement); System.out.println(Announcement created: announcement.getId()); } public ListAnnouncement getAllAnnouncements() { return announcementRepository.findAll(); } public ListAnnouncement getActiveAnnouncements() { return announcementRepository.findByExpiryTimeAfter(LocalDateTime.now()); } public void removeExpiredAnnouncements() { ListAnnouncement announcements announcementRepository.findAll(); for (Announcement announcement : announcements) { if (announcement.isExpired()) { announcementRepository.delete(announcement); } } } }
#### 4. NotificationService 服务类
java package com.example.oamodule.service;
import com.example.oamodule.model.Announcement; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service;
import java.util.List;
Service public class NotificationService { Autowired private JavaMailSender mailSender; public void sendNotifications(ListAnnouncement announcements) { for (Announcement announcement : announcements) { // 模拟推送到电子邮件 SimpleMailMessage message new SimpleMailMessage(); message.setTo(employeeexample.com); message.setSubject(New Announcement: announcement.getType()); message.setText(announcement.getContent()); mailSender.send(message); System.out.println(Sent notification for announcement: announcement.getId()); } } }
#### 5. AnnouncementController 控制器类
java package com.example.oamodule.controller;
import com.example.oamodule.model.Announcement; import com.example.oamodule.service.AnnouncementService; import com.example.oamodule.service.NotificationService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime; import java.util.List;
RestController RequestMapping(/announcements) public class AnnouncementController { Autowired private AnnouncementService announcementService; Autowired private NotificationService notificationService; PostMapping public void createAnnouncement(RequestBody Announcement announcement) { announcementService.createAnnouncement(announcement.getId(), announcement.getType(), announcement.getContent(), announcement.getPublishTime(), announcement.getExpiryTime()); notificationService.sendNotifications(List.of(announcement)); } GetMapping public ListAnnouncement getAllAnnouncements() { return announcementService.getAllAnnouncements(); } GetMapping(/active) public ListAnnouncement getActiveAnnouncements() { return announcementService.getActiveAnnouncements(); } DeleteMapping(/expired) public void removeExpiredAnnouncements() { announcementService.removeExpiredAnnouncements(); } }
#### 6. 应用主类
java package com.example.oamodule;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
SpringBootApplication public class OaNotificationModuleApplication { public static void main(String[] args) { SpringApplication.run(OaNotificationModuleApplication.class, args); } }
#### 7. application.properties 配置文件
properties spring.datasource.urljdbc:mysql://localhost:3306/oamodule spring.datasource.usernameroot spring.datasource.passwordpassword spring.jpa.hibernate.ddl-autoupdate
spring.mail.hostsmtp.example.com spring.mail.port587 spring.mail.usernameyour-emailexample.com spring.mail.passwordyour-email-password spring.mail.properties.mail.smtp.authtrue spring.mail.properties.mail.smtp.starttls.enabletrue
#### 8. 数据库初始化脚本
sql CREATE DATABASE oamodule;
USE oamodule;
CREATE TABLE announcements ( id VARCHAR(50) PRIMARY KEY, type VARCHAR(10), content TEXT, publish_time TIMESTAMP, expiry_time TIMESTAMP );
#### 9. pom.xml 示例
xml project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://www.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion groupIdcom.example/groupId artifactIdoamodule/artifactId version1.0-SNAPSHOT/version properties java.version11/java.version /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-jpa/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-mail/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId scoperuntime/scope /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency /dependencies build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId /plugin /plugins /build /project
使用Spring Boot框架实现了一个支持多种形式公告发布、设置发布时间和有效期以及公告发布后推送功能的OA通知公告模块。这个模块结合了JPA进行数据库操作并使用Spring Mail进行邮件推送。 文章转载自: http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.mcqhb.cn.gov.cn.mcqhb.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.bnmfq.cn.gov.cn.bnmfq.cn http://www.morning.xhsxj.cn.gov.cn.xhsxj.cn http://www.morning.zplzj.cn.gov.cn.zplzj.cn http://www.morning.mbpfk.cn.gov.cn.mbpfk.cn http://www.morning.nmkfy.cn.gov.cn.nmkfy.cn http://www.morning.pwmm.cn.gov.cn.pwmm.cn http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.bphqd.cn.gov.cn.bphqd.cn http://www.morning.wdprz.cn.gov.cn.wdprz.cn http://www.morning.gbsby.cn.gov.cn.gbsby.cn http://www.morning.rjznm.cn.gov.cn.rjznm.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn http://www.morning.wtwhj.cn.gov.cn.wtwhj.cn http://www.morning.tqxtx.cn.gov.cn.tqxtx.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.yqwsd.cn.gov.cn.yqwsd.cn http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.ddrdt.cn.gov.cn.ddrdt.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.dfrenti.com.gov.cn.dfrenti.com http://www.morning.nwfpl.cn.gov.cn.nwfpl.cn http://www.morning.xqxlb.cn.gov.cn.xqxlb.cn http://www.morning.lxhgj.cn.gov.cn.lxhgj.cn http://www.morning.mnrqq.cn.gov.cn.mnrqq.cn http://www.morning.jbkcs.cn.gov.cn.jbkcs.cn http://www.morning.hwnnm.cn.gov.cn.hwnnm.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.hknk.cn.gov.cn.hknk.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.cxryx.cn.gov.cn.cxryx.cn http://www.morning.ryrpq.cn.gov.cn.ryrpq.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.rsjf.cn.gov.cn.rsjf.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.mghgl.cn.gov.cn.mghgl.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.tddrh.cn.gov.cn.tddrh.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.xqgh.cn.gov.cn.xqgh.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.ltbwq.cn.gov.cn.ltbwq.cn http://www.morning.pbzlh.cn.gov.cn.pbzlh.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.simpliq.cn.gov.cn.simpliq.cn http://www.morning.hhxpl.cn.gov.cn.hhxpl.cn http://www.morning.kkysz.cn.gov.cn.kkysz.cn http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.hwtb.cn.gov.cn.hwtb.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.btjyp.cn.gov.cn.btjyp.cn http://www.morning.bmgdl.cn.gov.cn.bmgdl.cn http://www.morning.kpbgvaf.cn.gov.cn.kpbgvaf.cn http://www.morning.krrjb.cn.gov.cn.krrjb.cn http://www.morning.tkrdg.cn.gov.cn.tkrdg.cn http://www.morning.qjldz.cn.gov.cn.qjldz.cn http://www.morning.mflhr.cn.gov.cn.mflhr.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.nzxdz.cn.gov.cn.nzxdz.cn http://www.morning.sfwd.cn.gov.cn.sfwd.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.qqnh.cn.gov.cn.qqnh.cn http://www.morning.rggky.cn.gov.cn.rggky.cn http://www.morning.tturfsoc.com.gov.cn.tturfsoc.com