今天整合mybatis的时候 遇到一个问题
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.pratice.mapper.UserMapper.selectList 一直找不到对应的mapper; 就是我自己写sql的话可以,但是如果用框架自带的查询 就报错了!!!原因在哪儿呢?
是这样 一般整合mybatis的话 (题外话)
@SpringBootApplication @MapperScan("com.mote.dao.mysql")//设置mapper接口的扫描包 public class PersonnelAttendanceApplication{} 扫描mapper就行了,但是你若是有自定义数据源的话就要注意了
自定义数据源 看我注释部分
package com.mote.config.mysql;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
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.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.mote.dao.mysql", sqlSessionFactoryRef = "mySqlSessionFactory")
public class MysqlDataSourceConfig {
static final String MAPPER_LOCATION = "classpath*:mybatis/mappers/*.xml";
@Primary
// 表示这个数据源是默认数据源
@Bean(name = "myDataSource")
@ConfigurationProperties(prefix = "spring.datasource.mysql")
public DataSource myDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "mySqlSessionFactory")
public SqlSessionFactory mySqlSessionFactory(
@Qualifier("myDataSource") DataSource datasource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MysqlDataSourceConfig.MAPPER_LOCATION));
return bean.getObject();
// SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
// sessionFactory.setDataSource(pgDataSource);
// sessionFactory
.setMapperLocations(new PathMatchingResourcePatternResolver()
// .getResources(PgsqlDataSourceConfig.MAPPER_LOCATION));
}
@Primary
@Bean("mySqlSessionTemplate")
public SqlSessionTemplate mySqlsessiontemplate(
@Qualifier("mySqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}
注释的部分我用的是 用的原生的SqlSessionFactoryBean没有用MybatisSqlSessionFactoryBean
所以 你想用框架的东西 就要看现在这个方法 mySqlSessionFactory使用MybatisSqlSessionFactoryBean 可以使用mybatis-BaseMapper中的方法
总结:希望我的思路能解决你的问题。另外最简单整合mybatis 请看我这个链接 非常简单!!
链接:https://pan.baidu.com/s/1gnriot0SaMUeeaeo-fyFkQ?pwd=lbcg
提取码:lbcg
--来自百度网盘超级会员V5的分享