用Java开发简易健康记录小程序,核心是实现健康数据的增删改查功能,同时保证数据存储的稳定性和操作的便捷性。整体可以采用Spring Boot作为后端框架,MySQL作为数据库,前端通过简单的接口调用实现交互。

开发环境准备
首先需要准备以下开发环境:
- JDK 1.8及以上版本
- IntelliJ IDEA或Eclipse开发工具
- MySQL 5.7及以上版本数据库
- Maven项目管理工具
数据库设计
健康记录小程序需要存储用户信息和健康数据,核心表设计如下:
| 表名 | 字段 | 说明 |
|---|---|---|
| user | id, username, password, create_time | 存储用户账号信息 |
| health_record | id, user_id, height, weight, blood_pressure, record_time, remark | 存储用户健康记录数据 |
核心功能实现
1. 实体类定义
首先定义对应的实体类,映射数据库表结构:
// 用户实体类
public class User {
private Integer id;
private String username;
private String password;
private Date createTime;
// 省略getter和setter方法
}
// 健康记录实体类
public class HealthRecord {
private Integer id;
private Integer userId;
private Double height;
private Double weight;
private String bloodPressure;
private Date recordTime;
private String remark;
// 省略getter和setter方法
}
2. 数据访问层实现
使用MyBatis实现数据访问接口,这里以健康记录的插入和查询为例:
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface HealthRecordMapper {
// 插入健康记录
@Insert("INSERT INTO health_record(user_id, height, weight, blood_pressure, record_time, remark) " +
"VALUES(#{userId}, #{height}, #{weight}, #{bloodPressure}, #{recordTime}, #{remark})")
int insert(HealthRecord healthRecord);
// 根据用户ID查询健康记录
@Select("SELECT * FROM health_record WHERE user_id = #{userId} ORDER BY record_time DESC")
List<HealthRecord> selectByUserId(Integer userId);
// 根据记录ID删除健康记录
@Delete("DELETE FROM health_record WHERE id = #{id}")
int deleteById(Integer id);
// 更新健康记录
@Update("UPDATE health_record SET height=#{height}, weight=#{weight}, blood_pressure=#{bloodPressure}, " +
"remark=#{remark} WHERE id=#{id}")
int update(HealthRecord healthRecord);
}
3. 服务层实现
服务层封装业务逻辑,处理数据校验等操作:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class HealthRecordService {
@Autowired
private HealthRecordMapper healthRecordMapper;
// 新增健康记录
public int addRecord(HealthRecord healthRecord) {
// 简单校验数据合法性
if (healthRecord.getUserId() == null || healthRecord.getHeight() == null) {
return 0;
}
return healthRecordMapper.insert(healthRecord);
}
// 查询用户健康记录
public List<HealthRecord> getRecordsByUserId(Integer userId) {
return healthRecordMapper.selectByUserId(userId);
}
// 删除健康记录
public int removeRecord(Integer id) {
return healthRecordMapper.deleteById(id);
}
// 更新健康记录
public int modifyRecord(HealthRecord healthRecord) {
return healthRecordMapper.update(healthRecord);
}
}
4. 控制层接口实现
控制层提供对外访问的接口,处理前端请求:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/health")
public class HealthRecordController {
@Autowired
private HealthRecordService healthRecordService;
// 新增记录接口
@PostMapping("/add")
public Map<String, Object> addRecord(@RequestBody HealthRecord healthRecord) {
Map<String, Object> result = new HashMap<>();
int rows = healthRecordService.addRecord(healthRecord);
if (rows > 0) {
result.put("code", 200);
result.put("msg", "添加成功");
} else {
result.put("code", 500);
result.put("msg", "添加失败,参数不完整");
}
return result;
}
// 查询记录接口
@GetMapping("/list")
public Map<String, Object> listRecords(@RequestParam Integer userId) {
Map<String, Object> result = new HashMap<>();
List<HealthRecord> records = healthRecordService.getRecordsByUserId(userId);
result.put("code", 200);
result.put("data", records);
return result;
}
// 删除记录接口
@DeleteMapping("/delete")
public Map<String, Object> deleteRecord(@RequestParam Integer id) {
Map<String, Object> result = new HashMap<>();
int rows = healthRecordService.removeRecord(id);
if (rows > 0) {
result.put("code", 200);
result.put("msg", "删除成功");
} else {
result.put("code", 500);
result.put("msg", "删除失败,记录不存在");
}
return result;
}
}
功能测试
开发完成后可以通过PostMan等工具测试接口,比如调用新增记录接口时,传入如下参数:
{
"userId": 1,
"height": 175.5,
"weight": 68.2,
"bloodPressure": "120/80",
"recordTime": "2024-05-20 10:30:00",
"remark": "体检数据"
}
如果返回code为200,说明记录添加成功,之后可以调用查询接口验证数据是否正确存储。
总结
以上就是一个简易Java健康记录小程序的完整开发流程,核心围绕Spring Boot框架整合MyBatis实现数据操作,开发者可以根据需求扩展更多功能,比如添加用户登录校验、健康数据统计分析、数据导出等功能,让小程序更贴合实际使用场景。
Java健康记录小程序Spring_BootMySQLMyBatis修改时间:2026-07-13 22:06:28