欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > Spring Boot

Spring Boot

2025/6/9 0:16:18 来源:https://blog.csdn.net/m0_66572887/article/details/148450195  浏览:    关键词:Spring Boot

一、Spring Boot 简介

Spring Boot 是基于 Spring 框架之上的快速开发框架,旨在简化 Spring 应用的构建过程,让开发者更专注于业务逻辑,而不是繁琐的配置。

特点:

  • 自动配置(Auto Configuration)

  • 起步依赖(Starter Dependencies)

  • 内嵌式服务器(如 Tomcat)

  • 无需大量 XML 配置

  • 配置灵活(支持 application.properties / application.yml


二、环境准备

1. 开发工具

  • IDE:IntelliJ IDEA(推荐)

  • JDK:Java 8 或更高版本

  • 构建工具:Maven 或 Gradle

  • 数据库:MySQL(或 H2 作为内嵌数据库)

2. 创建项目

推荐使用 Spring 官方提供的 Spring Initializr 来快速生成项目结构。


三、项目结构说明

src└── main├── java│   └── com.example.demo│       ├── controller│       ├── service│       ├── repository│       └── DemoApplication.java└── resources├── application.yml├── static/└── templates/

四、核心注解介绍

注解说明
@SpringBootApplication核心注解,包含 @Configuration@EnableAutoConfiguration@ComponentScan
@RestController组合注解,包含 @Controller + @ResponseBody
@RequestMapping用于请求路径映射
@Autowired自动注入依赖
@Service / @Repository标识服务层 / 数据访问层 Bean

五、配置文件说明(application.yml)

server:port: 8080spring:datasource:url: jdbc:mysql://localhost:3306/demousername: rootpassword: rootthymeleaf:cache: falsejpa:hibernate:ddl-auto: updateshow-sql: true

六、核心功能实战

1. Web 接口开发

Controller 示例:

@RestController
@RequestMapping("/api")
public class HelloController {@GetMapping("/hello")public String hello() {return "Hello, Spring Boot!";}
}

2. 服务层(Service)

@Service
public class UserService {public String getUserInfo() {return "用户信息";}
}

3. 数据访问层(Repository)

使用 Spring Data JPA:

public interface UserRepository extends JpaRepository<User, Long> {}

七、常用模块

1. Spring Boot + MyBatis

  • 添加依赖:

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.3.1</version>
</dependency>
  • Mapper 接口 + XML 配置

2. Spring Boot + Thymeleaf

用于构建前端模板页面:

@Controller
public class PageController {@GetMapping("/home")public String home(Model model) {model.addAttribute("msg", "欢迎!");return "home";}
}

八、定时任务实现

@Component
public class ScheduledTasks {@Scheduled(cron = "0/10 * * * * ?")public void reportCurrentTime() {System.out.println("现在时间:" + new Date());}
}
  • 启动类中添加:@EnableScheduling


九、异常处理机制

@RestControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(Exception.class)public ResponseEntity<String> handle(Exception e) {return ResponseEntity.status(500).body("服务器异常:" + e.getMessage());}
}

十、打包部署

使用 Maven 打包:

mvn clean package

运行 Jar:

java -jar target/demo-0.0.1-SNAPSHOT.jar

十一、常见问题与调试技巧

  • 端口被占用? 修改 application.yml 中的 server.port

  • 404 页面? 检查路径是否映射正确

  • 自动配置失效? 查看是否存在 @SpringBootApplication@ComponentScan 被覆盖的问题

  • 调试 SQL? 开启 spring.jpa.show-sql=true


十二、进阶学习建议

  • 学习 Spring Boot 与 SSM 的整合

  • 理解 Spring Boot 自动配置原理(查看源码:spring-boot-autoconfigure

  • 深入使用 AOP、缓存、消息队列(RabbitMQ/Kafka)

  • 掌握 Spring Cloud 微服务架构

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词