以下是一个基于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
包)
Elderly.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 Setters
public 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;
}
}
HealthRecord.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 Setters
public 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;
}
}
EmergencyAlert.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 Setters
public 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;
}
}
ServiceAppointment.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 Setters
public 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
包)
ElderlyRepository.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> {
}
HealthRecordRepository.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> {
}
EmergencyAlertRepository.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> {
}
ServiceAppointmentRepository.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
包)
ElderlyService.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 {
@Autowired
private 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);
}
}
HealthRecordService.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 {
@Autowired
private 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);
}
}
EmergencyAlertService.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 {
@Autowired
private 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);
}
}
ServiceAppointmentService.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 {
@Autowired
private 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
包)
ElderlyController.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 {
@Autowired
private ElderlyService elderlyService;
@GetMapping
public List<Elderly> getAllElderly() {
return elderlyService.getAllElderly();
}
@GetMapping("/{id}")
public Elderly getElderlyById(@PathVariable Long id) {
return elderlyService.getElderlyById(id);
}
@PostMapping
public 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);
}
}
HealthRecordController.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 {
@Autowired
private HealthRecordService healthRecordService;
@GetMapping
public List<HealthRecord> getAllHealthRecords() {
return healthRecordService.getAllHealthRecords();
}
@GetMapping("/{id}")
public HealthRecord getHealthRecordById(@PathVariable Long id) {
return healthRecordService.getHealthRecordById(id);
}
@PostMapping
public 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);
}
}
EmergencyAlertController.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 {
@Autowired
private EmergencyAlertService emergencyAlertService;
@GetMapping
public List<EmergencyAlert> getAllEmergencyAlerts() {
return emergencyAlertService.getAllEmergencyAlerts();
}
@GetMapping("/{id}")
public EmergencyAlert getEmergencyAlertById(@PathVariable Long id) {
return emergencyAlertService.getEmergencyAlertById(id);
}
@PostMapping
public 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);
}
}
ServiceAppointmentController.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 {
@Autowired
private ServiceAppointmentService serviceAppointmentService;
@GetMapping
public List<ServiceAppointment> getAllServiceAppointments() {
return serviceAppointmentService.getAllServiceAppointments();
}
@GetMapping("/{id}")
public ServiceAppointment getServiceAppointmentById(@PathVariable Long id) {
return serviceAppointmentService.getServiceAppointmentById(id);
}
@PostMapping
public 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);
}
}
7. 主应用类 (SmartElderlyCareApplication.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. 运行项目
- 使用
mvn spring-boot:run
命令运行项目。 - 访问
http://localhost:8080/h2-console
查看H2数据库。 - 使用API工具(如Postman)测试各个接口。
10. 扩展功能
- 添加用户登录和权限管理(使用Spring Security)。
- 添加健康数据分析功能,根据健康记录生成报告。