问题预览
- 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
注解,添加上去即可。