@Qualifier("dataSource")报错!

来源:3-3 datasource和sessionfactorybean的配置

慕先生0230488

2018-03-13 15:07

http://img3.mukewang.com/5aa778500001a3bc05110154.jpg

我都是跟着视频敲的 为什么到了这里会出错呢

写回答 关注

5回答

  • 慕后端6395667
    2018-06-07 20:51:42

    https://img.mukewang.com/5b19299400013e6509870363.jpg

    你所建的config包必须和Application类同级或更低级,才会被扫描,注意springboot工程结构,问题解决。

  • qq_花纹纹_0
    2018-06-03 16:10:44

    我也出现了这个问题,请问怎么解决


  • 宝慕林7059632
    2018-05-25 21:56:10

    我也出现了这个错误请问楼主解决了吗

  • 慕先生0230488
    2018-03-13 15:12:28

    package config.dao;

    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Value;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;

    import java.beans.PropertyVetoException;

    @Configuration
    //配置mybatis中mapper的扫描路径
    @MapperScan("com.imooc.demo.dao")
    public class DataSourceConfiguration {
       @Value("${jdbc.driver}")
       private String jdbcDriver;
       @Value("${jdbc.url}")
       private String jdbcUrl;
       @Value("${jdbc.username}")
       private String jdbcUserName;
       @Value("${jdbc.password}")
       private String jdbcPassword;

       @Bean(name="dataSource")
       public ComboPooledDataSource createDataSource() throws PropertyVetoException {
           //设置数据源
           ComboPooledDataSource dataSource = new ComboPooledDataSource();
           dataSource.setDriverClass(jdbcDriver);
           dataSource.setJdbcUrl(jdbcUrl);
           dataSource.setUser(jdbcUserName);
           dataSource.setPassword(jdbcPassword);
           //关闭连接后不自动提交
           dataSource.setAutoCommitOnClose(false);
           return dataSource;
       }
    }

  • 慕先生0230488
    2018-03-13 15:12:05

    package config.dao;

    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

    import javax.sql.DataSource;
    import java.io.IOException;

    //扫描这个类
    @Configuration
    public class SessionFactoryConfiguration {
       @Value("${mybatis_config_file}")
       private String myBatisConfigFilePath;
       @Value("${mybatis_path}")
       private String mapperPath;
       @Value("${entity_package}")
       private String entityPackage;

       @Autowired
       private DataSource dataSource;

       @Bean(name="sqlSessionFactory")
       public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException {
           SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
           sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(myBatisConfigFilePath));
           PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
           String packageSearchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperPath;
           sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSearchPath));
           sqlSessionFactoryBean.setDataSource(dataSource);
           sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage);
           return sqlSessionFactoryBean;
       }
    }

SpringBoot+MyBatis搭建迷你小程序

SpringBoot+MyBatis搭建小程序后台

91500 学习 · 621 问题

查看课程

相似问题