mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
增加说明书内容
This commit is contained in:
parent
c420adf006
commit
91d0e89ee6
@ -105,6 +105,89 @@ interface AccountMapper extends BaseMapper{
|
||||
}
|
||||
```
|
||||
|
||||
`@UseDataSource(value = "#my_expression")`扩展数据源切换,表达式动态读取 key 值
|
||||
|
||||
- 自定义表达式扩展,对接口 DataSourceProcessor 进行实现,推荐约定动态表达式以 `#` 作为起始标识(当然并不强制做要求)
|
||||
```java
|
||||
// 自定义基于Session的表达式取值示例
|
||||
public class InSessionDataSourceProcessor implements DataSourceProcessor {
|
||||
private static final String SESSION_PREFIX = "#session";
|
||||
@Override
|
||||
public String process(String dataSourceKey, Object mapper, Method method, Object[] arguments) {
|
||||
if (StrUtil.isBlank(dataSourceKey)) return null;
|
||||
if (!dataSourceKey.startsWith(SESSION_PREFIX)) return null;
|
||||
|
||||
HttpServletRequest request = RequestContextHolder.get.....;
|
||||
if (null==request) return null;
|
||||
return request.getSession().getAttribute(dataSourceKey.substring(9)).toString();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- 内置取值表达式取值解析处理器,作为示例参考
|
||||
- `DelegatingDataSourceProcessor`,对`DataSourceProcessor`结构进行扩大和增强的委托类,多处理器推荐使用该委托类进行注入,注意委托类内使用`List`委托管理多个解析处理器,执行时有先后顺序。
|
||||
- `ParamIndexDataSourceProcessor`,针对简单参数快速取值。
|
||||
- `SpelExpressionDataSourceProcessor`,`Spring` 模式下 `SPEL` 表达式取值。
|
||||
- 注入动态数据源取值处理器
|
||||
|
||||
```java
|
||||
// 注入顺序既为执行时的先后顺序(前面的处理器一旦正确处理,将不会再往下传递处理)
|
||||
DelegatingDataSourceProcessor dataSourceProcessor = DelegatingDataSourceProcessor.with(
|
||||
// 参数索引快速取值的
|
||||
new ParamIndexDataSourceProcessor(),
|
||||
// Spel 表达式的
|
||||
new SpelExpressionDataSourceProcessor(),
|
||||
new .....等多个自行扩展表达式()
|
||||
);
|
||||
// 实例 setter 赋值
|
||||
DataSourceManager.setDataSourceProcessor(dataSourceProcessor);
|
||||
```
|
||||
|
||||
- 应用示例:
|
||||
```java
|
||||
@Mapper
|
||||
public interface MyMapper extends BaseMapper<MyEntity >{
|
||||
// 取值第一个参数的值(arg0的值)
|
||||
@UseDataSource(value = "#first")
|
||||
MyEntity testFirst(String arg0, String arg1, String arg2);
|
||||
|
||||
// 取值索引1(第二个参数 arg1 )的值
|
||||
@UseDataSource(value = "#index1")
|
||||
MyEntity testIndex(String arg0, String arg1, String arg2);
|
||||
|
||||
// 取值最后一个参数的值(arg3的值)
|
||||
@UseDataSource(value = "#last")
|
||||
MyEntity testLast(String arg0, String arg1, String arg2, String arg3);
|
||||
|
||||
// Spel 表达式取值数据源
|
||||
@UseDataSource(value = "#condition.getDsId()")
|
||||
MyEntity testSpel(MyDatasourceCondition condition);
|
||||
}
|
||||
```
|
||||
- 调用
|
||||
```java
|
||||
public void test(){
|
||||
// mapper中的注解 @UseDataSource(value = "#first")
|
||||
myMapper.testFirst("my-dskey1", "arg1", "arg2");
|
||||
|
||||
// 取值索引1(第二个参数)的值;mapper中的注解 @UseDataSource(value = "#index1")
|
||||
myMapper.testIndex("arg0", "my-dskey2", "arg2");
|
||||
|
||||
// mapper中的注解 @UseDataSource(value = "#last")
|
||||
myMapper.testLast("arg0", "arg1", "arg2","my-dskey4");
|
||||
|
||||
MyDatasourceCondition condition = new MyDatasourceCondition();
|
||||
condition.setDsId("my-dskey5");
|
||||
// mapper中的注解 @UseDataSource(value = "#condition.getDsId()")
|
||||
myMapper.testSpel(condition);
|
||||
}
|
||||
```
|
||||
|
||||
- **注意:**
|
||||
- 1:使用区分`Spring模式`和`非Spring`模式,`Spring`模式下,处理逻辑`DataSourceInterceptor`优先级高于 `FlexMapperProxy`,
|
||||
所以`Spring模式下仅 DataSourceInterceptor 生效`(切面生效的前提下)。`非Spring` 模式下,`仅支持注解使用到 Mapper(Dao层)`,
|
||||
使用到其他层(如Service层)不支持注解解析。
|
||||
- 2:`Spring模式`下,不区分使用到程序的层(Controller、Service、Dao层都支持),下层控制粒度细上层控制粒粗,使用时根据需要进行灵活应用。
|
||||
|
||||
|
||||
`@Table(dataSource="dataSourceName")` 示例:
|
||||
|
||||
@ -11,10 +11,7 @@ import java.lang.reflect.Method;
|
||||
* 使用区分Spring模式 和 非Spring模式,Spring模式下,代理处理逻辑 DataSourceInterceptor{@link com.mybatisflex.spring.datasource.DataSourceInterceptor} 优先级高于 FlexMapperProxy{@link com.mybatisflex.core.mybatis.binding.FlexMapperProxy} ;
|
||||
* 所以Spring模式下仅 DataSourceInterceptor 生效(切面生效的前提下)。非Spring 模式下,仅支持注解使用到 Mapper(Dao层),使用到其他层(如Service层)不支持注解解析。
|
||||
* <p>
|
||||
* Spring模式下,切面生效的前提下,不区分使用到程序的层(Controller、Service、Dao层都支持),但是建议使用到Mapper层使用注解。其更接近数据操作,更符合控制粒度。
|
||||
* 使用在 Controller 和 Service 层则其下层调用的所有操作都会受影响,控制粒度会比较粗,受影响面大。而且容易被下层的注解配置替换上层的注解配置。
|
||||
* <p>
|
||||
* 如果同一个调用链多处使用注解 @UseDataSource 则越接近Dao层优先级越高,如 Service 和 Mapper 上同时使用了,则 Service 中解析出来后,当执行 Mapper 时再次解析,会覆盖之前的值。
|
||||
* Spring模式下,不区分使用到程序的层(Controller、Service、Dao层都支持),下层控制粒度细上层控制粒粗,使用时根据需要进行灵活应用。
|
||||
*
|
||||
* @author Alay
|
||||
* @since 2024-12-07 15:34
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user