一个基于Spring Boot的智慧养老平台

news/2025/1/13 15:12:05/

以下是一个基于Spring Boot的智慧养老平台的案例代码。这个平台包括老人信息管理、健康监测、紧急呼叫、服务预约等功能。代码结构清晰,适合初学者学习和参考。
在这里插入图片描述

1. 项目结构

src/main/java/com/example/smartelderlycare├── controller│   ├── ElderlyController.java│   ├── HealthRecordController.java│   ├── EmergencyAlertController.java│   └── ServiceAppointmentController.java├── model│   ├── Elderly.java│   ├── HealthRecord.java│   ├── EmergencyAlert.java│   └── ServiceAppointment.java├── repository│   ├── ElderlyRepository.java│   ├── HealthRecordRepository.java│   ├── EmergencyAlertRepository.java│   └── ServiceAppointmentRepository.java├── service│   ├── ElderlyService.java│   ├── HealthRecordService.java│   ├── EmergencyAlertService.java│   └── ServiceAppointmentService.java└── SmartElderlyCareApplication.java

2. 依赖配置 (pom.xml)

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>

3. 实体类 (model 包)

java_65">Elderly.java
java">package com.example.smartelderlycare.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class Elderly {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;private int age;private String address;private String contactNumber;// Getters and Setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getContactNumber() {return contactNumber;}public void setContactNumber(String contactNumber) {this.contactNumber = contactNumber;}
}
java_128">HealthRecord.java
java">package com.example.smartelderlycare.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDate;@Entity
public class HealthRecord {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private Long elderlyId;private LocalDate recordDate;private double bloodPressure;private double heartRate;private String notes;// Getters and Setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public Long getElderlyId() {return elderlyId;}public void setElderlyId(Long elderlyId) {this.elderlyId = elderlyId;}public LocalDate getRecordDate() {return recordDate;}public void setRecordDate(LocalDate recordDate) {this.recordDate = recordDate;}public double getBloodPressure() {return bloodPressure;}public void setBloodPressure(double bloodPressure) {this.bloodPressure = bloodPressure;}public double getHeartRate() {return heartRate;}public void setHeartRate(double heartRate) {this.heartRate = heartRate;}public String getNotes() {return notes;}public void setNotes(String notes) {this.notes = notes;}
}
java_201">EmergencyAlert.java
java">package com.example.smartelderlycare.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;@Entity
public class EmergencyAlert {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private Long elderlyId;private LocalDateTime alertTime;private String message;// Getters and Setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public Long getElderlyId() {return elderlyId;}public void setElderlyId(Long elderlyId) {this.elderlyId = elderlyId;}public LocalDateTime getAlertTime() {return alertTime;}public void setAlertTime(LocalDateTime alertTime) {this.alertTime = alertTime;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}
java_256">ServiceAppointment.java
java">package com.example.smartelderlycare.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;@Entity
public class ServiceAppointment {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private Long elderlyId;private LocalDateTime appointmentTime;private String serviceType;private String notes;// Getters and Setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public Long getElderlyId() {return elderlyId;}public void setElderlyId(Long elderlyId) {this.elderlyId = elderlyId;}public LocalDateTime getAppointmentTime() {return appointmentTime;}public void setAppointmentTime(LocalDateTime appointmentTime) {this.appointmentTime = appointmentTime;}public String getServiceType() {return serviceType;}public void setServiceType(String serviceType) {this.serviceType = serviceType;}public String getNotes() {return notes;}public void setNotes(String notes) {this.notes = notes;}
}

4. 仓库接口 (repository 包)

java_322">ElderlyRepository.java
java">package com.example.smartelderlycare.repository;import com.example.smartelderlycare.model.Elderly;
import org.springframework.data.jpa.repository.JpaRepository;public interface ElderlyRepository extends JpaRepository<Elderly, Long> {
}
java_334">HealthRecordRepository.java
java">package com.example.smartelderlycare.repository;import com.example.smartelderlycare.model.HealthRecord;
import org.springframework.data.jpa.repository.JpaRepository;public interface HealthRecordRepository extends JpaRepository<HealthRecord, Long> {
}
java_346">EmergencyAlertRepository.java
java">package com.example.smartelderlycare.repository;import com.example.smartelderlycare.model.EmergencyAlert;
import org.springframework.data.jpa.repository.JpaRepository;public interface EmergencyAlertRepository extends JpaRepository<EmergencyAlert, Long> {
}
java_358">ServiceAppointmentRepository.java
java">package com.example.smartelderlycare.repository;import com.example.smartelderlycare.model.ServiceAppointment;
import org.springframework.data.jpa.repository.JpaRepository;public interface ServiceAppointmentRepository extends JpaRepository<ServiceAppointment, Long> {
}

5. 服务层 (service 包)

java_372">ElderlyService.java
java">package com.example.smartelderlycare.service;import com.example.smartelderlycare.model.Elderly;
import com.example.smartelderlycare.repository.ElderlyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class ElderlyService {@Autowiredprivate ElderlyRepository elderlyRepository;public List<Elderly> getAllElderly() {return elderlyRepository.findAll();}public Elderly getElderlyById(Long id) {return elderlyRepository.findById(id).orElse(null);}public Elderly saveElderly(Elderly elderly) {return elderlyRepository.save(elderly);}public void deleteElderly(Long id) {elderlyRepository.deleteById(id);}
}
java_407">HealthRecordService.java
java">package com.example.smartelderlycare.service;import com.example.smartelderlycare.model.HealthRecord;
import com.example.smartelderlycare.repository.HealthRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class HealthRecordService {@Autowiredprivate HealthRecordRepository healthRecordRepository;public List<HealthRecord> getAllHealthRecords() {return healthRecordRepository.findAll();}public HealthRecord getHealthRecordById(Long id) {return healthRecordRepository.findById(id).orElse(null);}public HealthRecord saveHealthRecord(HealthRecord healthRecord) {return healthRecordRepository.save(healthRecord);}public void deleteHealthRecord(Long id) {healthRecordRepository.deleteById(id);}
}
java_442">EmergencyAlertService.java
java">package com.example.smartelderlycare.service;import com.example.smartelderlycare.model.EmergencyAlert;
import com.example.smartelderlycare.repository.EmergencyAlertRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class EmergencyAlertService {@Autowiredprivate EmergencyAlertRepository emergencyAlertRepository;public List<EmergencyAlert> getAllEmergencyAlerts() {return emergencyAlertRepository.findAll();}public EmergencyAlert getEmergencyAlertById(Long id) {return emergencyAlertRepository.findById(id).orElse(null);}public EmergencyAlert saveEmergencyAlert(EmergencyAlert emergencyAlert) {return emergencyAlertRepository.save(emergencyAlert);}public void deleteEmergencyAlert(Long id) {emergencyAlertRepository.deleteById(id);}
}
java_477">ServiceAppointmentService.java
java">package com.example.smartelderlycare.service;import com.example.smartelderlycare.model.ServiceAppointment;
import com.example.smartelderlycare.repository.ServiceAppointmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class ServiceAppointmentService {@Autowiredprivate ServiceAppointmentRepository serviceAppointmentRepository;public List<ServiceAppointment> getAllServiceAppointments() {return serviceAppointmentRepository.findAll();}public ServiceAppointment getServiceAppointmentById(Long id) {return serviceAppointmentRepository.findById(id).orElse(null);}public ServiceAppointment saveServiceAppointment(ServiceAppointment serviceAppointment) {return serviceAppointmentRepository.save(serviceAppointment);}public void deleteServiceAppointment(Long id) {serviceAppointmentRepository.deleteById(id);}
}

6. 控制器层 (controller 包)

java_514">ElderlyController.java
java">package com.example.smartelderlycare.controller;import com.example.smartelderlycare.model.Elderly;
import com.example.smartelderlycare.service.ElderlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/elderly")
public class ElderlyController {@Autowiredprivate ElderlyService elderlyService;@GetMappingpublic List<Elderly> getAllElderly() {return elderlyService.getAllElderly();}@GetMapping("/{id}")public Elderly getElderlyById(@PathVariable Long id) {return elderlyService.getElderlyById(id);}@PostMappingpublic Elderly createElderly(@RequestBody Elderly elderly) {return elderlyService.saveElderly(elderly);}@PutMapping("/{id}")public Elderly updateElderly(@PathVariable Long id, @RequestBody Elderly elderly) {elderly.setId(id);return elderlyService.saveElderly(elderly);}@DeleteMapping("/{id}")public void deleteElderly(@PathVariable Long id) {elderlyService.deleteElderly(id);}
}
java_560">HealthRecordController.java
java">package com.example.smartelderlycare.controller;import com.example.smartelderlycare.model.HealthRecord;
import com.example.smartelderlycare.service.HealthRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/health-records")
public class HealthRecordController {@Autowiredprivate HealthRecordService healthRecordService;@GetMappingpublic List<HealthRecord> getAllHealthRecords() {return healthRecordService.getAllHealthRecords();}@GetMapping("/{id}")public HealthRecord getHealthRecordById(@PathVariable Long id) {return healthRecordService.getHealthRecordById(id);}@PostMappingpublic HealthRecord createHealthRecord(@RequestBody HealthRecord healthRecord) {return healthRecordService.saveHealthRecord(healthRecord);}@PutMapping("/{id}")public HealthRecord updateHealthRecord(@PathVariable Long id, @RequestBody HealthRecord healthRecord) {healthRecord.setId(id);return healthRecordService.saveHealthRecord(healthRecord);}@DeleteMapping("/{id}")public void deleteHealthRecord(@PathVariable Long id) {healthRecordService.deleteHealthRecord(id);}
}
java_606">EmergencyAlertController.java
java">package com.example.smartelderlycare.controller;import com.example.smartelderlycare.model.EmergencyAlert;
import com.example.smartelderlycare.service.EmergencyAlertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/emergency-alerts")
public class EmergencyAlertController {@Autowiredprivate EmergencyAlertService emergencyAlertService;@GetMappingpublic List<EmergencyAlert> getAllEmergencyAlerts() {return emergencyAlertService.getAllEmergencyAlerts();}@GetMapping("/{id}")public EmergencyAlert getEmergencyAlertById(@PathVariable Long id) {return emergencyAlertService.getEmergencyAlertById(id);}@PostMappingpublic EmergencyAlert createEmergencyAlert(@RequestBody EmergencyAlert emergencyAlert) {return emergencyAlertService.saveEmergencyAlert(emergencyAlert);}@PutMapping("/{id}")public EmergencyAlert updateEmergencyAlert(@PathVariable Long id, @RequestBody EmergencyAlert emergencyAlert) {emergencyAlert.setId(id);return emergencyAlertService.saveEmergencyAlert(emergencyAlert);}@DeleteMapping("/{id}")public void deleteEmergencyAlert(@PathVariable Long id) {emergencyAlertService.deleteEmergencyAlert(id);}
}
java_652">ServiceAppointmentController.java
java">package com.example.smartelderlycare.controller;import com.example.smartelderlycare.model.ServiceAppointment;
import com.example.smartelderlycare.service.ServiceAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/service-appointments")
public class ServiceAppointmentController {@Autowiredprivate ServiceAppointmentService serviceAppointmentService;@GetMappingpublic List<ServiceAppointment> getAllServiceAppointments() {return serviceAppointmentService.getAllServiceAppointments();}@GetMapping("/{id}")public ServiceAppointment getServiceAppointmentById(@PathVariable Long id) {return serviceAppointmentService.getServiceAppointmentById(id);}@PostMappingpublic ServiceAppointment createServiceAppointment(@RequestBody ServiceAppointment serviceAppointment) {return serviceAppointmentService.saveServiceAppointment(serviceAppointment);}@PutMapping("/{id}")public ServiceAppointment updateServiceAppointment(@PathVariable Long id, @RequestBody ServiceAppointment serviceAppointment) {serviceAppointment.setId(id);return serviceAppointmentService.saveServiceAppointment(serviceAppointment);}@DeleteMapping("/{id}")public void deleteServiceAppointment(@PathVariable Long id) {serviceAppointmentService.deleteServiceAppointment(id);}
}

java_698">7. 主应用类 (SmartElderlyCareApplication.java)

java">package com.example.smartelderlycare;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SmartElderlyCareApplication {public static void main(String[] args) {SpringApplication.run(SmartElderlyCareApplication.class, args);}
}

8. 配置文件 (application.properties)

spring.datasource.url=jdbc:h2:mem:elderlycare
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true

9. 运行项目

  1. 使用 mvn spring-boot:run 命令运行项目。
  2. 访问 http://localhost:8080/h2-console 查看H2数据库。
  3. 使用API工具(如Postman)测试各个接口。

10. 扩展功能

  • 添加用户登录和权限管理(使用Spring Security)。
  • 添加健康数据分析功能,根据健康记录生成报告。

http://www.ppmy.cn/news/1562829.html

相关文章

Linux中通过frp实现内网穿透

1、准备工作 准备一台公网服务器&#xff08;云服务器&#xff09;&#xff0c;推荐阿里云或者腾讯云都可以 需要下载好frp安装包Linux端的和Windows端的安装包 网址&#xff1a;Releases fatedier/frp (github.com)https://github.com/fatedier/frp/releases 2、下载frp_0…

【Linux】Linux命令

目录 ​编辑 系统维护命令 man man&#xff1a;查看 man 手册 sudo passwd 用户名&#xff1a;修改用户密码 su&#xff1a;切换用户 echo ”输出内容“&#xff1a;向终端输出内容&#xff0c;默认换行 date查看当前系统的日期 clear&#xff1a;清屏 df -Th /df -h&…

[SAP ABAP] 使用LOOP AT...ASSIGNING FIELD-SYMBOL 直接更新内表数据

使用 LOOP AT...ASSIGNING FIELD-SYMBOL... 可以直接修改内表中的数据&#xff0c;而不需要先将内表数据复制到相应的工作区&#xff0c;然后再更新回内表中&#xff0c;从而提高性能 针对上述代码进行优化&#xff0c;我们使用LOOP AT...ASSIGNING FIELD-SYMBOL 直接更新内表数…

国产游戏崛起,燕云十六移动端1.9上线,ToDesk云电脑先开玩

游戏爱好者的利好消息出新了&#xff01;网易大型武侠仙游《燕云十六声》正式官宣&#xff0c;移动端要在1月9日正式上线了&#xff01;你期待手游版的燕云吗&#xff1f;不妨评论区留言说说你的看法。小编分别花了几个小时在台式机电脑和手机上都试了下&#xff0c;欣赏画面还…

C# 与 Windows API 交互的“秘密武器”:结构体和联合体

一、引言 在 C# 的编程世界里&#xff0c;当我们想要深入挖掘 Windows 系统的底层功能&#xff0c;与 Windows API 打交道时&#xff0c;结构体和联合体就像是两把神奇的钥匙&#x1f511; 它们能够帮助我们精准地操控数据&#xff0c;实现一些高级且强大的功能。就好比搭建一…

人工智能-数据分析及特征提取思路

1、概况 基于学生行为数据预测是否涉黄、涉黑等。 2.数据分析 数据分析的意义包括得到数据得直觉、发掘潜在的结构、提取重要的变量、删除异常值、检验潜在的假设和建立初步的模型。 2.1数据质量分析 2.1.1数据值分析 查看数据类型&#xff1a; 首先明确各字段的数据类型…

Jenkins内修改allure报告名称

背景&#xff1a; 最近使用Jenkins搭建自动化测试环境时&#xff0c;使用Jenkins的allure插件生成的报告&#xff0c;一直显示默认ALLURE REPORT&#xff0c;想自定义成与项目关联的名称&#xff0c;如图所示&#xff0c;很明显自定义名称显得高大上些&#xff0c;之前…

【MySQL】第三章 库的操作

系列文章目录 《【MySQL】第一章 MySQL 5.7的安装与卸载》 《【MySQL】第二章 初识数据库》 《【MySQL】第三章 库的操作》 《【MySQL】第四章 表的操作》 《【MySQL】第五章 数据类型》 文章目录 系列文章目录库的增删查改创建数据库删除数据库查看数据库查看数据库查看数据库…