代码之家  ›  专栏  ›  技术社区  ›  Mashukur Rahman

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

  •  0
  • Mashukur Rahman  · 技术社区  · 6 年前

    请帮忙。下面是我的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>

    我很乐意接受任何建议。

    1 回复  |  直到 6 年前
        1
  •  1
  •   brettw    6 年前

    不配置 二者都 jdbcUrl dataSourceClassName ,选择一种样式或另一种样式。在这种情况下,我建议你坚持 jdbcUrl