Spring Boot学习|问题汇总(一)

问题预览

  • 1)Failed to configure a DataSource: ‘url’ attribute is not specified…
  • 2)ERR Client sent AUTH, but no password is set
  • 3)关于SpringDataJpa中findOne()方法报错问题
  • 4)关于SpringBoot中使用RedisCacheManager没有构造函数的问题
  • 5)springboot 启动报错Field XXX required a bean of type XXX that could not be found.

1.Failed to configure a DataSource: ‘url’ attribute is not specified…

  • Spring Boot项目中含有Mybatis,打Jar包运行之后,报如下错误:
    Description:
       
       Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
       
       Reason: Failed to determine a suitable driver class
       
       
       Action:
       
       Consider the following:
           If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
           If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently
   active).
  • 解决方案:

    方案一
    DataSourceAutoConfiguration会自动加载,所以排除此类的autoconfig。启动以后就可以正常运行。

    @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
    

    方案二
    没有配置spring - datasource - url属性,所以在application.properties/或者application.yml文件中没有添加数据库配置信息.

    spring:
      datasource:
        url: jdbc:mysql://localhost:XX/XX?useUnicode=true&characterEncoding=UTF-8&useSSL=false
        username: xx
        password: XX
        driver-class-name: com.mysql.cj.jdbc.Driver
    

2.ERR Client sent AUTH, but no password is set
客户端设置了auth认证,但没设置密码

  • 解决方案
    方案一:
    在redis配置文件中redis.conf加入:

       requirePass: 你的密码
    

    方案二:
    把redis配置中password一行去掉,既然没密码,就不要写。

3.关于SpringDataJpa中findOne()方法报错问题

  • findOne()方法报错了,不能用来当作根据id查询了,
    2.0.5的已经变成了findById(id).get()来查询了。
    这是两个不同的版本,源码已经发生变化。
    后来去找源码中的findOne方法发现,findOne方法已经变了。

4.关于SpringBoot中使用RedisCacheManager没有构造函数的问题
在SpringBoot2.0中, 使用

public CacheManager cacheManager(RedisTemplate redisTemplate){
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
    rcm.setDefaultExpiration(60);//秒
    return rcm;
} 

会提示没有相应的构造方法,这是由于SpringBoot新的改动导致。
改成:

public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheManager rcm = RedisCacheManager.builder(factory).build();
return rcm;
}

问题解决

5.pringboot 启动报错Field XXX required a bean of type XXX that could not be found.

Description:
 
Field XXMapper in com.XXService required a bean of type 'com.XXMapper' that could not be found.
  • 解决方案:
    方案一:
    配置了mybatis,但没有指定扫描的包。 方法1:启动类加注释:
@MapperScan(basePackages= { "mapper所在的包路径" }, sqlSessionFactoryRef = "sqlSessionFactory")

,表示扫描xx.xx.mapper包下的所有mapper。
也可以直接在你生成出来的xxxMapper.java类上加@Mapper标签。

方案二:
没有自动注入导致。
你的service类上面没有@service注解或者mapper上没有@Repository注解,添加上去即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值