若依ruoyi系统整合配置mybatis-flex说明

若依系统版本:4.7.8

mybatis-flex版本:1.7.8

一、在POM中添加依赖

父pom.xml添加依赖

添加 mybatis-flex.pom 依赖

            <!-- mybatis-flex -->
            <dependency>
                <groupId>com.mybatis-flex</groupId>
                <artifactId>mybatis-flex-dependencies</artifactId>
                <version>${mybatis-flex.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

移除如下依赖

            <!-- pagehelper 分页插件 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>${pagehelper.boot.version}</version>
            </dependency>

替换成如下依赖:[Bug]: 和PageHelper一起使用 java.sql.SQLException: No value specified for parameter x

            <!-- pagehelper 分页插件 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.3.3</version>
            </dependency>

添加编译依赖:mybatis-flex-processor,如下图:

子pom.xml添加依赖

在ruoyi-common的pom.xml中添加如下依赖

        <dependency>
            <groupId>com.mybatis-flex</groupId>
            <artifactId>mybatis-flex-spring-boot-starter</artifactId>
        </dependency>

二、原Mybatis之yml中配置更换

原配置:

# MyBatis
mybatis:
  # 搜索指定包别名
  typeAliasesPackage: com.ruoyi.**.domain
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath*:mapper/**/*Mapper.xml
  # 加载全局的配置文件
  configLocation: classpath:mybatis/mybatis-config.xml

 新配置:

# MyBatis
mybatis-flex:
  type-aliases-package: com.ruoyi.**.domain
  mapper-locations: classpath*:mapper/**/*Mapper.xml
  config-location: classpath:mybatis/mybatis-config.xml

三、原类MyBatisConfig代码替换

替换结果如下:

import com.github.pagehelper.PageInterceptor;
import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.spring.boot.ConfigurationCustomizer;
import com.mybatisflex.spring.boot.MybatisFlexAutoConfiguration;
import com.mybatisflex.spring.boot.SpringBootVFS;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.AutoMappingBehavior;
import org.apache.ibatis.session.AutoMappingUnknownColumnBehavior;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Mybatis配置
 *
 * @author songjianyong
 */
@Configuration
public class MyBatisConfig implements ConfigurationCustomizer {

    /**
     * 页面拦截器使用自动配置无效,
     * 需要手动注入bean,以让{@link MybatisFlexAutoConfiguration}正常获取到{@link PageInterceptor}
     *
     * @return {@link PageInterceptor}
     */
    @Bean
    public PageInterceptor pageInterceptor() {
        return new PageInterceptor();
    }

    @Override
    public void customize(FlexConfiguration configuration) {
        configuration.setLogImpl(StdOutImpl.class);
        configuration.setVfsImpl(SpringBootVFS.class);
        // 自动驼峰命名规则(camel case)映射
        configuration.setMapUnderscoreToCamelCase(true);
        // MyBatis 自动映射策略
        // NONE:不启用 PARTIAL:只对非嵌套 resultMap 自动映射 FULL:对所有 resultMap 自动映射
        configuration.setAutoMappingBehavior(AutoMappingBehavior.FULL);
        // MyBatis 自动映射时未知列或未知属性处理策
        // NONE:不做处理 WARNING:打印相关警告 FAILING:抛出异常和详细信息
        configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.NONE);
    }
}

四、自动生成TableDef配置【可选】

  • Mapper.java类继承接口:com.mybatisflex.core.BaseMapper
  • 在实体类上添加注解:com.mybatisflex.annotation.Table
  • 在实体类字段体检主键注解:com.mybatisflex.annotation.Id
    /**
     * 自增主键
     */
    @Id(keyType = KeyType.Auto)
    private Long id;
  •  非数据库对应字段添加忽略注解:com.mybatisflex.annotation.Column
    /**
     * 搜索值:只是业务字段,而非数据库对应字段
     */
    @JsonIgnore
    @Column(ignore = true)
    private String searchValue;

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值