记录一次排坑:mybatis-plus无法正确识别数据库

现象:

使用mybatis-plus的自动分页,查询sql后面拼接的一直是limit 10,而我本地连接的数据库是oceanbase Oracle语法。导致分页一直报错。

开始的连接数据库url格式:


jdbc:oceanbase://<host>:<port>/<database>?user=<username>&password=<password>&...

排查:

开始试图通过手动修改mybatis-plus的数据库类型

@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {
    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.Oracle));
        return interceptor;
    }
}

结果修改不生效,mybatis-plus的数据库类型走的还是MySQL

通过debug排查,发现是mybatis-plus在连接oceabase数据库时会默认选择mysql类型。

解决:

修改连接数据库url格式:


jdbc:oceanbase:oracle://<host>:<port>/<database>?user=<username>&password=<password>&...

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于 MyBatis-Plus 和 PostgreSQL 数据库的集合类型的映射配置,你可以按照以下步骤进行操作: 1. 首先,确保你已经正确配置了 MyBatis-Plus 和 PostgreSQL 的依赖项。 2. 在你的实体类中,定义一个字段来映射 PostgreSQL 数据库的集合类型。例如,你可以使用 Java 的 List 类型。 ```java public class YourEntity { private Long id; private List<String> yourCollection; // getter and setter methods } ``` 3. 在你的 Mapper 接口中,使用 `@Param` 注解指定参数名,并在 SQL 语句中使用 `#{yourCollection, jdbcType=ARRAY}` 来映射集合类型。 ```java public interface YourMapper extends BaseMapper<YourEntity> { @Insert("INSERT INTO your_table (id, your_collection) VALUES (#{id}, #{yourCollection, jdbcType=ARRAY})") int insertYourEntity(@Param("id") Long id, @Param("yourCollection") List<String> yourCollection); } ``` 4. 在你的数据库配置文件中,添加一个类型处理器来处理集合类型的映射。创建一个类继承 `BaseTypeHandler<List>` 并实现相应的方法。 ```java import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.postgresql.util.PGobject; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; public class PgListTypeHandler extends BaseTypeHandler<List> { @Override public void setNonNullParameter(PreparedStatement ps, int i, List parameter, JdbcType jdbcType) throws SQLException { PGobject pGobject = new PGobject(); pGobject.setType("your_collection_type"); // 替换为你的集合类型的名称 pGobject.setValue(parameter.toString().replace("[", "{").replace("]", "}")); // 转换为 PostgreSQL 数组格式 ps.setObject(i, pGobject); } @Override public List getNullableResult(ResultSet rs, String columnName) throws SQLException { // 返回结果集中指定列的值 return (List) rs.getArray(columnName).getArray(); } @Override public List getNullableResult(ResultSet rs, int columnIndex) throws SQLException { // 返回结果集中指定列的值 return (List) rs.getArray(columnIndex).getArray(); } @Override public List getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { // 返回存储过程中指定输出参数的值 return (List) cs.getArray(columnIndex).getArray(); } } ``` 5. 在你的数据库配置文件中,注册这个类型处理器。 ```xml <configuration> <typeHandlers> <typeHandler handler="com.example.PgListTypeHandler"/> </typeHandlers> </configuration> ``` 请注意,上述代码中的一些部分可能需要根据你的实际情况进行调整,例如实体类名称、表名、字段名以及集合类型名称等。确保在使用之前,根据你的项目需求进行适当的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值