spring - 如何在 Springs MVC 4 中使用 HikariCP 和 JdbcTemplate

标签 spring spring-mvc jdbctemplate hikaricp

我是 Springs MVC 的新手,我尝试将 HikariCP 与 JdbcTemplate 集成。但是在如何将 JdbcTemplate 指向 HikariCP DataSource 方面得到了很多帮助。

在 applicationContext.xml 中

<bean id="dataSource" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName"       value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
<property name="dataSourceProperties">
    <props>
        <prop key="url">${jdbc.url}</prop>
        <prop key="user">${jdbc.username}</prop>
        <prop key="password">${jdbc.password}</prop>
    </props>
</property>
</bean>

 <bean id="jdbcTemplate" class="com.zaxxer.hikari.HikariDataSource" destroy- method="close">
     <property name="dataSource" ref="dataSource"></property>
 </bean>

在 Controller 中:

Controller 公共(public)类 HandleWareHouse{

private DataSource dataSource;
private JdbcTemplate jdbcTemplate;


public void setDataSource(DataSource dataSource){
    this.dataSource = dataSource;
}


@RequestMapping(value="/saveProduct_categories", method = RequestMethod.POST)
@ResponseBody
public String insertPc(@RequestParam Map<String,String> requestParams){
    jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "INSERT INTO product_categories(name,code,des,grp_name) VALUES(?,?,?,?)";
    jdbcTemplate.update(sql, new Object[] {requestParams.get("pname"),requestParams.get("pcode"),requestParams.get("stext"),requestParams.get("pcategory")});
    return "sucess";
   }

 }

但是报错如下:

  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplate' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.zaxxer.hikari.HikariConfig' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.zaxxer.hikari.HikariConfig] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found

最佳答案

我已经使用 spring javaconfig 配置数据源并获取 jdbctemplate。我认为这是最好的

@Bean
    public DataSource getDataSource() {

        private HikariDataSource dataSource() {
           final HikariDataSource ds = new HikariDataSource();
           ds.setMaximumPoolSize(100);
           ds.setDriverClassName("oracle.jdbc.driver.OracleDriver"); 
           ds.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:XE"); ;
           ds.setUsername("username");
           ds.setPassword("password");
           return ds;
        }

    }

    @Bean
    public JdbcTemplate getJdbcTemplate() {

        return new JdbcTemplate(getDataSource());
    }

我希望这对你有用。

关于spring - 如何在 Springs MVC 4 中使用 HikariCP 和 JdbcTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161438/

相关文章:

java - 存储函数 - 发送/接收 boolean 值 - BD

spring-mvc - 从 DB 获取 Blob 抛出 "invalid transaction handle"

java - Spring 启动 : Issue when importing spring-data-jdbc and spring-data-cassandra together within same project

spring - spring 5 中的 ContextSingletonBeanFactoryLocator 替代方案

Spring - 如何保护 RESTful 私有(private)资源?

java - Spring 启动: configure hsqldb datasource to persist data

java - 当 WebMvcAutoConfiguration$EnableWebMvcConfiguration 只需要一个 entityManager 时如何在 Spring Boot 中进行 Multi-Tenancy

java - Spring中路径的限制

Spring MVC + 磁贴 : integration testing

java - 如何从结果集中获取单行