学习进度条
今日所花时间:一小时
今日代码量:100行
博客量:一篇
了解到的知识点: 继续学习androidStudio 完成学习记录APP的开发
controller层代码
DailySummaryController
package com.example.searchdemo.controller;
import com.example.searchdemo.pojo.DailySummary;
import com.example.searchdemo.mapper.DailySummaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@CrossOrigin
@RestController
@RequestMapping("/dailySummary")
public class DailySummaryController {
@Autowired
private DailySummaryMapper dailySummaryMapper;
// 每日总结
@PostMapping("/addSummary")
public String addDailySummary(@RequestBody DailySummary dailySummary) {
// 验证 user_id 是否为 null
if (dailySummary.getUserId() == null) {
return "user_id 不能为空";
}
int insertedRows = dailySummaryMapper.insertDailySummary(dailySummary);
if (insertedRows > 0) {
return "每日总结添加成功";
} else {
return "每日总结添加失败";
}
}
}
WeekGoalController
package com.example.searchdemo.controller;
import com.example.searchdemo.pojo.WeeklyGoal;
import com.example.searchdemo.mapper.WeeklyGoalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/weeklyGoal")
public class WeeklyGoalController {
@Autowired
private WeeklyGoalMapper weeklyGoalMapper;
// 设定每周学习目标
@PostMapping("/setGoal")
public String setWeeklyGoal(@RequestBody WeeklyGoal weeklyGoal) {
// 简单验证user_id是否为空,你可以根据需求进行更复杂的验证
if (weeklyGoal.getUserId() == null) {
return "user_id 不能为空";
}
int insertedRows = weeklyGoalMapper.insertWeeklyGoal(weeklyGoal);
if (insertedRows > 0) {
return "每周学习目标设置成功";
} else {
return "每周学习目标设置失败";
}
}
}
Mapper层
DaliySummaryMapper
package com.example.searchdemo.mapper;
import com.example.searchdemo.pojo.DailySummary;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface DailySummaryMapper {
// 插入每日总结
@Insert("insert into `daily_summary` (`user_id`, `summary_date`, `blog_url`, `create_time`) " +
"values (#{userId}, #{summaryDate}, #{blogUrl}, now())")
@Options(useGeneratedKeys = true, keyProperty = "id")
int insertDailySummary(DailySummary dailySummary);
}
WeeklyGoalMapper
package com.example.searchdemo.mapper;
import com.example.searchdemo.pojo.WeeklyGoal;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface WeeklyGoalMapper {
// 插入每周学习目标
@Insert("insert into `weekly_goal` (`user_id`, `goal_content`, `week_start_date`, `create_time`) " +
"values (#{userId}, #{goalContent}, #{weekStartDate}, now())")
@Options(useGeneratedKeys = true, keyProperty = "id")
int insertWeeklyGoal(WeeklyGoal weeklyGoal);
}
至此基本完成了代码的书写,但是运行之后发现存在一定的问题,接下来要阶段性的解决代码问题,实现代码优化。

浙公网安备 33010602011771号