【java】springboot前后端分离简单案例

以下是一个简单的示例,展示了如何使用Spring Boot实现前后端分离项目,包含代码讲解和详细注释。

 

1. 创建项目:

创建一个空的Spring Boot项目。

 

2. 设置依赖:

使用你喜欢的构建工具(例如Maven)将以下依赖添加到项目的pom.xml文件中:

```xml

    org.springframework.boot

    spring-boot-starter-web

 

    org.springframework.boot

    spring-boot-starter-data-jpa

 

    com.h2database

    h2

    runtime

```

这些依赖包含了Spring Boot、Spring MVC和Spring Data JPA相关的库。

 

3. 创建数据库表:

使用数据库管理工具创建一个名为"student"的表,包含id、name、age和score字段。

 

4. 创建实体类:

创建一个名为Student的实体类,添加相应的注解,使其能够与数据库表进行映射。

```java

@Entity

@Table(name = "student")

public class Student {

    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;

    

    private String name;

    

    private int age;

    

    private double score;

    

    // 省略getter和setter方法

}

```

 

5. 创建数据访问层(Repository):

创建一个名为StudentRepository的接口,继承自JpaRepository,用于对数据库表进行增删改查操作。

```java

public interface StudentRepository extends JpaRepository {

}

```

 

6. 创建业务逻辑层(Service):

创建一个名为StudentService的接口,定义一些学生信息相关的业务逻辑方法,如添加学生、删除学生、修改学生信息等。

```java

public interface StudentService {

    Student addStudent(Student student);

    void deleteStudent(Long id);

    Student updateStudent(Student student);

    List getAllStudents();

}

```

 

7. 实现业务逻辑层(Service):

创建一个名为StudentServiceImpl的类,实现StudentService接口,并注入StudentRepository,用于对数据库进行操作。

```java

@Service

public class StudentServiceImpl implements StudentService {

    

    private final StudentRepository studentRepository;

    

    public StudentServiceImpl(StudentRepository studentRepository) {

        this.studentRepository = studentRepository;

    }

    

    @Override

    public Student addStudent(Student student) {

        return studentRepository.save(student);

    }

    

    @Override

    public void deleteStudent(Long id) {

        studentRepository.deleteById(id);

    }

    

    @Override

    public Student updateStudent(Student student) {

        return studentRepository.save(student);

    }

    

    @Override

    public List getAllStudents() {

        return studentRepository.findAll();

    }

}

```

 

8. 创建控制器层(Controller):

创建一个名为StudentController的类,添加@RestController注解,并注入StudentService,用于处理前端的请求。

```java

@RestController

@RequestMapping("/api/students")

public class StudentController {

    

    private final StudentService studentService;

    

    public StudentController(StudentService studentService) {

        this.studentService = studentService;

    }

    

    @PostMapping

    public ResponseEntity addStudent(@RequestBody Student student) {

        Student newStudent = studentService.addStudent(student);

        return ResponseEntity.ok(newStudent);

    }

    

    @DeleteMapping("/{id}")

    public ResponseEntity deleteStudent(@PathVariable Long id) {

        studentService.deleteStudent(id);

        return ResponseEntity.noContent().build();

    }

    

    @PutMapping("/{id}")

    public ResponseEntity updateStudent(@PathVariable Long id, @RequestBody Student student) {

        student.setId(id);

        Student updatedStudent = studentService.updateStudent(student);

        return ResponseEntity.ok(updatedStudent);

    }

    

    @GetMapping

    public ResponseEntity> getAllStudents() {

        List students = studentService.getAllStudents();

        return ResponseEntity.ok(students);

    }

}

```

 

9. 前端实现:

在resources/static目录下创建一个index.html文件,编写HTML页面的结构和样式,并使用JavaScript发起与后端的接口交互。

 

```html

   

    Student Management System

   

Students

    

   

       

           

               

                   

                   

                   

                   

               

           

           

               

           

       

ID Name Age Score

   

    

   

       

Add Student

       

           

           

       

       

           

           

       

       

           

           

       

       

           

       

   

    

   

```

 

11. 启动项目:

运行项目,启动Spring Boot应用程序。

 

12. 前后端交互:

使用浏览器访问前端页面,触发相应的请求,后端会根据URL和请求方式调用相应的方法,完成相应的业务逻辑,并将结果返回给前端。

在本例中,访问http://localhost:8080,即可看到学生信息的表格。可以通过表单添加学生,添加成功后表格会刷新显示最新的学生信息。

你可能感兴趣的:(java,spring,boot,spring,后端)