spring boot(学习笔记第十九课)
- Spring boot的batch框架,以及Swagger3(
OpenAPI
)整合
学习内容:
- Spring boot的batch框架
- Spring boot的Swagger3(
OpenAPI
)整合
1. Spring boot batch框架
Spring Batch
是什么Spring Batch
是一个轻量级的、完善的批处理框架,旨在帮助企业建立健壮、高效的批处理应用。Spring Batch
的执行流程主要包括以下几个步骤:- 配置JobLaunch
- 配置Job
- 配置Step
- 配置ItemReader来读取数据
- 配置ItemProcessor来处理数据
- 配置ItemWriter来写数据
- 配置JobRepository来管理作业状态
- 创建并运行作业
- 练习使用
Spring Batch
Spring Batch Guide(可以参照最新的官方guide)
- 加入必要的依赖(这里使用
jdbc
连接mysql
数据库)<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-batch</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>
Spring Batch
会创建出自己管理的表,所以要执行sql
进行创建。sql
都已经准备在导入的依赖中准备好了。spring.batch.jdbc.schema=classpath:/org/springframework/batch/core/schema-mysql.sql
将上面的sql
拷贝出来,在msql
数据库中执行。
- 配置
application.properties
进行数据库设置。
本来spring.batch.jdbc.initialize-schema=always
能够进行自动创建Spring Batch
需要的table
,但是没有成功,手动创建了。spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=finlay spring.datasource.password=123456 spring.batch.jdbc.initialize-schema=always spring.batch.jdbc.table-prefix=BATCH_ spring.batch.jdbc.schema=classpath:/org/springframework/batch/core/schema-mysql.sql spring.batch.job.enabled=false
- 准备数据文件
data.csv
。
注意,接下来要在后面的代码中,load csv文件的时候,(new ClassPathResource("data.csv"))
从ClassPath
中取得,所以事先放在/resources
这个ClassPath
的直下。
- 准备
User
的entity
类。@Data public class User {private Integer id;private String name;private String address;private String gender; }
- 准备
CsvBatchJobConfig
类,配置Spring Batch
的Job
。@Configuration public class CsvBatchJobConfig {@BeanFlatFileItemReader<User> itemReader() {FlatFileItemReader<User> reader =new FlatFileItemReader<User>();reader.setLinesToSkip(1);reader.setResource(new ClassPathResource("data.csv"));reader.setLineMapper(new DefaultLineMapper<>() {{setLineTokenizer(new DelimitedLineTokenizer() {{setNames("id", "name", "address", "gender");setDelimiter("\t");}});setFieldSetMapper(new BeanWrapperFieldSetMapper<>() {{setTargetType(User.class);}});}});return reader;}@Beanpublic JdbcBatchItemWriter<User> jdbcBatchItemWriter(DataSource dataSource) {return new JdbcBatchItemWriterBuilder<User>().sql("insert into user(id,name,address,gender)" +"values(:id,:name,:address,:gender)").dataSource(dataSource).beanMapped().build();}@Beanpublic Step csvStep(JobRepository jobRepository,DataSourceTransactionManager transactionManager,FlatFileItemReader<User> flatFileItemReader,JdbcBatchItemWriter<User> jdbcBatchItemWriter) {SimpleStepBuilder<User, User> stepBuilder =new StepBuilder("csvStep", jobRepository).<User, User>chunk(10, transactionManager).reader(flatFileItemReader).writer(jdbcBatchItemWriter);return stepBuilder.build();}@Beanpublic Job csvJob(JobRepository jobRepository,Step csvStep) {return new JobBuilder("csvJob", jobRepository).start(csvStep).build();} }
- 在
Spring Boot
的主类中@EnableBatchProcessing(databaseType = "mysql")
@SpringBootApplication @EnableBatchProcessing(databaseType = "mysql") public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}@BeanQueue queue(){return new ActiveMQQueue("amq");} }
- 定义
BatchController
,进行调用JobLauncher
。@Controller public class BatchController {@Autowiredpublic JobLauncher jobLauncher;@AutowiredJob csvJob;@GetMapping("csv_import")@ResponseBodypublic String csvImport() {String result;try {jobLauncher.run(csvJob,new JobParametersBuilder().toJobParameters());result = "csv job ok";} catch (Exception e) {e.printStackTrace();result = "csv job ng";}return result;} }
- 定义
User
目标表在mysql
。CREATE TABLE `USER` (`id` int(11) NOT NULL,`name` varchar(255) NOT NULL,`address` varchar(255) NOT NULL,`gender` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
- 启动
Spring Boot
应用程序,访问BatchController
。
- 检查
mysql
数据库的User
表。
可以看到数据都已经导入进去了。
2. Spring boot的Swagger3整合
前后端分离,一般采用Swagger3
,将RESTful API
文档和代码结合在一起。这样在维护代码的时候,自然生成最新的RESTful API
文档。
这里最新的Spring Boot
不支持Swagger2
,可以使用Swagger3 OpenAPI
- 在项目中导入
Swagger3
。- 导入需要的依赖。
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.6.0</version></dependency>
- 配置
springdoc-openapi Swagger3
@Configuration public class Swagger3Config {@BeanOpenAPI openAPI() {return new OpenAPI().info(new Info().title("项目接口API文档").description("项目接口API文档").version("v0.1").license(new License().name("Apache2.0").url("http://springdoc.org"))).externalDocs(new ExternalDocumentation().description("SpringShop Wiki Documentation").url("https://springshop.wiki.github.org/docs"));} }
- 在
application.properties
中限定生成springdoc
的package
springdoc.packagesToScan=com.example.demo.controller.swagger
- 定义自己项目的
controller
@RestController public class Swagger3Controller {@Operation(summary = "查询用户", description = "根据Id查询用户")@Parameter(in = ParameterIn.PATH, name = "id", description= "用户Id", required = true)@GetMapping("/user/{id}")public String getUserById(@PathVariable Integer id) {return "/user/" + id;}@ApiResponses({@ApiResponse(responseCode= "200", description= "删除成功"),@ApiResponse(responseCode= "500", description= "删除失败")})@Operation(summary= "删除用户", description= "通过Id删除用户")@DeleteMapping("/user/{id}")public Integer deleteUserById(@PathVariable Integer id) {return id;}@Operation(summary = "添加用户", description = "添加一个用户,传入用户名和地址")@Parameters({@Parameter(in = ParameterIn.QUERY,name = "username",description = "用户名",required = true),@Parameter(in = ParameterIn.QUERY,name = "address",description= "地址",required = true)})@PostMapping("/user")public String addUser(@RequestParam String username,@RequestParam String address) {return username + address;}@Operation(summary = "修改用户", description = "修改用户,传入用户信息")@PutMapping("/user")public String updateUser(@RequestBody User user) {return user.toString();}@Hidden@GetMapping("/ignore")public String ignoreMethod() {return "ignored method";} }
http://localhost:8080/swagger-ui/index.html
- 导入需要的依赖。