```xml
<dependency>
    <groupId>com.mybatis-flex</groupId>
    <artifactId>mybatis-flex-solon-plugin</artifactId>
    <version>最新版本</version>
</dependency>
```


### 1、描述

数据扩展插件，为 Solon Data 提供基于 mybatis-flex（[代码仓库](https://gitee.com/mybatis-flex/mybatis-flex)）的框架适配，以提供ORM支持。


可注入类型：

| 支持类型 | 说明                                                                           |
| -------- |------------------------------------------------------------------------------|
| Mapper.class     | 注入 Mapper。例：`@Inject UserMapper userMapper`                                  |
| SqlSessionFactory     | 注入 SqlSessionFactory。例：`@Inject SqlSessionFactory sessionFactory` （不推荐直接使用）  |
| RowMapperInvoker | 注入 RowMapperInvoker。例：`@Inject RowMapperInvoker rowMapper`                   |


### 3、数据源配置

`mybatis-flex` 配置对应的结构实体为： MybatisFlexProperties

```yml
# mybatis-flex 数据源配置（会自动构建数据源）
mybatis-flex.datasource:
  db1:
      type: "com.zaxxer.hikari.HikariDataSource"
      jdbcUrl: jdbc:mysql://localhost:3306/rock?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true
      driverClassName: com.mysql.cj.jdbc.Driver
      username: root
      password: 123456
  db2:
      type: "com.zaxxer.hikari.HikariDataSource"
      jdbcUrl: jdbc:mysql://localhost:3306/water?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true
      driverClassName: com.mysql.cj.jdbc.Driver
      username: root
      password: 123456

#  mybatis-flex 其它配置
mybatis-flex:
    type-aliases-package:    #支持包名 或 类名（大写开头 或 *）//支持 ** 或 * 占位符
        - "demo4021.model"
        - "demo4021.model.*" #这个表达式同上效果
    type-handlers-package: #支持包名 或 类名（大写开头 或 *）//支持 ** 或 * 占位符
        - "demo4021.dso.mybaits.handler"
        - "demo4021.dso.mybaits.handler.*" #这个表达式同上效果
    mapper-locations:        #支持包名 或 类名（大写开头 或 *）或 xml（.xml结尾）//支持 ** 或 * 占位符
        - "demo4021.**.mapper"
        - "demo4021.**.mapper.*" #这个表达式同上效果
        - "classpath:demo4035/**/mapper.xml"
        - "classpath:demo4035/**/mapping/*.xml"
    configuration:  #扩展配置（要与 FlexConfiguration 类的属性一一对应）
        cacheEnabled: false
        mapUnderscoreToCamelCase: true
    global-config:   #全局配置（要与 FlexGlobalConfig 类的属性一一对应）//只是示例，别照抄
        printBanner: false
        keyConfig:
            keyType: "Generator"
            value: "snowFlakeId"


#
#提示：使用 "**" 表达式时，范围要尽量小。不要用 "org.**"、"com.**" 之类的开头，范围太大了，会影响启动速度。
#
```

#### Mapper 配置注意事项：

* 通过 mapper 类包名配置。 xml 与 mapper 需同包同名

```yml
mybatis-flex.mapper-locations: 
  - "demo4035.dso.mapper"
```

* 通过 xml 目录进行配置。xml 可以固定在一个资源目录下

```yml
mybatis-flex.mapper-locations: 
  - "classpath:mybatis/db1/*.xml"
```


重要提示：如果启动时出现 mapper 注入失败，说明 mappers 配置有问题（没有覆盖到）

### 4、定制

如果 yml 配置不方便。。。也可以使用代码定制作为补充

```java
//配置 mf （如果配置不能满足需求，可以进一步代助代码）
@Component
public class MyBatisFlexCustomizerImpl implements MyBatisFlexCustomizer, ConfigurationCustomizer {
    @Override
    public void customize(FlexGlobalConfig globalConfig) {

    }

    @Override
    public void customize(FlexConfiguration configuration) {

    }
}
```


### 5、代码应用

```java
//应用
@Component
public class AppService {
    @Inject
    AppMapper appMapper; //xml sql mapper

    @Inject
    BaseMapper<App> appBaseMapper; //base mapper

    public void test0() {
        App app1 = appMapper.getAppById(12);
        App app2 = appBaseMapper.selectOneById(12);
    }

    @UseDataSource("db1")
    public void test1() {
        App app1 = appMapper.getAppById(12);
        App app2 = appBaseMapper.selectOneById(12);
    }

    public void test2() {
        try {
            DataSourceKey.use("db1");
            App app1 = appMapper.getAppById(12);
            App app2 = appBaseMapper.selectOneById(12);
        } finally {
            DataSourceKey.clear();
        }
    }
}
```




### 6、如需使用`mybatis-flex`的APT功能参考以下配置方式

#### 6.1、配置`annotationProcessorPaths`或引入`mybatis-flex-processor`，两者二选一

```xml
<!--配置annotationProcessorPaths，两种方式选一即可-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <annotationProcessorPaths>
            <path>
                <groupId>com.mybatis-flex</groupId>
                <artifactId>mybatis-flex-processor</artifactId>
                <version>最新版本</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>
<!-- 引入mybatis-flex-processor，两种方式选一即可-->
<dependency>
    <groupId>com.mybatis-flex</groupId>
    <artifactId>mybatis-flex-processor</artifactId>
    <version>最新版本</version>
    <scope>provided</scope>
</dependency>
```


#### 6.2、在项目的 根目录 （ `pom.xml` 所在的目录）下创建名为 `mybatis-flex.config` 的文件
> 完整配置参考：https://mybatis-flex.com/zh/others/apt.html


#### 6.3、调用`mvn clean package`即可生成对应的APT类


#### 6.4、Enjoy it



### 具体参考：

[https://gitee.com/opensolon/solon-examples/tree/main/4.Solon-Data/demo4035-mybatisflex](https://gitee.com/opensolon/solon-examples/tree/main/4.Solon-Data/demo4035-mybatisflex)

