Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required

使用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。问题终于解决了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青岚竹心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值