记录一次BaseMapper下的方法不能使用的坑

1 篇文章 0 订阅
1 篇文章 0 订阅

最近从其他组接过来一个项目,BaseMapper下的方法全都不能使用

1.环境是多数据源,所以SqlSessionFactoryBean需要自己配置,而不是SpringBoot自动生成。

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

2.问题开始:写完Server层之后,开始Debug执行,恶心的事情出现了,所有BaseMapper下的自带方法全都不能用,报错:未找到绑定

3.解决问题:将 SqlSessionFactoryBean 换成 MybatisSqlSessionFactoryBean。这样就可以解决BaseMapper下的方法找不到的问题!

// 自定义数据源时,使用 SqlSessionFactoryBean 建立sql会话,这样不可以使用BaseMapper里边的方法
// SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
// 需要使用 MybatisSqlSessionFactoryBean 建立sql会话
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();

4.我们的项目有两个数据源,Mysql和Persto,下面是我Mysql的MybatisDataSourceConfig配置类。

package com.xxx.xxx.xxx;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@MapperScan(basePackages = {"com.yaoyanshe.dataapi.mybatis.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory3")
public class MybatisDataSourceConfig {


    @Bean(name = "sqlSessionFactory3")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
        // 自定义数据源时,使用 SqlSessionFactoryBean 建立sql会话,这样不可以使用BaseMapper里边的方法
        // SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        // 需要使用 MybatisSqlSessionFactoryBean 建立sql会话
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        // 自定义数据源时,使用mybatis的方式建立 SqlSessionFactoryBean,这样不可以使用BaseMapper里边的方法
        // org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();

        com.baomidou.mybatisplus.core.MybatisConfiguration configuration = new com.baomidou.mybatisplus.core.MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);

        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis-mapper/*" +
                ".xml"));

        //分页插件
        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        //数据库
        properties.setProperty("helperDialect", "mysql");
        //是否分页合理化
        properties.setProperty("reasonable", "false");
        interceptor.setProperties(properties);

        Interceptor[] plugins = {interceptor};
        bean.setPlugins(plugins);


        return bean.getObject();
    }

    @Bean(name = "transactionManager3")
    public DataSourceTransactionManager transactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionTemplate3")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory3") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    
}
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值