Compare commits

...

12 Commits

Author SHA1 Message Date
Michael Yang
354f9d11a3 build: v1.11.1 release (^.^)YYa!! 2025-07-25 10:30:36 +08:00
Michael Yang
03f6c28463 build: v1.11.1 release (^.^)YYa!! 2025-07-25 10:25:31 +08:00
Michael Yang
f786fd64b5 test: fixed test 2025-07-25 09:52:51 +08:00
Michael Yang
bf973f9f06 Merge branch 'main' of https://gitee.com/mybatis-flex/mybatis-flex 2025-07-25 09:35:47 +08:00
Michael Yang
60a30deea2
!545 增加SpringBatch的支持的测试demo
Merge pull request !545 from zhj149/main
2025-07-25 01:35:11 +00:00
Michael Yang
d47465a91e Merge branch 'main' of https://github.com/mybatis-flex/mybatis-flex 2025-07-25 09:32:34 +08:00
Michael Yang
4d8c132b8d
!552 fix:lambda的orElse是方法的话,会在执行lambda之前堆栈就调用了,那么Optional的逻辑就失去作用了,并且发现了一个逻…
Merge pull request !552 from 谨宸/main
2025-07-25 01:25:03 +00:00
Michael Yang
ece2adc0e3
Merge pull request #562 from ruansheng8/fix-pkgImport
修复Optional包依赖问题
2025-07-24 12:18:05 +08:00
ruansheng
acba6066bc feat: -m core 导入Optional包依赖 2025-07-23 18:51:32 +08:00
谨宸
08d6dae776 fix:lambda的orElse是方法的话,会在执行lambda之前堆栈就调用了,那么Optional的逻辑就失去作用了,并且发现了一个逻辑问题,addDataSource没有使用到参数,会导致multiple逻辑有问题 2025-07-15 19:00:10 +08:00
zhj149
336e938471 增加SpringBatch的支持的测试demo 2025-04-28 15:13:52 +08:00
zhj149
ea03a5f3df 增加SpringBatch的支持,Linux下开发,有大小写名称的问题.所以AccountMapper改了下名字. 无法install到本地测试,没写测试 2025-04-28 13:47:12 +08:00
42 changed files with 1003 additions and 65 deletions

View File

@ -7,7 +7,7 @@ body:
attributes:
label: 这个 Bug 是否已经存在:
options:
- label: 我确定已经把 MyBatis-Flex 升级到最新版本 v1.11.0,并已搜索过现有的问题 (https://gitee.com/mybatis-flex/mybatis-flex/issues)
- label: 我确定已经把 MyBatis-Flex 升级到最新版本 v1.11.1,并已搜索过现有的问题 (https://gitee.com/mybatis-flex/mybatis-flex/issues)
required: true
- type: textarea
attributes:

View File

@ -2,6 +2,18 @@
查看 [全部代码贡献者](/zh/intro/what-is-mybatisflex.html#贡献者)。
## v1.11.1 20250725
- 新增:添加数据源缺失处理器,方便项目启动后,再通过代码添加数据源
- 新增between 条件遇到 一个参数为 null 时自动转换成 LE 或 GE 逻辑 #ICKPDB
- 新增:添加对 Spring Batch 的支持
- 修复:动态添加数据源第二次切换数据源无效的问题 #ICLQQ3
- 修复Datasource lambda 的 orElse 是方法的话,会在执行 lambda 之前堆栈就调用了,那么 Optional 的逻辑就失去作用了
- 测试:添加对 Spring Batch 的单元测试
## v1.11.0 20250713
- 新增:代码生成器 Column 支持带有范型的类型,感谢 @benshi
- 新增:对于用户常用类,启用代码折叠支持,感谢 @hewei

View File

@ -2,6 +2,18 @@
查看 [全部代码贡献者](/zh/intro/what-is-mybatisflex.html#贡献者)。
## v1.11.1 20250725
- 新增:添加数据源缺失处理器,方便项目启动后,再通过代码添加数据源
- 新增between 条件遇到 一个参数为 null 时自动转换成 LE 或 GE 逻辑 #ICKPDB
- 新增:添加对 Spring Batch 的支持
- 修复:动态添加数据源第二次切换数据源无效的问题 #ICLQQ3
- 修复Datasource lambda 的 orElse 是方法的话,会在执行 lambda 之前堆栈就调用了,那么 Optional 的逻辑就失去作用了
- 测试:添加对 Spring Batch 的单元测试
## v1.11.0 20250713
- 新增:代码生成器 Column 支持带有范型的类型,感谢 @benshi
- 新增:对于用户常用类,启用代码折叠支持,感谢 @hewei

View File

@ -53,7 +53,7 @@ VALUES (1, '张三', 18, '2020-01-11'),
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
@ -81,7 +81,7 @@ VALUES (1, '张三', 18, '2020-01-11'),
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot3-starter</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>

View File

@ -10,7 +10,7 @@
```kotlin
dependencies {
implementation("com.mybatis-flex:mybatis-flex-core:1.11.0")
implementation("com.mybatis-flex:mybatis-flex-core:1.11.1")
}
```
@ -18,7 +18,7 @@ dependencies {
```groovy
dependencies {
implementation 'com.mybatis-flex:mybatis-flex-core:1.11.0'
implementation 'com.mybatis-flex:mybatis-flex-core:1.11.1'
}
```
@ -28,7 +28,7 @@ dependencies {
```kotlin
dependencies {
implementation("com.mybatis-flex:mybatis-flex-spring:1.11.0")
implementation("com.mybatis-flex:mybatis-flex-spring:1.11.1")
}
```
@ -36,7 +36,7 @@ dependencies {
```groovy
dependencies {
implementation 'com.mybatis-flex:mybatis-flex-spring:1.11.0'
implementation 'com.mybatis-flex:mybatis-flex-spring:1.11.1'
}
```
@ -46,7 +46,7 @@ dependencies {
```kotlin
dependencies {
implementation("com.mybatis-flex:mybatis-flex-spring-boot-starter:1.11.0")
implementation("com.mybatis-flex:mybatis-flex-spring-boot-starter:1.11.1")
}
```
@ -54,7 +54,7 @@ dependencies {
```groovy
dependencies {
implementation 'com.mybatis-flex:mybatis-flex-spring-boot-starter:1.11.0'
implementation 'com.mybatis-flex:mybatis-flex-spring-boot-starter:1.11.1'
}
```
@ -65,7 +65,7 @@ dependencies {
```kotlin
dependencies {
implementation("com.mybatis-flex:mybatis-flex-solon-plugin:1.11.0")
implementation("com.mybatis-flex:mybatis-flex-solon-plugin:1.11.1")
}
```
@ -73,7 +73,7 @@ dependencies {
```groovy
dependencies {
implementation 'com.mybatis-flex:mybatis-flex-solon-plugin:1.11.0'
implementation 'com.mybatis-flex:mybatis-flex-solon-plugin:1.11.1'
}
```
@ -91,7 +91,7 @@ dependencies {
```kotlin
dependencies {
annotationProcessor("com.mybatis-flex:mybatis-flex-processor:1.11.0")
annotationProcessor("com.mybatis-flex:mybatis-flex-processor:1.11.1")
}
```
@ -99,6 +99,6 @@ dependencies {
```groovy
dependencies {
annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.11.0'
annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.11.1'
}
```

View File

@ -12,12 +12,12 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
<scope>provided</scope>
</dependency>
```
@ -28,12 +28,12 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
<scope>provided</scope>
</dependency>
``````
@ -44,12 +44,12 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
<scope>provided</scope>
</dependency>
```
@ -60,12 +60,12 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot3-starter</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
<scope>provided</scope>
</dependency>
```
@ -76,12 +76,12 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-solon-plugin</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
<scope>provided</scope>
</dependency>
```
@ -104,7 +104,7 @@
<path>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</path>
</annotationProcessorPaths>
</configuration>

View File

@ -248,7 +248,7 @@ pom.xml 添加 `annotationProcessorPaths` 配置,
```
dependencies {
...
annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.11.0'
annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.11.1'
}
```

View File

@ -21,7 +21,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-codegen</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</dependency>
```

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -24,7 +24,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
<exclusions>
<exclusion>
<groupId>com.mybatis-flex</groupId>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -102,7 +102,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-annotation</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
<scope>compile</scope>
</dependency>

View File

@ -27,7 +27,7 @@ public class FlexConsts {
}
public static final String NAME = "MyBatis-Flex";
public static final String VERSION = "1.11.0";
public static final String VERSION = "1.11.1";
public static final String SQL = "$$sql";

View File

@ -36,6 +36,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
/**
@ -61,13 +62,13 @@ public class FlexDataSource extends AbstractDataSource {
this(dataSourceKey, dataSource, DbTypeUtil.getDbType(dataSource), needDecryptDataSource);
}
public FlexDataSource(String dataSourceKey, DataSource dataSource, DbType dbType, boolean needDecryptDataSource){
public FlexDataSource(String dataSourceKey, DataSource dataSource, DbType dbType, boolean needDecryptDataSource) {
if (needDecryptDataSource) {
DataSourceManager.decryptDataSource(dataSource);
}
// 处理dbType
dbType = Optional.ofNullable(dbType).orElse(DbTypeUtil.getDbType(dataSource));
dbType = Optional.ofNullable(dbType).orElseGet(() -> DbTypeUtil.getDbType(dataSource));
this.defaultDataSourceKey = dataSourceKey;
this.defaultDataSource = dataSource;
@ -88,8 +89,7 @@ public class FlexDataSource extends AbstractDataSource {
}
// 优先取缓存否则根据数据源返回数据库类型
DbType dbType = Optional.ofNullable(dbTypeHashMap.get(dataSourceKey))
.orElse(DbTypeUtil.getDbType(ds));
DbType dbType = Optional.ofNullable(dbTypeHashMap.get(dataSourceKey)).orElseGet(() -> DbTypeUtil.getDbType(ds));
this.defaultDataSourceKey = dataSourceKey;
this.defaultDataSource = ds;
@ -104,13 +104,12 @@ public class FlexDataSource extends AbstractDataSource {
addDataSource(dataSourceKey, dataSource, DbTypeUtil.getDbType(dataSource), needDecryptDataSource);
}
public void addDataSource(String dataSourceKey, DataSource dataSource, DbType dbType,boolean needDecryptDataSource) {
public void addDataSource(String dataSourceKey, DataSource dataSource, DbType dbType, boolean needDecryptDataSource) {
if (needDecryptDataSource) {
DataSourceManager.decryptDataSource(dataSource);
}
dbType = Optional.ofNullable(dbTypeHashMap.get(dataSourceKey))
.orElse(DbTypeUtil.getDbType(dataSource));
dbType = Optional.ofNullable(dbType).orElseGet(() -> DbTypeUtil.getDbType(dataSource));
dataSourceMap.put(dataSourceKey, dataSource);
dbTypeHashMap.put(dataSourceKey, dbType);
@ -205,7 +204,7 @@ public class FlexDataSource extends AbstractDataSource {
} catch (SQLException e) {
if (log.isDebugEnabled()) {
log.debug("Error resetting autoCommit to true before closing the connection. " +
"Cause: " + e);
"Cause: " + e);
}
}
}
@ -242,6 +241,7 @@ public class FlexDataSource extends AbstractDataSource {
/**
* 获取数据源缺失处理器
*
* @return DataSourceMissingHandler 数据源缺失处理器实例用于自定义处理逻辑记录日志抛出异常或提供默认数据源
*/
public DataSourceMissingHandler getDataSourceMissingHandler() {

View File

@ -259,7 +259,7 @@ public class DynamicConditionTest {
@Test
public void testCastFunction1() {
QueryCondition condition = QueryCondition.create(new QueryColumn("id"), SqlOperator.IN, new Object[]{null});
Assert.assertSame(condition, getConditionCaster().apply(condition));
Assert.assertNotSame(condition, getConditionCaster().apply(condition));
}
@Test

View File

@ -6,7 +6,7 @@
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-dependencies</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
<packaging>pom</packaging>
@ -48,7 +48,7 @@
</developer>
</developers>
<properties>
<mybatis-flex.version>1.11.0</mybatis-flex.version>
<mybatis-flex.version>1.11.1</mybatis-flex.version>
</properties>
<dependencyManagement>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>com.mybatis-flex</groupId>
<artifactId>parent</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<name>mybatis-flex-loveqq-starter</name>

View File

@ -5,14 +5,14 @@
<parent>
<artifactId>parent</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>mybatis-flex-processor</name>
<artifactId>mybatis-flex-processor</artifactId>
<packaging>jar</packaging>
<version>1.11.0</version>
<version>1.11.1</version>
<properties>
@ -24,7 +24,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-annotation</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</dependency>
</dependencies>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -24,7 +24,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</dependency>
<dependency>
@ -105,7 +105,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</dependency>
</dependencies>

View File

@ -128,7 +128,8 @@ public class MultiDataSourceAutoConfiguration {
}
// 如果没有构建成功dbType需要自解析
dbType = Optional.ofNullable(dbType).orElse(DbTypeUtil.getDbType(dataSource));
final DataSource lambdaInnerDataSource = dataSource;
dbType = Optional.ofNullable(dbType).orElseGet(() -> DbTypeUtil.getDbType(lambdaInnerDataSource));
if (flexDataSource == null) {
flexDataSource = new FlexDataSource(entry.getKey(), dataSource, dbType, false);
} else {

View File

@ -6,7 +6,7 @@
<parent>
<groupId>com.mybatis-flex</groupId>
<artifactId>parent</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<name>mybatis-flex-spring-boot3-starter</name>
@ -21,7 +21,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -23,7 +23,7 @@
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>1.11.0</version>
<version>1.11.1</version>
</dependency>
<dependency>
@ -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>

View File

@ -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.");
}
}

View File

@ -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);
}
}
}
}
}

View File

@ -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());
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mybatis-flex-test</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -4,7 +4,7 @@
<parent>
<artifactId>mybatis-flex-test</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mybatis-flex-test</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mybatis-flex-test</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -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>-->

View File

@ -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;
}
}

View File

@ -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) {
}
}

View File

@ -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));
}
}

View File

@ -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;
}
}
}

View File

@ -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:

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mybatis-flex-test</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -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>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mybatis-flex-test</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>com.mybatis-flex</groupId>
<version>1.11.0</version>
<version>1.11.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

17
pom.xml
View File

@ -7,7 +7,7 @@
<groupId>com.mybatis-flex</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.11.0</version>
<version>1.11.1</version>
<name>mybatis-flex</name>
<url>https://mybatis-flex.com</url>
@ -65,7 +65,7 @@
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<mybatis-flex.version>1.11.0</mybatis-flex.version>
<mybatis-flex.version>1.11.1</mybatis-flex.version>
<mybatis.version>3.5.19</mybatis.version>
<mybatis-spring.version>2.1.2</mybatis-spring.version>
@ -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>