diff --git a/mybatis-flex-spring/pom.xml b/mybatis-flex-spring/pom.xml index f3708bce..b6bf14c9 100644 --- a/mybatis-flex-spring/pom.xml +++ b/mybatis-flex-spring/pom.xml @@ -41,6 +41,16 @@ spring-jdbc + + org.springframework.batch + spring-batch-core + + + + org.springframework.batch + spring-batch-infrastructure + + com.github.gavlyukovskiy datasource-decorator-spring-boot-autoconfigure diff --git a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/MyBatisFlexCursorItemReader.java b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/MyBatisFlexCursorItemReader.java new file mode 100644 index 00000000..fd85fc63 --- /dev/null +++ b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/MyBatisFlexCursorItemReader.java @@ -0,0 +1,98 @@ +package com.mybatisflex.spring.batch; + +import com.mybatisflex.core.BaseMapper; +import com.mybatisflex.core.query.QueryWrapper; +import org.apache.ibatis.cursor.Cursor; +import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader; +import org.springframework.beans.factory.InitializingBean; + +import java.util.*; + +import static org.springframework.util.Assert.notNull; +import static org.springframework.util.ClassUtils.getShortName; + +/** + * 游标模式读取 + * @author zhangjian + */ +public class MyBatisFlexCursorItemReader extends AbstractItemCountingItemStreamItemReader + implements InitializingBean { + + /** + * 当前的mapper + */ + private BaseMapper mapper; + + /** + * 拼接的入参列表 + */ + private QueryWrapper queryWrapper; + + /** + * + */ + private Cursor cursor; + + /** + * + */ + private Iterator cursorIterator; + + public MyBatisFlexCursorItemReader() { + setName(getShortName(MyBatisFlexCursorItemReader.class)); + } + + /** + * 当前的mapper对象 + * @param mapper + */ + public void setMapper(BaseMapper mapper) { + this.mapper = mapper; + } + + /** + * 当前的参数对象 + * @param queryWrapper + */ + public void setQueryWrapper(QueryWrapper queryWrapper) { + this.queryWrapper = queryWrapper; + } + + @Override + protected T doRead() throws Exception { + T next = null; + if (cursorIterator.hasNext()) { + next = cursorIterator.next(); + } + return next; + } + + @Override + protected void doOpen() { + if (Objects.isNull(this.mapper) || Objects.isNull(this.queryWrapper)) { + throw new IllegalArgumentException("mapper or queryWrapper is required."); + } + this.cursor = this.mapper.selectCursorByQuery(queryWrapper); + cursorIterator = cursor.iterator(); + } + + @Override + protected void doClose() throws Exception { + if (cursor != null) { + cursor.close(); + } + cursorIterator = null; + } + + /** + * Check mandatory properties. + * + * @see InitializingBean#afterPropertiesSet() + */ + @Override + public void afterPropertiesSet() { + notNull(mapper, "A BaseMapper is required."); + notNull(queryWrapper, "A queryWrapper is required."); + } + +} diff --git a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/MybatisFlexBatchItemWriter.java b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/MybatisFlexBatchItemWriter.java new file mode 100644 index 00000000..dd90a6ea --- /dev/null +++ b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/MybatisFlexBatchItemWriter.java @@ -0,0 +1,76 @@ +package com.mybatisflex.spring.batch; + +import com.mybatisflex.core.BaseMapper; +import org.mybatis.logging.Logger; +import org.mybatis.logging.LoggerFactory; +import org.springframework.batch.item.ItemWriter; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.dao.EmptyResultDataAccessException; + +import java.util.List; +import java.util.Objects; + +import static org.springframework.util.Assert.notNull; + +/** + * mybatisflex实现的数据写入工具 + * @author zhangjian + * @param + */ +public class MybatisFlexBatchItemWriter implements ItemWriter, InitializingBean { + + private static final Logger LOGGER = LoggerFactory.getLogger(MybatisFlexBatchItemWriter.class); + + private BaseMapper mapper; + + private boolean assertUpdates = true; + + /** + * Public setter for the flag that determines whether an assertion is made that number of BatchResult objects returned + * is one and all items cause at least one row to be updated. + * + * @param assertUpdates the flag to set. Defaults to true; + */ + public void setAssertUpdates(boolean assertUpdates) { + this.assertUpdates = assertUpdates; + } + + /** + * mapper对象 + * @param mapper + */ + public void setMapper(BaseMapper mapper) { + if (Objects.isNull(mapper)) + throw new RuntimeException("MybatisFlex Mapper can't be null!"); + + this.mapper = mapper; + } + + /** + * Check mandatory properties - there must be an SqlSession and a statementId. + */ + @Override + public void afterPropertiesSet() { + notNull(mapper, "A Mapper is required."); + } + + /** + * {@inheritDoc} + */ + @Override + public void write(final List items) { + + if (!items.isEmpty()) { + LOGGER.debug(() -> "Executing batch with " + items.size() + " items."); + int results = this.mapper.insertBatch((List) items); + + if (assertUpdates) { + if (results != items.size()) { + throw new EmptyResultDataAccessException( + "Items.size + " + items.size() + " doesn't match the number of updated rows: " + results, 1); + + } + } + } + } +} diff --git a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/MybatisFlexPagingItemReader.java b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/MybatisFlexPagingItemReader.java new file mode 100644 index 00000000..22b797c4 --- /dev/null +++ b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/MybatisFlexPagingItemReader.java @@ -0,0 +1,84 @@ +package com.mybatisflex.spring.batch; + +import com.mybatisflex.core.BaseMapper; +import com.mybatisflex.core.paginate.Page; +import com.mybatisflex.core.query.QueryWrapper; +import org.mybatis.spring.batch.MyBatisPagingItemReader; +import org.springframework.batch.item.database.AbstractPagingItemReader; + +import java.util.concurrent.CopyOnWriteArrayList; + +import static org.springframework.util.Assert.notNull; +import static org.springframework.util.ClassUtils.getShortName; + +/** + * mybatis-flex的分页读取器 + * @author zhangjian + * @param 实体类型 + */ +public class MybatisFlexPagingItemReader extends AbstractPagingItemReader { + + /** + * 当前的mapper + */ + private BaseMapper mapper; + + /** + * 拼接的入参列表 + */ + private QueryWrapper queryWrapper; + + public MybatisFlexPagingItemReader() { + setName(getShortName(MyBatisPagingItemReader.class)); + } + + /** + * 当前的mapper对象 + * @param mapper + */ + public void setMapper(BaseMapper mapper) { + this.mapper = mapper; + } + + /** + * 当前的参数对象 + * @param queryWrapper + */ + public void setQueryWrapper(QueryWrapper queryWrapper) { + this.queryWrapper = queryWrapper; + } + + /** + * Check mandatory properties. + * + * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() + */ + @Override + public void afterPropertiesSet() throws Exception { + super.afterPropertiesSet(); + notNull(mapper, "mapper is required."); + notNull(queryWrapper, "querywrapper is required."); + } + + @Override + protected void doReadPage() { + if (results == null) { + results = new CopyOnWriteArrayList<>(); + } else { + results.clear(); + } + Page paginate = mapper.paginate(getPage() + 1, getPageSize(), queryWrapper); + results.addAll(paginate.getRecords()); + } + + @Override + protected void doJumpToPage(int itemIndex) { + if (results == null) { + results = new CopyOnWriteArrayList<>(); + } else { + results.clear(); + } + Page paginate = mapper.paginate(itemIndex + 1, getPageSize(), queryWrapper); + results.addAll(paginate.getRecords()); + } +} diff --git a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/builder/MyBatisFlexBatchItemWriterBuilder.java b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/builder/MyBatisFlexBatchItemWriterBuilder.java new file mode 100644 index 00000000..55892aa7 --- /dev/null +++ b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/builder/MyBatisFlexBatchItemWriterBuilder.java @@ -0,0 +1,55 @@ +package com.mybatisflex.spring.batch.builder; + +import com.mybatisflex.spring.batch.MybatisFlexBatchItemWriter; +import com.mybatisflex.core.BaseMapper; + +import java.util.Optional; + +/** + * 构造MybatisFlex数据的写入工具 + * + * @author zhangjian + * + * @see MybatisFlexBatchItemWriter + */ +public class MyBatisFlexBatchItemWriterBuilder { + + /** + * mapper对象 + */ + private BaseMapper mapper; + + private Boolean assertUpdates; + + /** + * mapper对象 + * @param mapper + * @return + */ + public MyBatisFlexBatchItemWriterBuilder mapper(BaseMapper mapper) { + this.mapper = mapper; + return this; + } + + /** + * 是否更新标志位 + * @param assertUpdates + * @return + */ + public MyBatisFlexBatchItemWriterBuilder assertUpdates(boolean assertUpdates) { + this.assertUpdates = assertUpdates; + return this; + } + + /** + * 构建写入工具 + * @return + */ + public MybatisFlexBatchItemWriter build() { + MybatisFlexBatchItemWriter writer = new MybatisFlexBatchItemWriter<>(); + writer.setMapper(this.mapper); + Optional.ofNullable(this.assertUpdates).ifPresent(writer::setAssertUpdates); + return writer; + } + +} diff --git a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/builder/MyBatisFlexCursorItemReaderBuilder.java b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/builder/MyBatisFlexCursorItemReaderBuilder.java new file mode 100644 index 00000000..bc3d7b6d --- /dev/null +++ b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/builder/MyBatisFlexCursorItemReaderBuilder.java @@ -0,0 +1,88 @@ +package com.mybatisflex.spring.batch.builder; + +import com.mybatisflex.spring.batch.MyBatisFlexCursorItemReader; +import com.mybatisflex.core.BaseMapper; +import com.mybatisflex.core.query.QueryWrapper; +import org.mybatis.spring.batch.MyBatisCursorItemReader; + +import java.util.Optional; + +/** + * MyBatisCursorItemReader 构造工具 + * @author zhangjian + * @param + */ +public class MyBatisFlexCursorItemReaderBuilder { + + /** + * mapper对象 + */ + private BaseMapper mapper; + + /** + * 查询条件对象 + */ + private QueryWrapper queryWrapper; + + private Boolean saveState; + + /** + * 最大读取行数 + */ + private Integer maxItemCount; + + /** + * 设置mapper对象 + * @param mapper + * @return + */ + public MyBatisFlexCursorItemReaderBuilder mapper(BaseMapper mapper) { + this.mapper = mapper; + return this; + } + + /** + * 设置查询条件 + * @param queryWrapper + * @return + */ + public MyBatisFlexCursorItemReaderBuilder queryWrapper(QueryWrapper queryWrapper) { + this.queryWrapper = queryWrapper; + return this; + } + + /** + * 保存状态标志位 + * @param saveState + * @return + */ + public MyBatisFlexCursorItemReaderBuilder saveState(boolean saveState) { + this.saveState = saveState; + return this; + } + + /** + * 数据读取最大行数 + * @param maxItemCount + * @return + */ + public MyBatisFlexCursorItemReaderBuilder maxItemCount(int maxItemCount) { + this.maxItemCount = maxItemCount; + return this; + } + + /** + * Returns a fully built {@link MyBatisFlexCursorItemReader}. + * + * @return the reader + */ + public MyBatisFlexCursorItemReader build() { + MyBatisFlexCursorItemReader reader = new MyBatisFlexCursorItemReader<>(); + reader.setMapper(this.mapper); + reader.setQueryWrapper(this.queryWrapper); + Optional.ofNullable(this.saveState).ifPresent(reader::setSaveState); + Optional.ofNullable(this.maxItemCount).ifPresent(reader::setMaxItemCount); + return reader; + } + +} diff --git a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/builder/MyBatisFlexPagingItemReaderBuilder.java b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/builder/MyBatisFlexPagingItemReaderBuilder.java new file mode 100644 index 00000000..50ab7357 --- /dev/null +++ b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/batch/builder/MyBatisFlexPagingItemReaderBuilder.java @@ -0,0 +1,107 @@ +package com.mybatisflex.spring.batch.builder; + +import com.mybatisflex.core.BaseMapper; +import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.spring.batch.MybatisFlexPagingItemReader; + +import java.util.Optional; + +/** + * MybatisFlexPagingItemReader 构造工具 + * @author zhangjian + * @param + */ +public class MyBatisFlexPagingItemReaderBuilder { + + /** + * mapper对象 + */ + private BaseMapper mapper; + + /** + * 查询条件对象 + */ + private QueryWrapper queryWrapper; + + /** + * 分页大小 + */ + private Integer pageSize; + + /** + * 保存状态标志位 + */ + private Boolean saveState; + + /** + * 数据最大读取数量 + */ + private Integer maxItemCount; + + /** + * 设置mapper + * @param mapper + * @return + */ + public MyBatisFlexPagingItemReaderBuilder mapper(BaseMapper mapper) { + this.mapper = mapper; + return this; + } + + /** + * 设置查询条件 + * @param queryWrapper + * @return + */ + public MyBatisFlexPagingItemReaderBuilder queryWrapper(QueryWrapper queryWrapper) { + this.queryWrapper = queryWrapper; + return this; + } + + /** + * 分页大小 + * @param pageSize + * @return + */ + public MyBatisFlexPagingItemReaderBuilder pageSize(int pageSize) { + this.pageSize = pageSize; + return this; + } + + /** + * 是否更新状态标志位 + * @param saveState + * @return + */ + public MyBatisFlexPagingItemReaderBuilder saveState(boolean saveState) { + this.saveState = saveState; + return this; + } + + /** + * Configure the max number of items to be read. + * default Integer.Max_Value + * @param maxItemCount + * @return + */ + public MyBatisFlexPagingItemReaderBuilder maxItemCount(int maxItemCount) { + this.maxItemCount = maxItemCount; + return this; + } + + /** + * Returns a fully built {@link MybatisFlexPagingItemReader}. + * + * @return the reader + */ + public MybatisFlexPagingItemReader build() { + MybatisFlexPagingItemReader reader = new MybatisFlexPagingItemReader<>(); + reader.setMapper(this.mapper); + reader.setQueryWrapper(this.queryWrapper); + Optional.ofNullable(this.pageSize).ifPresent(reader::setPageSize); + Optional.ofNullable(this.saveState).ifPresent(reader::setSaveState); + Optional.ofNullable(this.maxItemCount).ifPresent(reader::setMaxItemCount); + return reader; + } + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml index 05247ea5..196b0692 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml @@ -43,6 +43,12 @@ 1.2.18 + + org.springframework.boot + spring-boot-starter-batch + ${spring-boot.version} + + @@ -64,6 +70,12 @@ mysql-connector-j + + org.springdoc + springdoc-openapi-ui + 1.8.0 + + diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/batch/BatchConfiguration.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/batch/BatchConfiguration.java new file mode 100755 index 00000000..85003316 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/batch/BatchConfiguration.java @@ -0,0 +1,50 @@ +package com.mybatisflex.test.batch; + +import com.mybatisflex.spring.FlexTransactionManager; +import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.support.SimpleJobLauncher; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.SimpleAsyncTaskExecutor; +import org.springframework.transaction.PlatformTransactionManager; + +/** + * 配置batch + */ +@Configuration +@EnableBatchProcessing +public class BatchConfiguration extends DefaultBatchConfigurer { + + /** + * springbatch默认配置;使用内存库 + * + * @return + */ + + @Override + protected JobRepository createJobRepository() throws Exception { + MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(); + return factory.getObject(); + } + + @Override + protected JobLauncher createJobLauncher() throws Exception { + SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); + jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor()); //转换为异步任务 + jobLauncher.setJobRepository(this.getJobRepository()); + jobLauncher.afterPropertiesSet(); + return jobLauncher; + } + + /** + * @return + */ + @Override + public PlatformTransactionManager getTransactionManager() { + FlexTransactionManager mybatisTransactionManager = new FlexTransactionManager(); + return mybatisTransactionManager; + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/batch/BatchJobConfiguration.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/batch/BatchJobConfiguration.java new file mode 100644 index 00000000..9ca9fc43 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/batch/BatchJobConfiguration.java @@ -0,0 +1,180 @@ +package com.mybatisflex.test.batch; + +import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.spring.batch.MybatisFlexBatchItemWriter; +import com.mybatisflex.spring.batch.MybatisFlexPagingItemReader; +import com.mybatisflex.spring.batch.builder.MyBatisFlexBatchItemWriterBuilder; +import com.mybatisflex.spring.batch.builder.MyBatisFlexPagingItemReaderBuilder; +import com.mybatisflex.test.mapper.MyAccountMapper; +import com.mybatisflex.test.model.Account; +import com.mybatisflex.test.model.table.AccountTableDef; +import org.springframework.batch.core.*; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepScope; +import org.springframework.batch.core.step.tasklet.TaskletStep; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; + +/** + * 生成batch demo + */ +@Configuration +public class BatchJobConfiguration implements StepExecutionListener, JobExecutionListener { + + /** + * 帐户信息 + */ + @Autowired + @Lazy + private MyAccountMapper accountMapper; + + + /** + * 帐户导入信息 + * + * @param jobBuilders + * @return + */ + @Bean(name = "testImportJob") + public Job testImportJob(JobBuilderFactory jobBuilders, + @Qualifier("accountStep") Step accountStep) { + return jobBuilders.get("testImportJob") + .start(accountStep).listener(this) + .build(); + } + + /** + * 帐户执行阶段 + * + * @param stepBuilders + * @return + */ + @Bean + public Step accountStep(StepBuilderFactory stepBuilders, + @Qualifier("accountReader") MybatisFlexPagingItemReader accountReader, + @Qualifier("accountProcessor") ItemProcessor accountProcessor, + @Qualifier("accountWriter") MybatisFlexBatchItemWriter accountWriter) { + TaskletStep step = stepBuilders.get("创建帐户") + .chunk(10) + .reader(accountReader) + .processor(accountProcessor) + .writer(accountWriter) + .build(); + + step.registerStepExecutionListener(this); + return step; + } + + /** + * 帐户读取 + * @return + */ + @Bean + @StepScope + public MybatisFlexPagingItemReader accountReader() { + QueryWrapper query = QueryWrapper.create(); + query.select(AccountTableDef.ACCOUNT.ALL_COLUMNS) + .from(AccountTableDef.ACCOUNT); + + MyBatisFlexPagingItemReaderBuilder builder = new MyBatisFlexPagingItemReaderBuilder(); + MybatisFlexPagingItemReader reader = builder.mapper(accountMapper) + .pageSize(10) + .queryWrapper(query) + .build(); + + return reader; + } + + /** + * 数据转换 + * @return + */ + @Bean + @StepScope + public ItemProcessor accountProcessor() { + ItemProcessor processor = new ItemProcessor() { + @Override + public Account process(Account account) throws Exception { + Account entity = new Account(); + BeanUtils.copyProperties(account, entity); + entity.setUserName(account.getUserName() + "_1"); + entity.setId(null); + return entity; + } + }; + + return processor; + } + + /** + * 帐户写入 + * + * @return + */ + @Bean + @StepScope + public MybatisFlexBatchItemWriter accountWriter() { + MybatisFlexBatchItemWriter writer = new MyBatisFlexBatchItemWriterBuilder() + .mapper(accountMapper) + .build(); + + return writer; + } + +//============以下是事件监听============== + + /** + * Initialize the state of the listener with the {@link StepExecution} from + * the current scope. + * + * @param stepExecution instance of {@link StepExecution}. + */ + @Override + public void beforeStep(StepExecution stepExecution) { + } + + /** + * Give a listener a chance to modify the exit status from a step. The value + * returned will be combined with the normal exit status using + * {@link ExitStatus#and(ExitStatus)}. + *

+ * Called after execution of step's processing logic (both successful or + * failed). Throwing exception in this method has no effect, it will only be + * logged. + * + * @param stepExecution {@link StepExecution} instance. + * @return an {@link ExitStatus} to combine with the normal value. Return + * {@code null} to leave the old value unchanged. + */ + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + System.out.println(stepExecution.getStepName() + " 共计导入:" + stepExecution.getWriteCount() + "行数据"); + return stepExecution.getExitStatus(); + } + + /** + * Callback before a job executes. + * + * @param jobExecution the current {@link JobExecution} + */ + @Override + public void beforeJob(JobExecution jobExecution) { + } + + /** + * Callback after completion of a job. Called after both both successful and + * failed executions. To perform logic on a particular status, use + * "if (jobExecution.getStatus() == BatchStatus.X)". + * + * @param jobExecution the current {@link JobExecution} + */ + @Override + public void afterJob(JobExecution jobExecution) { + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/config/OpenApiConfig.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/config/OpenApiConfig.java new file mode 100755 index 00000000..2ff1c6d5 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/config/OpenApiConfig.java @@ -0,0 +1,37 @@ +package com.mybatisflex.test.config; + +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * swagger配置 + * + * @author sam + */ +@Configuration +public class OpenApiConfig { + + /** + * 本系统端口 + */ + @Value("${spring-boot.version:2.7.11}") + private String version; + + /** + * swagger2 http://localhost:8080/swagger-ui.html + * swagger3 http://localhost:8080/swagger-ui/index.html + * + * @return + */ + @Bean + public OpenAPI myOpenAPI() { + return new OpenAPI() + .info(new Info() + .title("mybatis-flex测试") + .description("mybatis-flex") + .version(version)); + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/BatchController.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/BatchController.java new file mode 100644 index 00000000..6a7df833 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/BatchController.java @@ -0,0 +1,77 @@ +package com.mybatisflex.test.controller; + +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Lazy; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * 测试SpringBatch功能的controller + */ +@RestController +@CrossOrigin +@RequestMapping("job") +@Tag(name = "SpringBatch测试", description = "SpringBatch测试") +public class BatchController { + + /** + * job执行器 + */ + @Autowired + @Lazy + private JobLauncher jobLauncher; + + /** + * springbatch job + */ + @Autowired + @Lazy + @Qualifier("testImportJob") + private Job testImportJob; + + /** + * springbatch job 测试 + * + * @return + */ + @GetMapping("testImportJob") + public Map testImportJob() { + try { + JobParametersBuilder parameters = new JobParametersBuilder(); + //为了防止任务无法重复创建的bug + parameters.addDate("createDate", new Date()); + JobParameters jobParameters = parameters.toJobParameters(); + + JobExecution run1 = jobLauncher.run(testImportJob, jobParameters); + run1.setStartTime(new Date()); + while (run1.isRunning()) { + Thread.sleep(500); + } + Map map = new HashMap<>(); + map.put("code", 200); + map.put("success", true); + map.put("message", "job completed"); + return map; + } catch (Exception ex) { + Map map = new HashMap<>(); + map.put("code", 500); + map.put("success", false); + map.put("message", ex.getMessage()); + return map; + } + } + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml index f9c84f2d..a99845b1 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml @@ -36,6 +36,26 @@ # url: jdbc:mysql://localhost:3306/flex_test # username: root # password: 12345678 + +#禁止项目启动自动运行job +spring: + batch: + job: + enabled: false + +#springdoc-openapi项目配置 +springdoc: + swagger-ui: + path: /swagger-ui.html + tags-sorter: alpha + operations-sorter: alpha + api-docs: + path: /v3/api-docs + group-configs: + - group: 'default' + paths-to-match: '/**' + packages-to-scan: com.mybatisflex.test.controller + mybatis-flex: datasource: ds1: diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/AccountMapper.xml similarity index 100% rename from mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml rename to mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/AccountMapper.xml diff --git a/mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml index 392aa70f..c59a3c59 100644 --- a/mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-spring-cloud-test/pom.xml @@ -81,6 +81,12 @@ spring-boot-starter-test test + + com.mybatis-flex + mybatis-flex-spring-test + 1.10.9 + compile + diff --git a/pom.xml b/pom.xml index c71750b8..e4693e81 100644 --- a/pom.xml +++ b/pom.xml @@ -77,6 +77,7 @@ 4.0.3 5.3.27 + 4.3.10 2.7.11 3.0.1 1.1.0-java8 @@ -180,6 +181,18 @@ ${spring-boot.version} + + org.springframework.batch + spring-batch-core + ${spring-batch.version} + + + + org.springframework.batch + spring-batch-infrastructure + ${spring-batch.version} + + junit