最近从其他组接过来一个项目,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);
}
}