总体描述
java出现Error creating bean with name 'blockController': Unsatisfied dependency expressed through field 'blockService'问题,后续有Cannot load driver class: com.mysql.cj.jdbc.Driver
我的工程目录结构
本人主要代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>ssmdemo01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ssmdemo01</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 处理一些请求日志,或者对某些方法进行一些监控,如果出现例外情况应该进行怎么样的处理 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- thymeleaf模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- 热启动配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork><!-- fork:如果没有该配置,这个devtools不会起作用,即应用不会restart -->
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
前端框架thymeleaf映射
spring.thymeleaf.prefix=classpath:/templates/
server.servlet.session.timeout=10
server.tomcat.uri-encoding=UTF-8
#spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.servlet.content-type=text/html
#数据库
# ?serverTimezone=GMT,去除时区异常
spring.datasource.url = jdbc:mysql://localhost:3306/agriculture_test?serverTimezone=GMT%2B8
spring.datasource.username = root
spring.datasource.password = rs651616
#jdbc驱动的全限定类名,默认根据URL自动检测
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
#配置连接池同时能维持的最大连接数
spring.datasource.tomcat.max-active = 200
### 空闲时间,idle连接池中的最大连接数
spring.datasource.tomcat.max-idle = 10
### idle连接池中的最小空闲连接数
spring.datasource.tomcat.min-idle = 5
##连接池在等待返回连接时,最长等待时间再抛出异常
spring.datasource.tomcat.max-wait = 10000
## 在连接池启动时要建立的连接数
spring.datasource.tomcat.initial-size = 5
Block.java
package com.example.demo.dao;
public class Block {
private String id;
private String name;
private String type;
private String coord;
private String property;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
BlockService.java
package com.example.demo.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import com.example.demo.dao.Block;
@Service
public class BlockService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Block> getList(){
String sql = "SELECT * FROM base_block";
return (List<Block>) jdbcTemplate.query(sql, new RowMapper<Block>(){
@Override
public Block mapRow(ResultSet rs, int rowNum) throws SQLException {
// TODO Auto-generated method stub
Block block = new Block();
block.setId(rs.getString("id"));
block.setName(rs.getString("name"));
block.setType(rs.getString("type"));
block.setCoord(rs.getString("coord"));
block.setProperty(rs.getString("property"));
return block;
}
});
}
// public Block findById(String i) {
// // TODO Auto-generated method stub
//
// return null;
// }
}
BlockController.java
package com.example.demo.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.dao.Block;
import com.example.demo.service.BlockService;
@RestController
@RequestMapping("/block")
public class BlockController {
private static final Logger logger= LoggerFactory.getLogger(BlockController.class);
@Autowired
private BlockService blockService;
@RequestMapping("/datalist")
public List<Block> getBlocks(){
logger.info("从数据库读取Bolckdata集合");
return blockService.getList();
}
}
出现的问题
Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver
at org.springframework.util.Assert.state(Assert.java:94) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:224) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:176) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:43) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:81) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
... 73 common frames omitted
解决方案
开始一直以为是我的controller或service类写错了,仔细看完错误最后发现是 Cannot load driver class: com.mysql.cj.jdbc.Driver,百度后逐个试,发现是jdbc驱动的原因
#jdbc驱动的全限定类名,默认根据URL自动检测
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
查到这个链接,由于mysql驱动版本比较低(5及以下),不支持com.mysql.cj.jdbc.Driver驱动,故使用较低版本驱动即可!com.mysql.jdbc.Driver,修改后可以运行了,
具体版本对应详情参考。
修改后就可以了。
2019年10月14日