Spring Boot的特点
概念:约Spring Boot 是 Spring 架的扩展和自动化,它省去了在 Spring 中需要
进行的 XML 文件配置,使得开发变得更快、更高效、更自动化。
特征:
1. SpringBoot Starter:他将常用的依赖分组进行了整合,将其合并到一个依赖中,这样就可以一次性添加到项目的Maven或Gradle构建中。
2,使编码变得简单,SpringBoot采用 JavaConfig的方式对Spring进行配置,并且提供了大量的注解,极大的提高了工作效率,比如@Configuration和@bean注解结合,基于@Configuration完成类扫描,基于@bean注解把返回值注入IOC容器。
3.自动配置:SpringBoot的自动配置特性利用了Spring对条件化配置的支持,合理地推测应用所需的bean并自动化配置他们。
4.使部署变得简单,SpringBoot内置了三种Servlet容器,Tomcat,Jetty,undertow.我们只需要一个Java的运行环境就可以跑SpringBoot的项目了,SpringBoot的项目可以打成一个jar包。
Spring Boot项目创建
Spring Boot环境配置:1.下载安装jdk
2.配置环境
Maven环境配置
应用程序启动
Spring Boot核心配置及注解
自动配置的定义:Spring Boot的自动配置是指在应用程序启动时,Spring Boot根据classpath路径下的jar包自动配置应用程序所需的一系列bean和组件,从而减少开发者的配置工作,提高开发效率。代码:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
全局配置:Spring Boot的全局配置通常是在application.properties或application.yml文件中进行的。这些文件通常放在src/main/resources目录下。
以下是一个示例的application.properties文件:
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
logging:
level:
com.example: DEBUG
以下是一个示例的application.yml文件:
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
logging:
level:
com.example: DEBUG
Spring Boot的核心注解:为@SpringBootApplication注解,该注解是Spring Boot的核心注解,由@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan三个注解组合而成
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Spring Boot属性注入:Spring Boot属性注入是指将应用程序的配置信息注入到Spring Boot的Bean中。Spring Boot支持多种属性注入方式,包括基于注解的属性注入、基于XML文件的属性注入、基于YAML文件的属性注入和基于属性文件的属性注入等。可以使用Spring Boot提供的注解,如@Value、@ConfigurationProperties等来注入属性。@Value注解可以用于单个属性的注入,而@ConfigurationProperties注解可以用于将配置信息注入到一个Java Bean中。例如:
@Component
@ConfigurationProperties(prefix = "db")
public class DatabaseProperties {
private String url;
private String username;
private String password;
// getters and setters
}
@PropertySource("classpath:application.properties")
public class AppConfig {
@Value("${db.url}")
private String dbUrl;
// ...
}
上面的代码使用了@PropertySource注解来指定属性文件的位置为classpath:application.properties,然后使用@Value注解注入了名为db.url的属性值到dbUrl变量中。
Spring Boot数据整合
在Spring Boot中,我们通常使用Spring Data JPA、MyBatis、Hibernate等库来处理数据库操作。下面,我将简单地解释一下如何使用Spring Data JPA进行数据访问。
1.添加依赖
在你的pom.xml
文件中添加Spring Data JPA 和数据库驱动的依赖。例如,如果你正在使用MySQL数据库,你需要添加以下依赖并配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
application.properties文件:
# 配置数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 配置 MyBatis
mybatis.mapper-locations=classpath:mapper/*.xml
配置文件中,我们配置了MySQL数据库的连接信息和MyBatis的Mapper文件所在目录。
MyBatis配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper class="com.example.demo.mapper.UserMapper"/>
</mappers>
</configuration>
在Mapper文件中,我们定义了一个UserMapper接口,用于操作用户数据
@Mapper
public interface UserMapper {
@Select("select * from user where id=#{id}")
User getUserById(Integer id);
@Insert("insert into user (name, age) values (#{name}, #{age})")
void insertUser(User user);
@Delete("delete from user where id=#{id}")
void deleteUser(Integer id);
@Update("update user set name=#{name}, age=#{age} where id=#{id}")
void updateUser(User user);
}
然后我们使用@Service注解注入UserMapper接口,并进行使用
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
public void insertUser(User user) {
userMapper.insertUser(user);
}
public void deleteUser(Integer id) {
userMapper.deleteUser(id);
}
public void updateUser(User user) {
userMapper.updateUser(user);
}
}
最后,在Controller中使用UserService:
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable Integer id) {
return userService.getUserById(id);
}
@PostMapping("/user")
public void insertUser(@RequestBody User user) {
userService.insertUser(user);
}
@DeleteMapping("/user/{id}")
public void deleteUser(@PathVariable Integer id) {
userService.deleteUser(id);
}
@PutMapping("/user")
public void updateUser(@RequestBody User user) {
userService.updateUser(user);
}
}
上述演示了如何在Spring Boot中整合MyBatis
Spring Boot打包部署
Spring Boot 可以使用 Maven 或 Gradle 来构建和打包你的应用程序。在完成构建之后,你可以将生成的 JAR 文件部署到生产环境中。
以下是在 Spring Boot 项目中使用 Maven 打包并部署应用程序的步骤:
- 配置 Maven
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
2.部署:直接运行jar包: 进入到项目的target目录,运行以下命令即可启动。直接运行jar包: 进入到项目的target目录,运行以下命令即可启动。
java -jar yourjarname.jar
其中nohup命令可以让程序在后台运行,&符号可以让程序后台运行。