今天在使用mybatis的基于springboot的框架的基础功能包抽离出来的时候,在增加spring yml自定义设置后,在业务项目启动时会报出以下异常。
***************************
APPLICATION FAILED TO START
***************************
Description:
Cannot determine embedded database driver class for database type NONE
Action:
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "dev,redis,config,mongodb,activemq,socketio" are currently active).
原因是需要在启动类的@EnableAutoConfiguration或@SpringBootApplication中添加exclude = {DataSourceAutoConfiguration.class},排除此类的autoconfig启动以后就可以正常运行。
并且需要使用到
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
上面两个依赖,更新spring boot的版本
并且mybatis在某个版本后取消了sqlSessionFactory的自动注入,需要显示注入。
然而设置这玩意儿是需要设置dataSource的,于是dataSource也得显示声明
单独配置一个:
@Configuration
public class MybatisConfig {
@Autowired
private DataSourceProperties dataSourceProperties;
@Bean(name = "dataSource")
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(dataSourceProperties.getUrl());
System.out.println(dataSourceProperties.getUrl());
dataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
dataSource.setUsername(dataSourceProperties.getUsername());
dataSource.setPassword(dataSourceProperties.getPassword());
return dataSource;
}
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
return sqlSessionFactoryBean.getObject();
}
}
tips:
注释:@MapperScan(basePackages = {“xxx.mapper.front”,“xxx,mapper.admin”,“xxx.mapper”})
意思为:改注解内声明对应包下的所有class都是Mapper类
通过使用@MapperScan可以指定要扫描的Mapper类的包的路径