Ich bin neu bei Springs MVC und ich habe versucht, HikariCP mit JdbcTemplate zu integrieren. Aber viele Fehler haben keine Hilfe, wie man JdbcTemplate auf HikariCP DataSource zeigt.Verwendung von HikariCP mit JdbcTemplate in Federn MVC 4
in 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>
Und in der Steuerung:
-Controller public class 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";
}
}
Aber Fehler bekommen, wie unten gezeigt:
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
Folgen Sie der Anleitung hier http://frameworkonly.com/hikaricp-connection-pooling-in-spring-hibernate-jpa/ – Nitin