Cannot load driver class: org.postgresql.Driver

文章描述了将一个基于SpringMVC的项目重构为前后端分离的SpringBoot+Vue应用的过程。在重构过程中遇到SpringBoot启动时无法加载postgresql驱动的问题,解决方案是在pom.xml中引入postgresql依赖,并在MybatisPlus配置中添加分页和乐观锁拦截器。
摘要由CSDN通过智能技术生成

一、背景介绍

近期需要将一个前后端不分离的 SpringMVC 工程,重构为前后端分离的工程,前端用 Vue框架,后端使用 SpringBoot 框架。


二、问题说明

在启动 SpringBoot 服务时,日志报错“Cannot load driver class: org.postgresql.Driver”


三、解决方案

1、在 pom.xml 文件中引入 postgresql
        <!-- postgresql  -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

2、增加拦截器 MybatisPlusInterceptor
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisPlusConfig {

    /**
     * 插件配置
     *
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 向MyBatis-Plus的过滤器链中添加分页拦截器,需要设置数据库类型(主要用于分页方言)
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL));
//        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 添加乐观锁拦截器
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值