使用STS创建的SpringBoot项目,其中配置文件默认的是application.properties,一般在这个文件里面配置数据源。但是这个文件想要起作用,则必须在配置主类中加入@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})自动注入,否则该配置文件中的配置无效。会报出Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.错误
但是此时如果加了这个注解,由于Mybatis的依赖中的jar包,
< dependency>
< groupId>org.mybatis.spring.boot< /groupId>
< artifactId>mybatis-spring-boot-starter< /artifactId>
< version>1.3.2</ version>
< /dependency>
mybatis-spring-1.3.2中取消了自动注入SqlSessionFactory 和 SqlSessionTemplate,所以会报出Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required错误。
解决方案一:因为我们dao层是继承于一个dao基类,所以只要在这个基类中注入任意一个属性即可。SqlSessionFactory在spring配置文件中已经配置。
public class CommonDao extends SqlSessionDaoSupport {
2 @Resource
3 public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
4 super.setSqlSessionFactory(sqlSessionFactory);
5 }
解决方案二:但是我的Mapper层是使用了注解版Mybatis,是一个接口,不能继承类。所以这个办法就不能用了。
删除@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})的括号里面的就可以了。
解决方案三:那就想办法不要@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})自动注入,所以必须把application.properties配置文件改为application.yml。问题终于解决了。