若依系统版本: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;