我正在使用Hikari CP和JDBC模板将Controller中的传入请求参数插入数据库。
所以我做了以下事情:
@RestController
public class HomeController {
@Autowired
private JdbcTemplate jtm;
@RequestMapping(value="/insert/data",produces={MediaType.APPLICATION_JSON_VALUE},method=RequestMethod.GET)
public ResponseEntity<?> insertData(@RequestParam("id")int id ,@RequestParam("name")String name) throws IOException, ClassNotFoundException, SQLException{
String sql = "INSERT INTO public.users(id, name) VALUES (?, ?, ?);";
jtm.update(sql,id,name);
}
}
但是这会引发以下错误:
The type org.springframework.jdbc.support.KeyHolder cannot be resolved. It is indirectly referenced from required .class files
我在/ src / main / resources
中有一个hikari.propertiesdriverClassName=org.postgresql.Driver
jdbcUrl=jdbc:postgresql://10.1.9.72:5432/data_base
maximumPoolSize=20
username=user
password=password
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
我的HikariCP配置Java类
AppConfig.java
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean(destroyMethod = "close")
public DataSource dataSource() throws SQLException {
HikariConfig config = new HikariConfig("/hikari.properties");
HikariDataSource dataSource = new HikariDataSource(config);
return dataSource;
}
}
我在我的pom.xml中添加了以下依赖项(Hikari Cp,Spring JBDC,spring-tx)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java7</artifactId>
<version>2.4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.1.RELEASE</version>
</dependency>
感谢任何帮助