mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
rename TableInfos to TableInfoFactory
This commit is contained in:
parent
f0fba210aa
commit
53b3bc1010
@ -50,6 +50,7 @@ export default defineConfig({
|
||||
{ text: '数据填充', link: '/zh/fill' },
|
||||
{ text: '数据脱敏', link: '/zh/mask' },
|
||||
{ text: 'SQL 审计', link: '/zh/audit' },
|
||||
{ text: '多数据源', link: '/zh/multi-datasource' },
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
24
docs/zh/multi-datasource.md
Normal file
24
docs/zh/multi-datasource.md
Normal file
@ -0,0 +1,24 @@
|
||||
# 多数据源
|
||||
|
||||
MyBaits-Flex 内置了功能完善的多数据源支持<Badge type="tip" text="^1.0.6" />,不需要借助第三方插件或者依赖,开箱即用,
|
||||
支持包括 `druid`、`hikaricp`、`dbcp2`、`beecp` 在内的任何数据源,MyBatis-Flex 多数据源配置如下:
|
||||
|
||||
```yaml
|
||||
mybatis-flex:
|
||||
datasource:
|
||||
ds1:
|
||||
url: jdbc:mysql://127.0.0.1:3306/db
|
||||
username: root
|
||||
password: 123456
|
||||
ds2:
|
||||
url: jdbc:mysql://127.0.0.1:3306/db2
|
||||
username: root
|
||||
password: 123456
|
||||
```
|
||||
|
||||
在以上配置中,`ds1` 和 `ds2` 是由用户自定义的,我们可以理解为数据源的名称,或者数据源的 `key`,这个在动态切换数据库中非常有用。
|
||||
|
||||
## 通过注解指定数据源
|
||||
|
||||
|
||||
## 编码动态切换数据源
|
||||
@ -67,6 +67,7 @@ public class MybatisFlexBootstrap {
|
||||
protected DbType dbType;
|
||||
protected SqlSessionFactory sqlSessionFactory;
|
||||
protected Class<? extends Log> logImpl;
|
||||
|
||||
private Map<Class<?>, Object> mapperObjects = new ConcurrentHashMap<>();
|
||||
private ThreadLocal<SqlSession> sessionThreadLocal = new ThreadLocal<>();
|
||||
|
||||
|
||||
@ -22,7 +22,7 @@ import com.mybatisflex.core.keygen.MybatisKeyGeneratorUtil;
|
||||
import com.mybatisflex.core.keygen.RowKeyGenerator;
|
||||
import com.mybatisflex.core.row.RowMapper;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfos;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
import org.apache.ibatis.executor.Executor;
|
||||
@ -244,7 +244,7 @@ public class FlexConfiguration extends Configuration {
|
||||
String mapperClassName = ms.getId().substring(0, ms.getId().lastIndexOf("."));
|
||||
try {
|
||||
Class<?> mapperClass = Class.forName(mapperClassName);
|
||||
return TableInfos.ofMapperClass(mapperClass);
|
||||
return TableInfoFactory.ofMapperClass(mapperClass);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ import com.mybatisflex.core.exception.FlexExceptions;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.row.Row;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfos;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
import org.apache.ibatis.builder.annotation.ProviderContext;
|
||||
|
||||
@ -83,7 +83,7 @@ class ProviderUtil {
|
||||
}
|
||||
|
||||
public static TableInfo getTableInfo(ProviderContext context){
|
||||
return TableInfos.ofMapperClass(context.getMapperType());
|
||||
return TableInfoFactory.ofMapperClass(context.getMapperType());
|
||||
}
|
||||
|
||||
public static Object getEntity(Map params) {
|
||||
|
||||
@ -18,7 +18,7 @@ package com.mybatisflex.core.row;
|
||||
import com.mybatisflex.core.javassist.ModifyAttrsRecord;
|
||||
import com.mybatisflex.core.query.QueryColumn;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfos;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
import com.mybatisflex.core.util.ArrayUtil;
|
||||
import com.mybatisflex.core.util.SqlUtil;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
@ -139,7 +139,7 @@ public class Row extends HashMap<String, Object> implements ModifyAttrsRecord {
|
||||
|
||||
|
||||
public <T> T toEntity(Class<T> entityClass) {
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(entityClass);
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
|
||||
return tableInfo.newInstanceByRow(this);
|
||||
}
|
||||
|
||||
|
||||
@ -43,6 +43,7 @@ public class TableInfo {
|
||||
private String tableName; //表名
|
||||
private Class<?> entityClass; //实体类
|
||||
private boolean camelToUnderline = true;
|
||||
private String dataSource;
|
||||
|
||||
//逻辑删除数据库列名
|
||||
private String logicDeleteColumn;
|
||||
@ -120,6 +121,14 @@ public class TableInfo {
|
||||
this.camelToUnderline = camelToUnderline;
|
||||
}
|
||||
|
||||
public String getDataSource() {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public void setDataSource(String dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public String getLogicDeleteColumn() {
|
||||
return logicDeleteColumn;
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ import java.time.chrono.JapaneseDate;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class TableInfos {
|
||||
public class TableInfoFactory {
|
||||
|
||||
|
||||
private static final Set<Class<?>> defaultSupportColumnTypes = CollectionUtil.newHashSet(
|
||||
@ -112,6 +112,9 @@ public class TableInfos {
|
||||
tableInfo.setOnUpdateListener(ClassUtil.newInstance(table.onUpdate()));
|
||||
}
|
||||
|
||||
if (StringUtil.isNotBlank(table.dataSource())){
|
||||
tableInfo.setDataSource(table.dataSource());
|
||||
}
|
||||
} else {
|
||||
//默认为类名转驼峰下划线
|
||||
String tableName = StringUtil.camelToUnderline(entityClass.getSimpleName());
|
||||
@ -7,7 +7,7 @@ import com.mybatisflex.core.dialect.LimitOffsetProcesser;
|
||||
import com.mybatisflex.core.query.CPI;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfos;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -241,7 +241,7 @@ public class AccountSqlTester {
|
||||
@Test
|
||||
public void testDeleteSql() {
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Account.class);
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Account.class);
|
||||
String sql = dialect.forDeleteEntityById(tableInfo);
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@ import com.mybatisflex.core.dialect.CommonsDialectImpl;
|
||||
import com.mybatisflex.core.dialect.IDialect;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfos;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -31,7 +31,7 @@ public class ArticleSqlTester {
|
||||
article.setContent("aaa");
|
||||
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Article.class);
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
|
||||
String sql = dialect.forInsertEntity(tableInfo, article);
|
||||
System.out.println(sql);
|
||||
}
|
||||
@ -47,7 +47,7 @@ public class ArticleSqlTester {
|
||||
article2.setContent("bbb");
|
||||
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Article.class);
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
|
||||
String sql = dialect.forInsertEntityBatch(tableInfo, CollectionUtil.newArrayList(article1, article2));
|
||||
System.out.println(sql);
|
||||
}
|
||||
@ -56,7 +56,7 @@ public class ArticleSqlTester {
|
||||
@Test
|
||||
public void testDeleteSql() {
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Article.class);
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
|
||||
String sql = dialect.forDeleteEntityById(tableInfo);
|
||||
System.out.println(sql);
|
||||
}
|
||||
@ -65,7 +65,7 @@ public class ArticleSqlTester {
|
||||
@Test
|
||||
public void testDeleteByIdsSql() {
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Article.class);
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
|
||||
String sql = dialect.forDeleteEntityBatchByIds(tableInfo, new Object[]{1, 2, 3});
|
||||
System.out.println(sql);
|
||||
}
|
||||
@ -79,7 +79,7 @@ public class ArticleSqlTester {
|
||||
article.setVersion(1L);
|
||||
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Article.class);
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
|
||||
String sql = dialect.forUpdateEntity(tableInfo, article, true);
|
||||
System.out.println(sql);
|
||||
}
|
||||
@ -96,7 +96,7 @@ public class ArticleSqlTester {
|
||||
.where(ARTICLE.ID.ge(100));
|
||||
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Article.class);
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
|
||||
String sql = dialect.forUpdateEntityByQuery(tableInfo, article, true, queryWrapper);
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user