Spring Boot 是一个基于 Spring 框架的开源 Java 框架,旨在简化新 Spring 应用的初始搭建及开发过程。它通过提供预配置的模板和简化的配置方式,让开发者可以更快地启动和运行应用。本文将介绍 Spring Boot 应用中的各个层次及其功能,并附上一些代码示例。
1. 控制器层(Controller Layer)
控制器层负责处理 HTTP 请求,解析输入,调用业务逻辑,并返回视图或数据。控制器通常使用 @RestController
注解来标记。
@Controller
@RequestMapping("/score")
public class score {
@Resource(name = "ScoreService")
private ScoreService scoreService;
@RequestMapping("/personalList")
public String scoreList(Model model,@RequestParam(value = "stuNumber",defaultValue = "") String stuNumber, HttpSession session){
stuNumber = (String) session.getAttribute("stuNumber");
List<Personal> list=scoreService.queryByNum(stuNumber);
model.addAttribute("personList",scoreService.queryByNum(stuNumber));
model.addAttribute("stuNumber",stuNumber);
model.addAttribute("avgPoint",scoreService.countPoint());
return "score/personalList";
}
@RequestMapping("/personalScore")
public String selfScore(Model model, HttpSession session){
String stuNumber = (String) session.getAttribute("stuNumber");
model.addAttribute("personList",scoreService.queryByNum(stuNumber));
model.addAttribute("stuNumber",stuNumber);
model.addAttribute("avgPoint",scoreService.countPoint());
return "score/personalList";
}
}
2. 服务层(Service Layer)
服务层包含业务逻辑。这一层的主要职责是处理业务规则和应用逻辑,通常使用 @Service
注解来标记。
@Service("ScoreService")
public class ScoreService {
@Resource(name = "scoreMapper")
private scoreMapper scoreMapper;
private List<Personal> personalList=null;
public List<Subject> queryByName(String couName){
return scoreMapper.queryByName(couName);
}
// 统计不同分数段的学生人数
public Map<String, Integer> countStudentsByScoreRange(String couName) {
List<Subject> subjects = queryByName(couName);
Map<String, Integer> scoreRangeCount = new HashMap<>();
// 初始化各个分数段的人数
scoreRangeCount.put("0-59", 0);
scoreRangeCount.put("60-69", 0);
scoreRangeCount.put("70-79", 0);
scoreRangeCount.put("80-89", 0);
scoreRangeCount.put("90-100", 0);
for (Subject subject : subjects) {
int score = Integer.parseInt(subject.getGrade());
if (score < 60) {
scoreRangeCount.put("0-59", scoreRangeCount.get("0-59") + 1);
} else if (score < 70) {
scoreRangeCount.put("60-69", scoreRangeCount.get("60-69") + 1);
} else if (score < 80) {
scoreRangeCount.put("70-79", scoreRangeCount.get("70-79") + 1);
} else if (score < 90) {
scoreRangeCount.put("80-89", scoreRangeCount.get("80-89") + 1);
} else {
scoreRangeCount.put("90-100", scoreRangeCount.get("90-100") + 1);
}
}
return scoreRangeCount;
}
public List<Personal> queryByNum(String stuNumber){
personalList=scoreMapper.queryByNum(stuNumber);
return personalList;
}
public List<AllPoint> queryByMajor(String major){
return scoreMapper.queryByMajor(major);
}
public float countPoint(){
float sum=0;
for (Personal person:personalList) {
sum+=person.getPoint();
}
return sum/personalList.size();
}
}
3. 数据访问层(Repository Layer)
数据访问层负责与数据库的交互。Spring Data JPA 提供了简化的数据库操作,通过接口继承 ,无需编写大量样板代码。
@Mapper
@Repository("scoreMapper")
public interface scoreMapper {
List<Subject> queryByName(String couName);
List<Personal> queryByNum(String stuNumber);
List<AllPoint> queryByMajor(String major);
}
应用启动类
Spring Boot 应用的入口类通常使用 @SpringBootApplication
注解标记,并包含 main
方法来启动应用。
@SpringBootApplication
public class SpringwebApplication {
public static void main(String[] args) {
//SpringApplication.run(SpringwebApplication.class, args);
ApplicationContext context = SpringApplication.run(SpringwebApplication.class, args);
String serverPort = context.getEnvironment().getProperty("server.port");
System.out.print("Verio started at http://localhost:" + serverPort);
}
}