Compare commits

..

25 Commits

Author SHA1 Message Date
Michael Yang
d555a8e289
!561 mybatis-flex-solon-plugin:当有 mapperLocations 配置又没有相关注册时,改为 warn 日志(之前为异常)
Merge pull request !561 from 西东/main
2025-11-28 13:14:26 +00:00
noear
536f4f6058 mybatis-flex-solon-plugin:当有 mapperLocations 配置又没有相关注册时,改为 warn 日志(之前为异常) 2025-11-28 18:00:48 +08:00
noear
c80284ef68 mybatis-flex-solon-plugin:当有 mapperLocations 配置又没有相关注册时,改为 warn 日志(之前为异常) 2025-11-28 17:32:33 +08:00
Michael Yang
f02b8857d1 feat: mybatis-flex-spring-boot4-starter prepare 2025-11-26 16:59:37 +08:00
Michael Yang
0e83000168
Merge pull request #613 from Macrow/main
feat: add module mybatis-flex-spring-boot4-starter
2025-11-26 16:19:48 +08:00
Michael Yang
5814d254b8
Merge pull request #614 from AXBest/main
fix(sql):优化删除语句中单主键条件的拼接逻辑
2025-11-26 16:19:18 +08:00
Michael Yang
9895a79a5b
Merge pull request #615 from ruansheng8/feat-orderBy
QueryOrderBy 新增获取字段属性方法
2025-11-26 16:16:57 +08:00
ruansheng
9469a727a2 feat: -m core 风格统一 2025-11-22 11:57:07 +08:00
ruansheng
ce23df8b09 feat: -m query 新增OrderType获取方法 2025-11-21 17:28:46 +08:00
ruansheng
ed4f30cb2c feat: -m core 新增getQueryColumn方法 2025-11-21 17:13:52 +08:00
Macrow
69c80d251d chore: upgrade springboot4 to v4.0.0 2025-11-21 10:05:41 +08:00
Macrow
04ffb316bf chore: rollback config for maven-gpg-plugin 2025-11-20 12:36:16 +08:00
Macrow
f1ba1b2bb9 Merge branch 'springboot4' 2025-11-20 09:45:26 +08:00
Macrow
40f98c84d0 chore: add dependency mybatis-flex-spring-boot4-starter in module mybatis-flex-dependencies 2025-11-20 09:45:15 +08:00
axbest
af3451b43f fix(sql):优化删除语句中单主键条件的拼接逻辑
- 将单主键删除条件从 OR 拼接改为 IN 方式拼接
2025-11-19 16:13:04 +08:00
Michael Yang
7185d2c79b
Merge pull request #612 from Fengxq2014/fix_version
Update version
2025-11-18 18:32:13 +08:00
Macrow
9a83f1b695 feat: add module mybatis-flex-spring-boot4-starter 2025-11-17 16:17:04 +08:00
Macrow
a5f56db1d0 feat: add module mybatis-flex-spring-boot4-starter 2025-11-17 16:00:19 +08:00
DefNed
d770b550b2
Update version 2025-11-17 14:53:12 +08:00
Michael Yang
de26c7a668
Merge pull request #610 from cybzzz/main
fix: 带子查询的场景下,分页优化误删 join 的修复
2025-11-16 13:49:59 +08:00
Michael Yang
ad154da481
Merge pull request #611 from 4ucl/patch-1
fix: 代码生成器指定 EntityConfig#withBasePackage 时, 生成文件路径错误
2025-11-16 13:49:46 +08:00
Michael Yang
c17e747012
Merge pull request #609 from ruansheng8/feat-tx
refactor: -m flex 调整TransactionObject访问修饰符为public
2025-11-16 13:48:42 +08:00
Lucas C
b78ae2c6c5
Fix base entity package path replacement logic 2025-11-15 18:31:58 +08:00
cybzzz
9343c8eabd fix: 带子查询的场景下,分页优化误删 join 的修复 2025-11-15 13:24:36 +08:00
ruansheng
45ac06ff97 refactor: -m flex 调整TransactionObject访问修饰符为public 2025-11-14 14:14:00 +08:00
25 changed files with 2037 additions and 26 deletions

View File

@ -132,7 +132,7 @@ public class EntityGenerator implements IGenerator {
String sourceDir = StringUtil.hasText(entityConfig.getSourceDir()) ? entityConfig.getSourceDir() : packageConfig.getSourceDir(); String sourceDir = StringUtil.hasText(entityConfig.getSourceDir()) ? entityConfig.getSourceDir() : packageConfig.getSourceDir();
String baseEntityPackagePath = packageConfig.getEntityPackage().replace(".", "/"); String baseEntityPackagePath = packageConfig.getEntityPackage().replace(".", "/");
baseEntityPackagePath = StringUtil.hasText(entityConfig.getWithBasePackage()) ? entityConfig.getWithBasePackage().replace(".", "") baseEntityPackagePath = StringUtil.hasText(entityConfig.getWithBasePackage()) ? entityConfig.getWithBasePackage().replace(".", "/")
: baseEntityPackagePath + "/base"; : baseEntityPackagePath + "/base";
String baseEntityClassName = table.buildEntityClassName() + entityConfig.getWithBaseClassSuffix(); String baseEntityClassName = table.buildEntityClassName() + entityConfig.getWithBaseClassSuffix();

View File

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

View File

@ -231,12 +231,14 @@ public class ClickhouseDialectImpl extends CommonsDialectImpl {
} }
// 单主键 // 单主键
else { else {
sql.append(wrap(primaryKeys[0])).append(IN).append(BRACKET_LEFT);
for (int i = 0; i < ids.length; i++) { for (int i = 0; i < ids.length; i++) {
if (i > 0) { if (i > 0) {
sql.append(OR); sql.append(DELIMITER);
} }
sql.append(wrap(primaryKeys[0])).append(EQUALS_PLACEHOLDER); sql.append(PLACEHOLDER);
} }
sql.append(BRACKET_RIGHT);
} }
prepareAuth(schema, table, sql, OperateType.DELETE); prepareAuth(schema, table, sql, OperateType.DELETE);
return sql.toString(); return sql.toString();
@ -294,12 +296,14 @@ public class ClickhouseDialectImpl extends CommonsDialectImpl {
} }
// 单主键 // 单主键
else { else {
sql.append(wrap(primaryKeys[0])).append(IN).append(BRACKET_LEFT);
for (int i = 0; i < primaryValues.length; i++) { for (int i = 0; i < primaryValues.length; i++) {
if (i > 0) { if (i > 0) {
sql.append(OR); sql.append(DELIMITER);
} }
sql.append(wrap(primaryKeys[0])).append(EQUALS_PLACEHOLDER); sql.append(PLACEHOLDER);
} }
sql.append(BRACKET_RIGHT);
} }
sql.append(BRACKET_RIGHT).append(AND).append(buildLogicNormalCondition(logicDeleteColumn, tableInfo)); sql.append(BRACKET_RIGHT).append(AND).append(buildLogicNormalCondition(logicDeleteColumn, tableInfo));

View File

@ -47,6 +47,7 @@ import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import static com.mybatisflex.core.constant.SqlConsts.AND; import static com.mybatisflex.core.constant.SqlConsts.AND;
import static com.mybatisflex.core.constant.SqlConsts.IN;
import static com.mybatisflex.core.constant.SqlConsts.ASTERISK; import static com.mybatisflex.core.constant.SqlConsts.ASTERISK;
import static com.mybatisflex.core.constant.SqlConsts.BLANK; import static com.mybatisflex.core.constant.SqlConsts.BLANK;
import static com.mybatisflex.core.constant.SqlConsts.BRACKET_LEFT; import static com.mybatisflex.core.constant.SqlConsts.BRACKET_LEFT;
@ -242,12 +243,14 @@ public class CommonsDialectImpl implements IDialect {
} }
// 单主键 // 单主键
else { else {
sql.append(wrap(primaryKeys[0])).append(IN).append(BRACKET_LEFT);
for (int i = 0; i < ids.length; i++) { for (int i = 0; i < ids.length; i++) {
if (i > 0) { if (i > 0) {
sql.append(OR); sql.append(DELIMITER);
} }
sql.append(wrap(primaryKeys[0])).append(EQUALS_PLACEHOLDER); sql.append(PLACEHOLDER);
} }
sql.append(BRACKET_RIGHT);
} }
prepareAuth(schema, table, sql, OperateType.DELETE); prepareAuth(schema, table, sql, OperateType.DELETE);
return sql.toString(); return sql.toString();
@ -776,12 +779,14 @@ public class CommonsDialectImpl implements IDialect {
} }
// 单主键 // 单主键
else { else {
sql.append(wrap(primaryKeys[0])).append(IN).append(BRACKET_LEFT);
for (int i = 0; i < primaryValues.length; i++) { for (int i = 0; i < primaryValues.length; i++) {
if (i > 0) { if (i > 0) {
sql.append(OR); sql.append(DELIMITER);
} }
sql.append(wrap(primaryKeys[0])).append(EQUALS_PLACEHOLDER); sql.append(PLACEHOLDER);
} }
sql.append(BRACKET_RIGHT);
} }
sql.append(BRACKET_RIGHT).append(AND).append(buildLogicNormalCondition(logicDeleteColumn, tableInfo)); sql.append(BRACKET_RIGHT).append(AND).append(buildLogicNormalCondition(logicDeleteColumn, tableInfo));
@ -1025,12 +1030,14 @@ public class CommonsDialectImpl implements IDialect {
} }
// 单主键 // 单主键
else { else {
sql.append(wrap(primaryKeys[0])).append(IN).append(BRACKET_LEFT);
for (int i = 0; i < primaryValues.length; i++) { for (int i = 0; i < primaryValues.length; i++) {
if (i > 0) { if (i > 0) {
sql.append(OR); sql.append(DELIMITER);
} }
sql.append(wrap(primaryKeys[0])).append(EQUALS_PLACEHOLDER); sql.append(PLACEHOLDER);
} }
sql.append(BRACKET_RIGHT);
} }
if (StringUtil.hasText(logicDeleteColumn) || ArrayUtil.isNotEmpty(tenantIdArgs)) { if (StringUtil.hasText(logicDeleteColumn) || ArrayUtil.isNotEmpty(tenantIdArgs)) {

View File

@ -76,7 +76,8 @@ public class OperatorSelectCondition extends QueryCondition {
@Override @Override
boolean containsTable(String... tables) { boolean containsTable(String... tables) {
QueryCondition condition = queryWrapper.getWhereQueryCondition(); QueryCondition condition = queryWrapper.getWhereQueryCondition();
return condition != null && condition.containsTable(tables); boolean subContains = condition != null && condition.containsTable(tables);
return subContains || nextContainsTable(tables);
} }
@Override @Override

View File

@ -66,6 +66,13 @@ public class QueryOrderBy implements CloneSupport<QueryOrderBy> {
return this; return this;
} }
public QueryColumn getQueryColumn() {
return this.queryColumn;
}
public String getOrderType() {
return this.orderType;
}
public String toSql(List<QueryTable> queryTables, IDialect dialect) { public String toSql(List<QueryTable> queryTables, IDialect dialect) {
String sql = queryColumn.toConditionSql(queryTables, dialect) + orderType; String sql = queryColumn.toConditionSql(queryTables, dialect) + orderType;

View File

@ -97,6 +97,11 @@
<artifactId>mybatis-flex-spring-boot3-starter</artifactId> <artifactId>mybatis-flex-spring-boot3-starter</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot4-starter</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

View File

@ -91,7 +91,7 @@ public class FlexTransactionManager extends AbstractPlatformTransactionManager {
TimeoutHolder.clear(); TimeoutHolder.clear();
} }
static class TransactionObject extends JdbcTransactionObjectSupport { public static class TransactionObject extends JdbcTransactionObjectSupport {
private static final ThreadLocal<String> ROLLBACK_ONLY_XIDS = new ThreadLocal<>(); private static final ThreadLocal<String> ROLLBACK_ONLY_XIDS = new ThreadLocal<>();

View File

@ -25,10 +25,11 @@ import org.noear.solon.annotation.Inject;
import org.noear.solon.core.AppContext; import org.noear.solon.core.AppContext;
import org.noear.solon.core.event.EventBus; import org.noear.solon.core.event.EventBus;
import org.noear.solon.core.util.ResourceUtil; import org.noear.solon.core.util.ResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Proxy;
/** /**
* Mybatis-Flex 自动装配 * Mybatis-Flex 自动装配
@ -37,6 +38,8 @@ import java.lang.reflect.Proxy;
*/ */
@Configuration @Configuration
public class MybatisFlexAutoConfiguration { public class MybatisFlexAutoConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(MybatisFlexAutoConfiguration.class);
private DataSource getDataSource() { private DataSource getDataSource() {
return MybatisFlexBootstrap.getInstance().getDataSource(); return MybatisFlexBootstrap.getInstance().getDataSource();
} }
@ -159,7 +162,9 @@ public class MybatisFlexAutoConfiguration {
//如果有配置但是没有 mapper 注册成功说明有问题了 //如果有配置但是没有 mapper 注册成功说明有问题了
if (flexConfiguration.getMapperRegistry().getMappers().size() == 0) { if (flexConfiguration.getMapperRegistry().getMappers().size() == 0) {
throw new IllegalStateException("Missing mapper registration, please check the 'mapperLocations' configuration!"); if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Property 'mapperLocations' was specified but matching resources are not found.");
}
} }
} }

View File

@ -39,7 +39,13 @@ import org.springframework.transaction.annotation.TransactionManagementConfigure
* *
* @author michael * @author michael
*/ */
@ConditionalOnClass(Db.class) @ConditionalOnClass(
value = Db.class,
name = {
"org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration",
"org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration",
}
)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@ConditionalOnMissingBean(TransactionManager.class) @ConditionalOnMissingBean(TransactionManager.class)
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

View File

@ -54,7 +54,13 @@ import java.util.Optional;
@ConditionalOnMybatisFlexDatasource() @ConditionalOnMybatisFlexDatasource()
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(MybatisFlexProperties.class) @EnableConfigurationProperties(MybatisFlexProperties.class)
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) @ConditionalOnClass(
value = {SqlSessionFactory.class, SqlSessionFactoryBean.class},
name = {
"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration",
"org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration",
}
)
@AutoConfigureBefore(value = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class} @AutoConfigureBefore(value = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class}
, name = {"com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure", , name = {"com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure",
"com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure"}) "com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure"})

View File

@ -96,7 +96,12 @@ import java.util.stream.Stream;
* @author 王帅 * @author 王帅
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) @ConditionalOnClass(
value = {SqlSessionFactory.class, SqlSessionFactoryBean.class},
name = {
"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration",
}
)
@ConditionalOnSingleCandidate(DataSource.class) @ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisFlexProperties.class) @EnableConfigurationProperties(MybatisFlexProperties.class)
@AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class}) @AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class})

View File

@ -34,7 +34,7 @@
<dependency> <dependency>
<groupId>org.mybatis</groupId> <groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId> <artifactId>mybatis-spring</artifactId>
<version>3.0.4</version> <version>3.0.5</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,137 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mybatis-flex</groupId>
<artifactId>parent</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>mybatis-flex-spring-boot4-starter</name>
<artifactId>mybatis-flex-spring-boot4-starter</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot4.version>4.0.0</spring-boot4.version>
</properties>
<dependencies>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot4.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<version>${spring-boot4.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot4.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-jdbc</artifactId>
<version>${spring-boot4.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.scripting</groupId>
<artifactId>mybatis-freemarker</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.scripting</groupId>
<artifactId>mybatis-velocity</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.scripting</groupId>
<artifactId>mybatis-thymeleaf</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.18</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.chris2018998</groupId>
<artifactId>beecp</artifactId>
<version>3.4.1</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-rm-datasource</artifactId>
<version>1.7.0</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.5</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,74 @@
/*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.spring.boot.v4;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.spring.FlexTransactionManager;
import org.jspecify.annotations.NonNull;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.jdbc.autoconfigure.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.transaction.autoconfigure.TransactionAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.core.Ordered;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
/**
* MyBatis-Flex 事务自动配置
*
* @author michael
*/
@ConditionalOnClass(
value = Db.class,
name = {
"org.springframework.boot.transaction.autoconfigure.TransactionAutoConfiguration",
"org.springframework.boot.jdbc.autoconfigure.DataSourceTransactionManagerAutoConfiguration",
}
)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@ConditionalOnMissingBean(TransactionManager.class)
@Configuration(proxyBeanMethods = false)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@AutoConfigureAfter({MybatisFlexAutoConfiguration.class})
@AutoConfigureBefore(value = {TransactionAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
public class FlexTransactionAutoConfiguration implements TransactionManagementConfigurer {
/**
* 这里使用 final 修饰属性是因为<br>
* <p>
* 1调用 {@link #annotationDrivenTransactionManager} 方法会返回 TransactionManager 对象<br>
* 2{@code @Bean} 注入又会返回 TransactionManager 对象<br>
* <p>
* 需要保证两个对象的一致性
*/
private final FlexTransactionManager flexTransactionManager = new FlexTransactionManager();
@NonNull
@Override
@Bean(name = "transactionManager")
public PlatformTransactionManager annotationDrivenTransactionManager() {
return flexTransactionManager;
}
}

View File

@ -0,0 +1,159 @@
/*
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.spring.boot.v4;
import com.mybatisflex.core.datasource.DataSourceBuilder;
import com.mybatisflex.core.datasource.DataSourceDecipher;
import com.mybatisflex.core.datasource.DataSourceManager;
import com.mybatisflex.core.datasource.FlexDataSource;
import com.mybatisflex.core.dialect.DbType;
import com.mybatisflex.core.dialect.DbTypeUtil;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.util.MapUtil;
import com.mybatisflex.spring.boot.ConditionalOnMybatisFlexDatasource;
import com.mybatisflex.spring.boot.v4.MybatisFlexProperties.SeataConfig;
import com.mybatisflex.spring.datasource.DataSourceAdvice;
import io.seata.rm.datasource.DataSourceProxy;
import io.seata.rm.datasource.xa.DataSourceProxyXA;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.autoconfigure.DataSourceAutoConfiguration;
import org.springframework.boot.jdbc.autoconfigure.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import javax.sql.DataSource;
import java.util.Map;
import java.util.Optional;
/**
* MyBatis-Flex 多数据源的配置支持
*
* @author michael
* @author 王帅
*/
@ConditionalOnMybatisFlexDatasource()
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(MybatisFlexProperties.class)
@ConditionalOnClass(
value = {SqlSessionFactory.class, SqlSessionFactoryBean.class},
name = {
"org.springframework.boot.jdbc.autoconfigure.DataSourceAutoConfiguration",
"org.springframework.boot.jdbc.autoconfigure.DataSourceTransactionManagerAutoConfiguration",
}
)
@AutoConfigureBefore(value = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class}
, name = {"com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure",
"com.alibaba.druid.spring.boot4.autoconfigure.DruidDataSourceAutoConfigure"})
public class MultiDataSourceAutoConfiguration {
private final String master;
private final Map<String, Map<String, String>> dataSourceProperties;
private final SeataConfig seataConfig;
// 数据源解密器
protected final DataSourceDecipher dataSourceDecipher;
public MultiDataSourceAutoConfiguration(MybatisFlexProperties properties
, ObjectProvider<DataSourceDecipher> dataSourceDecipherProvider
) {
dataSourceProperties = properties.getDatasource();
dataSourceDecipher = dataSourceDecipherProvider.getIfAvailable();
seataConfig = properties.getSeataConfig();
master = properties.getDefaultDatasourceKey();
}
@Bean
@ConditionalOnMissingBean
public DataSource dataSource() {
FlexDataSource flexDataSource = null;
if (dataSourceProperties != null && !dataSourceProperties.isEmpty()) {
if (dataSourceDecipher != null) {
DataSourceManager.setDecipher(dataSourceDecipher);
}
if (master != null) {
Map<String, String> map = dataSourceProperties.remove(master);
if (map != null) {
// 这里创建master时flexDataSource一定是null
flexDataSource = addDataSource(MapUtil.entry(master, map), null);
} else {
throw FlexExceptions.wrap("没有找到默认数据源 \"%s\" 对应的配置,请检查您的多数据源配置。", master);
}
}
for (Map.Entry<String, Map<String, String>> entry : dataSourceProperties.entrySet()) {
flexDataSource = addDataSource(entry, flexDataSource);
}
}
return flexDataSource;
}
private FlexDataSource addDataSource(Map.Entry<String, Map<String, String>> entry, FlexDataSource flexDataSource) {
DataSource dataSource = new DataSourceBuilder(entry.getValue()).build();
DataSourceManager.decryptDataSource(dataSource);
// 数据库类型
DbType dbType = null;
if (seataConfig != null && seataConfig.isEnable()) {
if (seataConfig.getSeataMode() == MybatisFlexProperties.SeataMode.XA) {
DataSourceProxyXA sourceProxyXa = new DataSourceProxyXA(dataSource);
dbType = DbType.findByName(sourceProxyXa.getDbType());
dataSource = sourceProxyXa;
} else {
DataSourceProxy dataSourceProxy = new DataSourceProxy(dataSource);
dbType = DbType.findByName(dataSourceProxy.getDbType());
dataSource = dataSourceProxy;
}
}
// 如果没有构建成功dbType需要自解析
final DataSource lambdaInnerDataSource = dataSource;
dbType = Optional.ofNullable(dbType).orElseGet(() -> DbTypeUtil.getDbType(lambdaInnerDataSource));
if (flexDataSource == null) {
flexDataSource = new FlexDataSource(entry.getKey(), dataSource, dbType, false);
} else {
flexDataSource.addDataSource(entry.getKey(), dataSource, dbType, false);
}
return flexDataSource;
}
/**
* {@link com.mybatisflex.annotation.UseDataSource} 注解切换数据源切面
*/
@Bean
@ConditionalOnMissingBean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public DataSourceAdvice dataSourceAdvice() {
return new DataSourceAdvice();
}
}

View File

@ -0,0 +1,428 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.spring.boot.v4;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.datasource.DataSourceDecipher;
import com.mybatisflex.core.datasource.DataSourceManager;
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
import com.mybatisflex.core.logicdelete.LogicDeleteProcessor;
import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.core.table.DynamicSchemaProcessor;
import com.mybatisflex.core.table.DynamicTableProcessor;
import com.mybatisflex.core.table.TableManager;
import com.mybatisflex.core.tenant.TenantFactory;
import com.mybatisflex.core.tenant.TenantManager;
import com.mybatisflex.spring.FlexSqlSessionFactoryBean;
import com.mybatisflex.spring.boot.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.TypeHandler;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.autoconfigure.DataSourceAutoConfiguration;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;
import java.beans.PropertyDescriptor;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Mybatis-Flex 的核心配置
* <p>
* 参考 <a href="https://github.com/mybatis/spring-boot-starter/blob/master/mybatis-spring-boot-autoconfigure/src/main/java/org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.java">
* MybatisAutoConfiguration.java</a>
* <p>
* Mybatis-Flex 开启自动配置功能主要修改以下几个方面:
* <p>
* 1替换配置为 mybatis-flex 的配置前缀<br>
* 2修改 SqlSessionFactory FlexSqlSessionFactoryBean<br>
* 3修改 Configuration FlexConfiguration<br>
*
* @author Eddú Meléndez
* @author Josh Long
* @author Kazuki Shimizu
* @author Eduardo Macarrón
* @author michael
* @author 王帅
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(
value = {SqlSessionFactory.class, SqlSessionFactoryBean.class},
name = {
"org.springframework.boot.jdbc.autoconfigure.DataSourceAutoConfiguration",
}
)
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisFlexProperties.class)
@AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class})
public class MybatisFlexAutoConfiguration implements InitializingBean {
protected static final Logger logger = LoggerFactory.getLogger(MybatisFlexAutoConfiguration.class);
protected final MybatisFlexProperties properties;
protected final Interceptor[] interceptors;
protected final TypeHandler[] typeHandlers;
protected final LanguageDriver[] languageDrivers;
protected final ResourceLoader resourceLoader;
protected final DatabaseIdProvider databaseIdProvider;
protected final List<ConfigurationCustomizer> configurationCustomizers;
protected final List<SqlSessionFactoryBeanCustomizer> sqlSessionFactoryBeanCustomizers;
//数据源解密器
protected final DataSourceDecipher dataSourceDecipher;
//动态表名
protected final DynamicTableProcessor dynamicTableProcessor;
//动态 schema 处理器
protected final DynamicSchemaProcessor dynamicSchemaProcessor;
//多租户
protected final TenantFactory tenantFactory;
//自定义逻辑删除处理器
protected final LogicDeleteProcessor logicDeleteProcessor;
//初始化监听
protected final List<MyBatisFlexCustomizer> mybatisFlexCustomizers;
public MybatisFlexAutoConfiguration(MybatisFlexProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider,
ObjectProvider<TypeHandler[]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider,
ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider,
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider,
ObjectProvider<List<SqlSessionFactoryBeanCustomizer>> sqlSessionFactoryBeanCustomizers,
ObjectProvider<DataSourceDecipher> dataSourceDecipherProvider,
ObjectProvider<DynamicTableProcessor> dynamicTableProcessorProvider,
ObjectProvider<DynamicSchemaProcessor> dynamicSchemaProcessorProvider,
ObjectProvider<TenantFactory> tenantFactoryProvider,
ObjectProvider<LogicDeleteProcessor> logicDeleteProcessorProvider,
ObjectProvider<MyBatisFlexCustomizer> mybatisFlexCustomizerProviders
) {
this.properties = properties;
this.interceptors = interceptorsProvider.getIfAvailable();
this.typeHandlers = typeHandlersProvider.getIfAvailable();
this.languageDrivers = languageDriversProvider.getIfAvailable();
this.resourceLoader = resourceLoader;
this.databaseIdProvider = databaseIdProvider.getIfAvailable();
this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
this.sqlSessionFactoryBeanCustomizers = sqlSessionFactoryBeanCustomizers.getIfAvailable();
//数据源解密器
this.dataSourceDecipher = dataSourceDecipherProvider.getIfAvailable();
//动态表名
this.dynamicTableProcessor = dynamicTableProcessorProvider.getIfAvailable();
//动态 schema 处理器
this.dynamicSchemaProcessor = dynamicSchemaProcessorProvider.getIfAvailable();
//多租户
this.tenantFactory = tenantFactoryProvider.getIfAvailable();
//逻辑删除处理器
this.logicDeleteProcessor = logicDeleteProcessorProvider.getIfAvailable();
//初始化监听器
this.mybatisFlexCustomizers = mybatisFlexCustomizerProviders.orderedStream().collect(Collectors.toList());
}
@Override
public void afterPropertiesSet() {
// 检测 MyBatis 原生配置文件是否存在
checkConfigFileExists();
// 添加 MyBatis-Flex 全局配置
if (properties.getGlobalConfig() != null) {
properties.getGlobalConfig().applyTo(FlexGlobalConfig.getDefaultConfig());
}
//数据源解密器
if (dataSourceDecipher != null) {
DataSourceManager.setDecipher(dataSourceDecipher);
}
// 动态表名配置
if (dynamicTableProcessor != null) {
TableManager.setDynamicTableProcessor(dynamicTableProcessor);
}
// 动态 schema 处理器配置
if (dynamicSchemaProcessor != null) {
TableManager.setDynamicSchemaProcessor(dynamicSchemaProcessor);
}
//多租户
if (tenantFactory != null) {
TenantManager.setTenantFactory(tenantFactory);
}
//逻辑删除处理器
if (logicDeleteProcessor != null) {
LogicDeleteManager.setProcessor(logicDeleteProcessor);
}
//初始化监听器
if (mybatisFlexCustomizers != null) {
mybatisFlexCustomizers.forEach(myBatisFlexCustomizer -> myBatisFlexCustomizer.customize(FlexGlobalConfig.getDefaultConfig()));
}
}
private void checkConfigFileExists() {
if (this.properties.isCheckConfigLocation() && StringUtils.hasText(this.properties.getConfigLocation())) {
Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation());
Assert.state(resource.exists(),
"Cannot find config location: " + resource + " (please add config file or check your Mybatis configuration)");
}
}
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new FlexSqlSessionFactoryBean();
factory.setDataSource(dataSource);
if (properties.getConfiguration() == null || properties.getConfiguration().getVfsImpl() == null) {
factory.setVfs(SpringBootVFS.class);
}
if (StringUtils.hasText(this.properties.getConfigLocation())) {
factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
}
applyConfiguration(factory);
if (this.properties.getConfigurationProperties() != null) {
factory.setConfigurationProperties(this.properties.getConfigurationProperties());
}
if (!ObjectUtils.isEmpty(this.interceptors)) {
factory.setPlugins(this.interceptors);
}
if (this.databaseIdProvider != null) {
factory.setDatabaseIdProvider(this.databaseIdProvider);
}
if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
}
if (this.properties.getTypeAliasesSuperType() != null) {
factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
}
if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
}
if (!ObjectUtils.isEmpty(this.typeHandlers)) {
factory.setTypeHandlers(this.typeHandlers);
}
Resource[] mapperLocations = this.properties.resolveMapperLocations();
if (!ObjectUtils.isEmpty(mapperLocations)) {
factory.setMapperLocations(mapperLocations);
}
Set<String> factoryPropertyNames = Stream
.of(new BeanWrapperImpl(SqlSessionFactoryBean.class).getPropertyDescriptors()).map(PropertyDescriptor::getName)
.collect(Collectors.toSet());
Class<? extends LanguageDriver> defaultLanguageDriver = this.properties.getDefaultScriptingLanguageDriver();
if (factoryPropertyNames.contains("scriptingLanguageDrivers") && !ObjectUtils.isEmpty(this.languageDrivers)) {
// Need to mybatis-spring 2.0.2+
factory.setScriptingLanguageDrivers(this.languageDrivers);
if (defaultLanguageDriver == null && this.languageDrivers.length == 1) {
defaultLanguageDriver = this.languageDrivers[0].getClass();
}
}
if (factoryPropertyNames.contains("defaultScriptingLanguageDriver")) {
// Need to mybatis-spring 2.0.2+
factory.setDefaultScriptingLanguageDriver(defaultLanguageDriver);
}
applySqlSessionFactoryBeanCustomizers(factory);
return factory.getObject();
}
protected void applyConfiguration(SqlSessionFactoryBean factory) {
MybatisFlexProperties.CoreConfiguration coreConfiguration = this.properties.getConfiguration();
FlexConfiguration configuration = null;
if (coreConfiguration != null || !StringUtils.hasText(this.properties.getConfigLocation())) {
configuration = new FlexConfiguration();
}
if (configuration != null && coreConfiguration != null) {
coreConfiguration.applyTo(configuration);
}
if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
customizer.customize(configuration);
}
}
factory.setConfiguration(configuration);
}
protected void applySqlSessionFactoryBeanCustomizers(SqlSessionFactoryBean factory) {
if (!CollectionUtils.isEmpty(this.sqlSessionFactoryBeanCustomizers)) {
for (SqlSessionFactoryBeanCustomizer customizer : this.sqlSessionFactoryBeanCustomizers) {
customizer.customize(factory);
}
}
}
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
ExecutorType executorType = this.properties.getExecutorType();
if (executorType != null) {
return new SqlSessionTemplate(sqlSessionFactory, executorType);
} else {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
/**
* This will just scan the same base package as Spring Boot does. If you want more power, you can explicitly use
* {@link org.mybatis.spring.annotation.MapperScan} but this will get typed mappers working correctly, out-of-the-box,
* similar to using Spring Data JPA repositories.
*/
public static class AutoConfiguredMapperScannerRegistrar
implements BeanFactoryAware, EnvironmentAware, ImportBeanDefinitionRegistrar {
private BeanFactory beanFactory;
private Environment environment;
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
if (!AutoConfigurationPackages.has(this.beanFactory)) {
logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.");
return;
}
logger.debug("Searching for mappers annotated with @Mapper");
List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
if (logger.isDebugEnabled()) {
packages.forEach(pkg -> logger.debug("Using auto-configuration base package '{}'", pkg));
}
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
builder.addPropertyValue("processPropertyPlaceHolders", true);
builder.addPropertyValue("annotationClass", Mapper.class);
builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(packages));
BeanWrapper beanWrapper = new BeanWrapperImpl(MapperScannerConfigurer.class);
Set<String> propertyNames = Stream.of(beanWrapper.getPropertyDescriptors()).map(PropertyDescriptor::getName)
.collect(Collectors.toSet());
if (propertyNames.contains("lazyInitialization")) {
// Need to mybatis-spring 2.0.2+
builder.addPropertyValue("lazyInitialization", "${mybatis.lazy-initialization:false}");
}
if (propertyNames.contains("defaultScope")) {
// Need to mybatis-spring 2.0.6+
builder.addPropertyValue("defaultScope", "${mybatis.mapper-default-scope:}");
}
// for spring-native
boolean injectSqlSession = environment.getProperty("mybatis.inject-sql-session-on-mapper-scan", Boolean.class,
Boolean.TRUE);
if (injectSqlSession && this.beanFactory instanceof ListableBeanFactory) {
ListableBeanFactory listableBeanFactory = (ListableBeanFactory) this.beanFactory;
Optional<String> sqlSessionTemplateBeanName = Optional
.ofNullable(getBeanNameForType(SqlSessionTemplate.class, listableBeanFactory));
Optional<String> sqlSessionFactoryBeanName = Optional
.ofNullable(getBeanNameForType(SqlSessionFactory.class, listableBeanFactory));
if (sqlSessionTemplateBeanName.isPresent() || !sqlSessionFactoryBeanName.isPresent()) {
builder.addPropertyValue("sqlSessionTemplateBeanName",
sqlSessionTemplateBeanName.orElse("sqlSessionTemplate"));
} else {
builder.addPropertyValue("sqlSessionFactoryBeanName", sqlSessionFactoryBeanName.get());
}
}
builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition(MapperScannerConfigurer.class.getName(), builder.getBeanDefinition());
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
private String getBeanNameForType(Class<?> type, ListableBeanFactory factory) {
String[] beanNames = factory.getBeanNamesForType(type);
return beanNames.length > 0 ? beanNames[0] : null;
}
}
/**
* If mapper registering configuration or mapper scanning configuration not present, this configuration allow to scan
* mappers based on the same component-scanning path as Spring Boot itself.
*/
@org.springframework.context.annotation.Configuration(proxyBeanMethods = false)
@Import(AutoConfiguredMapperScannerRegistrar.class)
@ConditionalOnMissingBean({MapperFactoryBean.class, MapperScannerConfigurer.class})
public static class MapperScannerRegistrarNotFoundConfiguration implements InitializingBean {
@Override
public void afterPropertiesSet() {
logger.debug(
"Not found configuration for registering mapper bean using @MapperScan, MapperFactoryBean and MapperScannerConfigurer.");
}
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* MyBatis-Flex Spring Boot 支持
*/
package com.mybatisflex.spring.boot.v4;

View File

@ -0,0 +1,91 @@
{
"properties": [
{
"defaultValue": false,
"name": "mybatis-flex.lazy-initialization",
"description": "Set whether enable lazy initialization for mapper bean.",
"type": "java.lang.Boolean"
},
{
"defaultValue": "",
"name": "mybatis-flex.mapper-default-scope",
"description": "A default scope for mapper bean that scanned by auto-configure.",
"type": "java.lang.String"
},
{
"defaultValue": true,
"name": "mybatis-flex.inject-sql-session-on-mapper-scan",
"description": "Set whether inject a SqlSessionTemplate or SqlSessionFactory bean (If you want to back to the behavior of 2.2.1 or before, specify false). If you use together with spring-native, should be set true.",
"type": "java.lang.Boolean"
},
{
"name": "mybatis-flex.scripting-language-driver.velocity.userdirective",
"deprecation": {
"level": "error",
"reason": "The 'userdirective' is deprecated since Velocity 2.x. This property defined for keeping backward compatibility with older velocity version.",
"replacement": "mybatis-flex.scripting-language-driver.velocity.velocity-settings.runtime.custom_directives"
}
},
{
"defaultValue": true,
"name": "mybatis-flex.datasource",
"description": "多数据源配置",
"type": "java.util.Map<java.lang.String,com.mybatisflex.spring.boot.DataSourceProperty>"
},
{
"defaultValue": 0,
"name": "mybatis-flex.global-config.normal-value-of-logic-delete",
"description": "逻辑删除未删除值标记",
"type": "java.lang.Object"
},
{
"defaultValue": 1,
"name": "mybatis-flex.global-config.deleted-value-of-logic-delete",
"description": "逻辑删除已删除值标记",
"type": "java.lang.Object"
},
{
"defaultValue": 10,
"name": "mybatis-flex.global-config.default-page-size",
"description": "默认的分页查询时的每页数据量",
"type": "java.lang.Integer"
},
{
"defaultValue": 2,
"name": "mybatis-flex.global-config.default-relation-query-depth",
"description": "默认的 Relation 注解查询深度",
"type": "java.lang.Integer"
},
{
"name": "mybatis-flex.global-config.key-config.value",
"description": "使用的 ID 生成器名称 或者 Sequence 执行的 SQL 内容",
"type": "java.lang.String"
},
{
"defaultValue": true,
"name": "mybatis-flex.global-config.key-config.before",
"description": "是否在数据插入之前执行,只在非自增上配置有效",
"type": "java.lang.Boolean"
},
{
"name": "mybatis-flex.global-config.key-config.key-type",
"description": "ID 生成策略",
"type": "com.mybatisflex.annotation.KeyType"
},
{
"name": "mybatis-flex.global-config.logic-delete-column",
"description": "全局默认逻辑删除字段",
"type": "java.lang.String"
},
{
"name": "mybatis-flex.global-config.tenant-column",
"description": "全局默认多租户字段",
"type": "java.lang.String"
},
{
"name": "mybatis-flex.global-config.version-column",
"description": "全局默认逻辑乐观锁字段",
"type": "java.lang.String"
}
]
}

View File

@ -0,0 +1,9 @@
# Depends On Database Initialization Detectors
org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector=\
com.mybatisflex.spring.boot.MybatisFlexDependsOnDatabaseInitializationDetector
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mybatisflex.spring.boot.v4.FlexTransactionAutoConfiguration,\
com.mybatisflex.spring.boot.v4.MultiDataSourceAutoConfiguration,\
com.mybatisflex.spring.boot.v4.MybatisFlexAutoConfiguration,\
com.mybatisflex.spring.boot.MybatisFlexAdminAutoConfiguration,\
com.mybatisflex.spring.boot.MybatisLanguageDriverAutoConfiguration

View File

@ -0,0 +1,5 @@
com.mybatisflex.spring.boot.v4.FlexTransactionAutoConfiguration
com.mybatisflex.spring.boot.v4.MultiDataSourceAutoConfiguration
com.mybatisflex.spring.boot.v4.MybatisFlexAutoConfiguration
com.mybatisflex.spring.boot.MybatisFlexAdminAutoConfiguration
com.mybatisflex.spring.boot.MybatisLanguageDriverAutoConfiguration

View File

@ -91,7 +91,7 @@ public class FlexTransactionManager extends AbstractPlatformTransactionManager {
TimeoutHolder.clear(); TimeoutHolder.clear();
} }
static class TransactionObject extends JdbcTransactionObjectSupport { public static class TransactionObject extends JdbcTransactionObjectSupport {
private static final ThreadLocal<String> ROLLBACK_ONLY_XIDS = new ThreadLocal<>(); private static final ThreadLocal<String> ROLLBACK_ONLY_XIDS = new ThreadLocal<>();

View File

@ -31,11 +31,7 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static com.mybatisflex.core.query.QueryMethods.case_; import static com.mybatisflex.core.query.QueryMethods.*;
import static com.mybatisflex.core.query.QueryMethods.column;
import static com.mybatisflex.core.query.QueryMethods.count;
import static com.mybatisflex.core.query.QueryMethods.distinct;
import static com.mybatisflex.core.query.QueryMethods.select;
import static com.mybatisflex.test.model.table.RoleTableDef.ROLE; import static com.mybatisflex.test.model.table.RoleTableDef.ROLE;
import static com.mybatisflex.test.model.table.UserRoleTableDef.USER_ROLE; import static com.mybatisflex.test.model.table.UserRoleTableDef.USER_ROLE;
import static com.mybatisflex.test.model.table.UserTableDef.USER; import static com.mybatisflex.test.model.table.UserTableDef.USER;
@ -199,4 +195,26 @@ class QueryWrapperTest {
" AND ` user_name ` = ''", sql); " AND ` user_name ` = ''", sql);
} }
@Test
void test07() {
QueryWrapper queryWrapper = QueryWrapper.create()
.select()
.from(USER.as("u"))
.leftJoin(USER_ROLE).as("ur").on(USER.USER_ID.eq(USER_ROLE.USER_ID))
.where(QueryCondition.createEmpty())
.and(USER.USER_ID.eq(1).or(USER.USER_ID.in(
QueryWrapper.create().select(USER_ROLE.USER_ID).from(USER_ROLE)))
)
.and(
exists(selectOne().from(ROLE)
.where(ROLE.ROLE_ID.eq(USER_ROLE.ROLE_ID)))
)
.and(USER_ROLE.USER_ID.eq(1));
System.out.println(queryWrapper.toSQL());
QueryCondition whereQueryCondition = CPI.getWhereQueryCondition(queryWrapper);
boolean contained = CPI.containsTable(whereQueryCondition, "tb_user_role");
Assertions.assertTrue(contained);
}
} }

View File

@ -55,6 +55,7 @@
<module>mybatis-flex-spring</module> <module>mybatis-flex-spring</module>
<module>mybatis-flex-spring-boot-starter</module> <module>mybatis-flex-spring-boot-starter</module>
<module>mybatis-flex-spring-boot3-starter</module> <module>mybatis-flex-spring-boot3-starter</module>
<module>mybatis-flex-spring-boot4-starter</module>
<module>mybatis-flex-solon-plugin</module> <module>mybatis-flex-solon-plugin</module>
<module>mybatis-flex-loveqq-starter</module> <module>mybatis-flex-loveqq-starter</module>
<module>mybatis-flex-test</module> <module>mybatis-flex-test</module>
@ -86,6 +87,7 @@
<spring.version>5.3.27</spring.version> <spring.version>5.3.27</spring.version>
<spring-batch.version>4.3.10</spring-batch.version> <spring-batch.version>4.3.10</spring-batch.version>
<spring-boot.version>2.7.11</spring-boot.version> <spring-boot.version>2.7.11</spring-boot.version>
<spring-boot4.version>4.0.0</spring-boot4.version>
<solon.version>3.0.1</solon.version> <solon.version>3.0.1</solon.version>
<loveqq.version>1.1.0-java8</loveqq.version> <loveqq.version>1.1.0-java8</loveqq.version>