Spring Boot: 简化开发的强大框架

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);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值