使用Springboot +JPA实现艺术培训管理系统MySQL作为后台数据库该系统功能包括查询全部艺术培训学员信息、修改艺术培训学员信息2个功能具体要求如下
-
创建数据库表
- 创建名为
art_student的表,包含以下字段:- id:主键,自增
- name:学员姓名,字符串类型
- age:学员年龄,整数类型
- gender:学员性别,字符串类型
- phone:学员手机号,字符串类型
- email:学员邮箱,字符串类型
- address:学员地址,字符串类型
- 创建名为
-
配置数据库连接
- 在
application.properties文件中配置数据库连接信息,如下所示:spring.datasource.url=jdbc:mysql://localhost:3306/db_name spring.datasource.username=root spring.datasource.password=password
- 在
-
创建实体类
- 创建名为
ArtStudent的实体类,对应数据库表art_student - 使用JPA注解进行实体类与数据库表的映射,如下所示:
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class ArtStudent { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; private String gender; private String phone; private String email; private String address; // 省略getter和setter方法 }
- 创建名为
-
创建数据访问层接口
- 创建名为
ArtStudentRepository的接口,继承自JpaRepository接口,用于对数据库表进行CRUD操作,如下所示:import org.springframework.data.jpa.repository.JpaRepository; public interface ArtStudentRepository extends JpaRepository<ArtStudent, Long> { }
- 创建名为
-
创建服务层接口和实现类
-
创建名为
ArtStudentService的接口,定义查询全部学员信息和修改学员信息的方法,如下所示:import java.util.List; public interface ArtStudentService { List<ArtStudent> getAllArtStudents(); ArtStudent updateArtStudent(Long id, ArtStudent artStudent); } -
创建名为
ArtStudentServiceImpl的实现类,实现ArtStudentService接口,并注入ArtStudentRepository作为数据访问层的实例,实现具体的方法逻辑,如下所示:import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class ArtStudentServiceImpl implements ArtStudentService { private final ArtStudentRepository artStudentRepository; @Autowired public ArtStudentServiceImpl(ArtStudentRepository artStudentRepository) { this.artStudentRepository = artStudentRepository; } @Override public List<ArtStudent> getAllArtStudents() { return artStudentRepository.findAll(); } @Override public ArtStudent updateArtStudent(Long id, ArtStudent artStudent) { ArtStudent existingArtStudent = artStudentRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("Invalid student id: " + id)); existingArtStudent.setName(artStudent.getName()); existingArtStudent.setAge(artStudent.getAge()); existingArtStudent.setGender(artStudent.getGender()); existingArtStudent.setPhone(artStudent.getPhone()); existingArtStudent.setEmail(artStudent.getEmail()); existingArtStudent.setAddress(artStudent.getAddress()); return artStudentRepository.save(existingArtStudent); } }
-
-
创建控制器类
- 创建名为
ArtStudentController的控制器类,用于接收前端请求并调用相应的服务方法,如下所示:import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/students") public class ArtStudentController { private final ArtStudentService artStudentService; @Autowired public ArtStudentController(ArtStudentService artStudentService) { this.artStudentService = artStudentService; } @GetMapping public List<ArtStudent> getAllArtStudents() { return artStudentService.getAllArtStudents(); } @PutMapping("/{id}") public ArtStudent updateArtStudent(@PathVariable Long id, @RequestBody ArtStudent artStudent) { return artStudentService.updateArtStudent(id, artStudent); } }
- 创建名为
-
启动应用程序
- 在
Application类中添加@SpringBootApplication注解,并运行main方法,启动Spring Boot应用程序。
- 在
-
测试接口
- 使用Postman等工具发送GET请求到
localhost:8080/students,可以查询全部艺术培训学员信息。 - 使用Postman等工具发送PUT请求到
localhost:8080/students/{id},将指定id的艺术培训学员信息进行修改
- 使用Postman等工具发送GET请求到
原文地址: http://www.cveoy.top/t/topic/hJjB 著作权归作者所有。请勿转载和采集!