課程
/后端開發
/Java
/SpringBoot+MyBatis搭建迷你小程序
我都是跟著視頻敲的 為什么到了這里會出錯呢
2018-03-13
源自:SpringBoot+MyBatis搭建迷你小程序 3-3
正在回答
你所建的config包必須和Application類同級或更低級,才會被掃描,注意springboot工程結構,問題解決。
我也出現了這個問題,請問怎么解決
我也出現了這個錯誤請問樓主解決了嗎
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; ? ?}}
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;//掃描這個類@Configurationpublic 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搭建小程序后臺
3 回答@Qualifier注解后的dataSource飄紅
3 回答@Qualifier("dataSource")引入不進來
2 回答無法創建dataSource的Bean 加了@Qualifier(value = "dataSource")也不行
4 回答dataSource報錯
3 回答private DataSource dataSource;怎么一直報錯啊?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-06-07
你所建的config包必須和Application類同級或更低級,才會被掃描,注意springboot工程結構,問題解決。
2018-06-03
我也出現了這個問題,請問怎么解決
2018-05-25
我也出現了這個錯誤請問樓主解決了嗎
2018-03-13
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;
? ?}
}
2018-03-13
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;
? ?}
}