Spring5 + hibernate + jpa + hikaricp xml配置出错

时间:2018-03-24 08:06:22

标签: spring hibernate hikaricp

请帮忙。以下是我的XML配置文件。我的应用运行正常。但它在调用数据库时说没有选择数据库

例外情况如下:

2018-03-24 13:44:17 DEBUG SqlExceptionHelper:139 - could not extract ResultSet [n/a]
java.sql.SQLException: No database selected
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)



<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
   	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc.xsd
   	http://www.springframework.org/schema/beans     
   	http://www.springframework.org/schema/beans/spring-beans.xsd
   	http://www.springframework.org/schema/context 
   	http://www.springframework.org/schema/context/spring-context.xsd
   	http://www.springframework.org/schema/tx 
   	http://www.springframework.org/schema/tx/spring-tx.xsd
   	http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

	<mvc:annotation-driven />
	<context:component-scan base-package="app.sphi" />

	<mvc:resources mapping="/res/**" location="/WEB-INF/res/" />
	
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>
	

	<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
		
		<property name="poolName" value="springHikariCP" />
		<property name="connectionTestQuery" value="SELECT 1" />
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb?useUnicode=true" />
		<property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
		<property name="username" value="root" />
		<property name="password" value="" />
				
	</bean>

	<!-- Create default configuration for Hibernate -->
	<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
		
	</bean>

	<!-- Configure the entity manager factory bean -->
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
		
		<!-- Set JPA properties -->
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>				
				<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
			</props>
		</property>
		<!-- Set base package of your entities -->
		<property name="packagesToScan" value="app.sphi.model" />
		<!-- Set share cache mode -->
		<property name="sharedCacheMode" value="ENABLE_SELECTIVE" />
		<!-- Set validation mode -->
		<property name="validationMode" value="NONE" />
	</bean>

	<!-- Configure the transaction manager bean -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>

	<!-- Enable annotation driven transaction management -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	<!-- Configure Spring Data JPA and set the base package of the repository interfaces -->
	<jpa:repositories base-package="app.sphi.repo"
		transaction-manager-ref="transactionManager"
		entity-manager-factory-ref="entityManagerFactory" />
	
</beans>
&#13;
&#13;
&#13;

我会很高兴任何建议。

1 个答案:

答案 0 :(得分:1)

不要配置 jdbcUrl dataSourceClassName,选择一种风格或另一种风格。在这种情况下,我建议您坚持使用jdbcUrl