【Spring Boot】四种核心类的依赖关系:实体类、数据处理类、业务处理类、控制器类

//1.配置项目环境,创建Spring Boot项目。

//2.数据库设置,配置数据库。

//3.创建实体类,映射到数据库。

//4.创建数据处理层类,Repository

//5.创建业务处理类,Service类

//6.创建控制器类,Controller类

Article.java

java

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

@Entity

public class Article{

@Id

@GeneratedValue(strategy = GenerationType.AOTU)

private Long id;

private String title;

private String content;

private String author;

//省略构造函数、getter和setter方法

//省略toString方法

//一个实体类完成了!

}

ArticleRepository.java(数据访问层类)

'''java

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface ArticleRepository extends JpaRepository<Article,Long>{

//可以在此自定义查询方法

//根据作者名查询文章

List<Article> findByAuthor(String author);

//根据标题和作者查询文章

List<Article> findByTitleAndAuthor(String title,String author);

//使用JPQL查询语句查询文章

@Query("Select a FROM Article a WHERE a.title LIKE %:keyword% OR a.content LIKE %:keyword%")

List<Article> fintByKeyword(String keyword);

}

ArticleService.Java(业务逻辑层):

'''java

import org.springframework.beans.factory. annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

import java.util.Optional;

@Service

public class ArticleService{

private final ArticleRepository articleRepository;

@Autowired

public ArticleService(ArticleRepository articleRepository){

this.articleRepository = articleRepository;

}

public List<Article>getAllArticles(){

return articleRepository.findAll();

}

public Optional<Article>getArticleById(Long id){

return articleRepository.findByID(id);

}

public Article createArticle(Article article){

return articleRepository.save(article);

}

/**

public void updateArticle(Long id, Article article) {

Optional<Article> existingArticle = articleRepository.findById(id);

if (existingArticle.isPresent()) {

Article updatedArticle = existingArticle.get();

updatedArticle.setTitle(article.getTitle());

updatedArticle.setContent(article.getContent());

updatedArticle.setAuthor(article.getAuthor());

articleRepository.save(updatedArticle);

}

}

**/

public void deleteArticle(Long id){

articleRepository.deleteById(id);

}

}
ArticleController.java(控制器类):

'''java

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.Optional;

@RestContrller

@RequestMapping("/articles")

public class ArticleController{

private final ArticleService articleService;

@Autowired

public ArticleControler(

ArticleService articleService

){

tihs.articleService = articleService;

}

@GetMapping

public ResponseEntity<List<Article>>getAllArticles(){

List<Article>articles = articleService.getAllArticles;

return new ResponseEntity<>(articles,HttpStatus.OK)

}

//以下为粘贴

@GetMapping("/{id}")

public ResponseEntity<Article> getArticleById(@PathVariable Long id) {

Optional<Article> article = articleService.getArticleById(id);

return article.map(value -> new ResponseEntity<>(value, HttpStatus.OK))

.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));

}

@PostMapping

public ResponseEntity<Article> createArticle(@RequestBody Article article) {

Article createdArticle = articleService.createArticle(article);

return new ResponseEntity<>(createdArticle, HttpStatus.CREATED);

}

@PutMapping("/{id}")

public ResponseEntity<Void> updateArticle(@PathVariable Long id, @RequestBody Article article) {

articleService.updateArticle(id, article);

return new ResponseEntity<>(HttpStatus.NO_CONTENT);

}

@DeleteMapping("/{id}")

public ResponseEntity<Void> deleteArticle(@PathVariable Long id) {

articleService.deleteArticle(id);

return new ResponseEntity<>(HttpStatus.NO_CONTENT);

}

}

}

相关推荐
大模型玩家七七11 分钟前
安全对齐不是消灭风险,而是重新分配风险
android·java·数据库·人工智能·深度学习·安全
李少兄11 分钟前
MySQL 中为时间字段设置默认当前时间
android·数据库·mysql
码海踏浪21 分钟前
从简单到专业在OceanBase中查看SQL是否走索引
数据库·sql·oceanbase
qinyia22 分钟前
**使用AI助手在智慧运维中快速定位并修复服务异常:以Nginx配置错误导致502错误为例**
linux·运维·服务器·数据库·mysql·nginx·自动化
熊文豪28 分钟前
关系数据库替换用金仓——Oracle兼容性深度解析
数据库·oracle·金仓数据库·电科金仓·kes
eWidget34 分钟前
面向Oracle生态的国产高兼容数据库解决方案
数据库·oracle·kingbase·数据库平替用金仓·金仓数据库
A懿轩A36 分钟前
【MySQL 数据库】MySQL 数据库核心概念详解:库、表、字段、主键与关系型模型一文读懂
数据库·mysql·oracle
盒马coding41 分钟前
postgreSQL中调整Checkpoint的重要性
数据库·mysql·postgresql
怣501 小时前
MySQL多表连接完全指南:内连接与外连接(零基础入门版)
数据库·mysql
爱吃山竹的大肚肚1 小时前
文件上传大小超过服务器限制
java·数据库·spring boot·mysql·spring