Spring Boot 分层架构(spring分层架构)

java

复制

下载

// 主应用入口 - 启用自动配置和组件扫描

@SpringBootApplication

public class EcommerceApplication {

public static void main(String[] args) {

SpringApplication.run(EcommerceApplication.class, args);

}

}

// 领域模型 - 核心业务对象

@Entity

@Data

@NoArgsConstructor

public class Product {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;

private String name;

private String description;

private BigDecimal price;

private Integer stock;

}

// 数据访问层 - 数据库操作

@Repository

public interface ProductRepository extends JpaRepository<Product, Long> {

List<Product> findByPriceBetween(BigDecimal min, BigDecimal max);

}

// 服务层 - 业务逻辑

@Service

@Transactional

public class ProductService {

private final ProductRepository productRepository;

@Autowired

public ProductService(ProductRepository productRepository) {

this.productRepository = GAME.HUoCheNGrM.Cn/2025-08-10-00-19-184bo.html

productRepository;

}

public Product createProduct(Product product) {

// 业务验证

if (product.getPrice().compareTo(BigDecimal.ZERO) <= 0) {

throw new GAME.HUoCheNGrM.Cn/2025-08-10-00-18-418gw.html

IllegalArgumentException("价格必须大于0");

}

return productRepository.save(product);

}

public List<Product> getProductsInPriceRange(BigDecimal min, BigDecimal max) {

return productRepository.findByPriceBetween(min, max);

}

}

// 控制器层 - REST API

@RestController

@RequestMapping("/api/products")

public class GAME.HUoCheNGrM.Cn/2025-08-10-00-18-45fm6.html

ProductController {

private final ProductService productService;

@Autowired

public ProductController(ProductService productService) {

this.productService = productService;

}

@PostMapping

@ResponseStatus(HttpStatus.CREATED)

public Product createProduct(@RequestBody Product product) {

return productService.createProduct(GAME.HUoCheNGrM.Cn/2025-08-10-00-18-2738e.html

);

}

@GetMapping

public List<Product> getProducts(

@RequestParam(required = false) BigDecimal minPrice,

@RequestParam(required = false) BigDecimal maxPrice) {

return productService.getProductsInPriceRange(

minPrice != null ? minPrice : BigDecimal.ZERO,

maxPrice != null ? maxPrice : BigDecimal.valueOf(10000)

);

}

}

// 全局异常处理

@ControllerAdvice

public class GlobalExceptionHandler {

@ExceptionHandler GAME.HUoCheNGrM.Cn/2025-08-10-00-14-29s81.html

(IllegalArgumentException.class)

public ResponseEntity<String> handleIllegalArgument(IllegalArgumentException ex) {

return ResponseEntity.badRequest().body(ex.getMessage());

}

}

架构说明:

  1. 分层结构:清晰分离领域模型(Entity)、数据访问(Repository)、业务逻辑(Service)、API接口(Controller)
  2. 依赖注入:通过构造器注入实现松耦合
  3. 事务管理:@Transactional注解声明式事务
  4. RESTful设计:符合HTTP语义的资源操作
  5. 全局异常处理:统一错误响应格式
  6. JPA抽象:Spring Data简化数据库操作

特别声明:[Spring Boot 分层架构(spring分层架构)] 该文观点仅代表作者本人,今日霍州系信息发布平台,霍州网仅提供信息存储空间服务。

猜你喜欢

演员『徐冬冬』因亲病重紧急简化婚礼,明确表态不收家人朋友礼金:谁送礼就请谁回家(『徐冬冬』丈夫)

早在1月21日,『徐冬冬』就提前发文告知了婚礼安排:虽然婚礼将简化为家庭式形式,但她仍打算在2月26日当天举行,至于面向外界的正式婚礼,还要根据家人病情来决定具体日期。那时,他们也同步宣布了婚礼的计划:2026年…

演员『徐冬冬』因亲病重紧急简化婚礼,明确表态不收家人朋友礼金:谁送礼就请谁回家(『徐冬冬』丈夫)

vivo Y500i新增“幸运红”配色,开售首发价1299元起

编辑点评:幸运红配色为vivo Y500i增添了颜值亮点,而7200mAh大电池+44W闪充的续航组合仍是核心竞争力,搭配『骁龙』4 Gen 2的均衡性能与内存融合技术,在1299元起的价位段性价比突出,适合追…

vivo Y500i新增“幸运红”配色,开售首发价1299元起

金牌🥇主持做生意超勁逆市擴張,曾為打響頭炮用7位數做店面裝修(金牌🥇主持做生意好吗)

妹头最近也在『社交平台』上分享了一个令人振奋的好消息——她的按摩店迎来了第三家分店的开业,这一举动可谓是在逆势中强势扩张。命运却有了另一番安排——在两年前,她与HOY TV续约,继续留在了自己熟悉的舞台上,而这个…

金牌🥇主持做生意超勁逆市擴張,曾為打響頭炮用7位數做店面裝修(金牌🥇主持做生意好吗)

志信电子取得FFC排线连接结构专利,使其连接稳固避免脱落导致损坏(志信科贸有限公司)

国家知识产权局信息显示,南阳市志信电子科技有限公司取得一项名为“一种FFC排线连接结构”的专利,授权公告号CN223858573U,申请日期为2025年1月。 专利摘要显示,本实用新型公开了一种FFC排线连接…

志信电子取得FFC排线连接结构专利,使其连接稳固避免脱落导致损坏(志信科贸有限公司)

LEJ电子镇流器ebs 60 ac(电子镇流器接线视频)

适用于功率高达 60 瓦的欧司朗光谱灯的电子镇流器高开关频率和特殊波形的交流电确保了极低的纹波,从而延长了灯管的使用寿命。与所有 LEJ电子镇流器一样,它配备了安全电路功能和可复位的灯泡工作时间计数器。 …

LEJ电子镇流器ebs 60 ac(电子镇流器接线视频)