mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
commit
60a30deea2
@ -41,6 +41,16 @@
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.batch</groupId>
|
||||
<artifactId>spring-batch-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.batch</groupId>
|
||||
<artifactId>spring-batch-infrastructure</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.gavlyukovskiy</groupId>
|
||||
<artifactId>datasource-decorator-spring-boot-autoconfigure</artifactId>
|
||||
|
||||
@ -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<T> extends AbstractItemCountingItemStreamItemReader<T>
|
||||
implements InitializingBean {
|
||||
|
||||
/**
|
||||
* 当前的mapper
|
||||
*/
|
||||
private BaseMapper<T> mapper;
|
||||
|
||||
/**
|
||||
* 拼接的入参列表
|
||||
*/
|
||||
private QueryWrapper queryWrapper;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private Cursor<T> cursor;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private Iterator<T> cursorIterator;
|
||||
|
||||
public MyBatisFlexCursorItemReader() {
|
||||
setName(getShortName(MyBatisFlexCursorItemReader.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前的mapper对象
|
||||
* @param mapper
|
||||
*/
|
||||
public void setMapper(BaseMapper<T> 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.");
|
||||
}
|
||||
|
||||
}
|
||||
@ -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 <T>
|
||||
*/
|
||||
public class MybatisFlexBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(MybatisFlexBatchItemWriter.class);
|
||||
|
||||
private BaseMapper<T> 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<T> 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<? extends T> items) {
|
||||
|
||||
if (!items.isEmpty()) {
|
||||
LOGGER.debug(() -> "Executing batch with " + items.size() + " items.");
|
||||
int results = this.mapper.insertBatch((List<T>) items);
|
||||
|
||||
if (assertUpdates) {
|
||||
if (results != items.size()) {
|
||||
throw new EmptyResultDataAccessException(
|
||||
"Items.size + " + items.size() + " doesn't match the number of updated rows: " + results, 1);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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 <T> 实体类型
|
||||
*/
|
||||
public class MybatisFlexPagingItemReader<T> extends AbstractPagingItemReader<T> {
|
||||
|
||||
/**
|
||||
* 当前的mapper
|
||||
*/
|
||||
private BaseMapper<T> mapper;
|
||||
|
||||
/**
|
||||
* 拼接的入参列表
|
||||
*/
|
||||
private QueryWrapper queryWrapper;
|
||||
|
||||
public MybatisFlexPagingItemReader() {
|
||||
setName(getShortName(MyBatisPagingItemReader.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前的mapper对象
|
||||
* @param mapper
|
||||
*/
|
||||
public void setMapper(BaseMapper<T> 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<T> 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<T> paginate = mapper.paginate(itemIndex + 1, getPageSize(), queryWrapper);
|
||||
results.addAll(paginate.getRecords());
|
||||
}
|
||||
}
|
||||
@ -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<T> {
|
||||
|
||||
/**
|
||||
* mapper对象
|
||||
*/
|
||||
private BaseMapper<T> mapper;
|
||||
|
||||
private Boolean assertUpdates;
|
||||
|
||||
/**
|
||||
* mapper对象
|
||||
* @param mapper
|
||||
* @return
|
||||
*/
|
||||
public MyBatisFlexBatchItemWriterBuilder<T> mapper(BaseMapper<T> mapper) {
|
||||
this.mapper = mapper;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否更新标志位
|
||||
* @param assertUpdates
|
||||
* @return
|
||||
*/
|
||||
public MyBatisFlexBatchItemWriterBuilder<T> assertUpdates(boolean assertUpdates) {
|
||||
this.assertUpdates = assertUpdates;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建写入工具
|
||||
* @return
|
||||
*/
|
||||
public MybatisFlexBatchItemWriter<T> build() {
|
||||
MybatisFlexBatchItemWriter<T> writer = new MybatisFlexBatchItemWriter<>();
|
||||
writer.setMapper(this.mapper);
|
||||
Optional.ofNullable(this.assertUpdates).ifPresent(writer::setAssertUpdates);
|
||||
return writer;
|
||||
}
|
||||
|
||||
}
|
||||
@ -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 <T>
|
||||
*/
|
||||
public class MyBatisFlexCursorItemReaderBuilder<T> {
|
||||
|
||||
/**
|
||||
* mapper对象
|
||||
*/
|
||||
private BaseMapper<T> mapper;
|
||||
|
||||
/**
|
||||
* 查询条件对象
|
||||
*/
|
||||
private QueryWrapper queryWrapper;
|
||||
|
||||
private Boolean saveState;
|
||||
|
||||
/**
|
||||
* 最大读取行数
|
||||
*/
|
||||
private Integer maxItemCount;
|
||||
|
||||
/**
|
||||
* 设置mapper对象
|
||||
* @param mapper
|
||||
* @return
|
||||
*/
|
||||
public MyBatisFlexCursorItemReaderBuilder<T> mapper(BaseMapper<T> mapper) {
|
||||
this.mapper = mapper;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置查询条件
|
||||
* @param queryWrapper
|
||||
* @return
|
||||
*/
|
||||
public MyBatisFlexCursorItemReaderBuilder<T> queryWrapper(QueryWrapper queryWrapper) {
|
||||
this.queryWrapper = queryWrapper;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存状态标志位
|
||||
* @param saveState
|
||||
* @return
|
||||
*/
|
||||
public MyBatisFlexCursorItemReaderBuilder<T> saveState(boolean saveState) {
|
||||
this.saveState = saveState;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据读取最大行数
|
||||
* @param maxItemCount
|
||||
* @return
|
||||
*/
|
||||
public MyBatisFlexCursorItemReaderBuilder<T> maxItemCount(int maxItemCount) {
|
||||
this.maxItemCount = maxItemCount;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a fully built {@link MyBatisFlexCursorItemReader}.
|
||||
*
|
||||
* @return the reader
|
||||
*/
|
||||
public MyBatisFlexCursorItemReader<T> build() {
|
||||
MyBatisFlexCursorItemReader<T> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -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 <T>
|
||||
*/
|
||||
public class MyBatisFlexPagingItemReaderBuilder<T> {
|
||||
|
||||
/**
|
||||
* mapper对象
|
||||
*/
|
||||
private BaseMapper<T> mapper;
|
||||
|
||||
/**
|
||||
* 查询条件对象
|
||||
*/
|
||||
private QueryWrapper queryWrapper;
|
||||
|
||||
/**
|
||||
* 分页大小
|
||||
*/
|
||||
private Integer pageSize;
|
||||
|
||||
/**
|
||||
* 保存状态标志位
|
||||
*/
|
||||
private Boolean saveState;
|
||||
|
||||
/**
|
||||
* 数据最大读取数量
|
||||
*/
|
||||
private Integer maxItemCount;
|
||||
|
||||
/**
|
||||
* 设置mapper
|
||||
* @param mapper
|
||||
* @return
|
||||
*/
|
||||
public MyBatisFlexPagingItemReaderBuilder<T> mapper(BaseMapper<T> mapper) {
|
||||
this.mapper = mapper;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置查询条件
|
||||
* @param queryWrapper
|
||||
* @return
|
||||
*/
|
||||
public MyBatisFlexPagingItemReaderBuilder<T> queryWrapper(QueryWrapper queryWrapper) {
|
||||
this.queryWrapper = queryWrapper;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页大小
|
||||
* @param pageSize
|
||||
* @return
|
||||
*/
|
||||
public MyBatisFlexPagingItemReaderBuilder<T> pageSize(int pageSize) {
|
||||
this.pageSize = pageSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否更新状态标志位
|
||||
* @param saveState
|
||||
* @return
|
||||
*/
|
||||
public MyBatisFlexPagingItemReaderBuilder<T> 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<T> maxItemCount(int maxItemCount) {
|
||||
this.maxItemCount = maxItemCount;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a fully built {@link MybatisFlexPagingItemReader}.
|
||||
*
|
||||
* @return the reader
|
||||
*/
|
||||
public MybatisFlexPagingItemReader<T> build() {
|
||||
MybatisFlexPagingItemReader<T> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -43,6 +43,12 @@
|
||||
<version>1.2.18</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-batch</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-jdbc</artifactId>-->
|
||||
@ -64,6 +70,12 @@
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-ui</artifactId>
|
||||
<version>1.8.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.h2database</groupId>-->
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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<Account> accountWriter) {
|
||||
TaskletStep step = stepBuilders.get("创建帐户")
|
||||
.<Account, Account>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<Account,Account> accountProcessor() {
|
||||
ItemProcessor<Account,Account> processor = new ItemProcessor<Account, Account>() {
|
||||
@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<Account> 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)}.
|
||||
* <p>
|
||||
* 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) {
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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<String, Object> 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<String, Object> map = new HashMap<>();
|
||||
map.put("code", 200);
|
||||
map.put("success", true);
|
||||
map.put("message", "job completed");
|
||||
return map;
|
||||
} catch (Exception ex) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("code", 500);
|
||||
map.put("success", false);
|
||||
map.put("message", ex.getMessage());
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -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:
|
||||
|
||||
@ -81,6 +81,12 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mybatis-flex</groupId>
|
||||
<artifactId>mybatis-flex-spring-test</artifactId>
|
||||
<version>1.10.9</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
13
pom.xml
13
pom.xml
@ -77,6 +77,7 @@
|
||||
<HikariCP.version>4.0.3</HikariCP.version>
|
||||
|
||||
<spring.version>5.3.27</spring.version>
|
||||
<spring-batch.version>4.3.10</spring-batch.version>
|
||||
<spring-boot.version>2.7.11</spring-boot.version>
|
||||
<solon.version>3.0.1</solon.version>
|
||||
<loveqq.version>1.1.0-java8</loveqq.version>
|
||||
@ -180,6 +181,18 @@
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.batch</groupId>
|
||||
<artifactId>spring-batch-core</artifactId>
|
||||
<version>${spring-batch.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.batch</groupId>
|
||||
<artifactId>spring-batch-infrastructure</artifactId>
|
||||
<version>${spring-batch.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--for test-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user