基于Spring Boot在线学习系统代码
时间: 2024-04-16 09:24:26 浏览: 14
以下是一个简单的基于Spring Boot的在线学习系统的示例代码:
```java
// 学习资源实体类
@Entity
public class LearningResource {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String description;
// 省略其他属性和方法
}
// 学习资源存储库
@Repository
public interface LearningResourceRepository extends JpaRepository<LearningResource, Long> {
// 自定义查询方法
List<LearningResource> findByTitleContaining(String keyword);
}
// 学习资源服务类
@Service
public class LearningResourceService {
private final LearningResourceRepository learningResourceRepository;
public LearningResourceService(LearningResourceRepository learningResourceRepository) {
this.learningResourceRepository = learningResourceRepository;
}
public LearningResource getLearningResourceById(Long id) {
return learningResourceRepository.findById(id).orElse(null);
}
public List<LearningResource> searchLearningResources(String keyword) {
return learningResourceRepository.findByTitleContaining(keyword);
}
public void saveLearningResource(LearningResource learningResource) {
learningResourceRepository.save(learningResource);
}
public void deleteLearningResource(Long id) {
learningResourceRepository.deleteById(id);
}
}
// 学习资源控制器
@RestController
@RequestMapping("/learning-resources")
public class LearningResourceController {
private final LearningResourceService learningResourceService;
public LearningResourceController(LearningResourceService learningResourceService) {
this.learningResourceService = learningResourceService;
}
@GetMapping("/{id}")
public ResponseEntity<LearningResource> getLearningResourceById(@PathVariable Long id) {
LearningResource learningResource = learningResourceService.getLearningResourceById(id);
return ResponseEntity.ok().body(learningResource);
}
@GetMapping("/search")
public ResponseEntity<List<LearningResource>> searchLearningResources(@RequestParam String keyword) {
List<LearningResource> learningResources = learningResourceService.searchLearningResources(keyword);
return ResponseEntity.ok().body(learningResources);
}
@PostMapping
public ResponseEntity<?> saveLearningResource(@RequestBody LearningResource learningResource) {
learningResourceService.saveLearningResource(learningResource);
return ResponseEntity.ok().build();
}
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteLearningResource(@PathVariable Long id) {
learningResourceService.deleteLearningResource(id);
return ResponseEntity.ok().build();
}
}
// Spring Boot应用程序入口
@SpringBootApplication
public class OnlineLearningSystemApplication {
public static void main(String[] args) {
SpringApplication.run(OnlineLearningSystemApplication.class, args);
}
}
```
以上代码是一个简单的基于Spring Boot的在线学习系统的示例,包括学习资源实体类、存储库、服务类和控制器等组件。你可以根据实际需求进行修改和扩展。