spring hibernate c3p0 整合连接不上数据库

4 篇文章 0 订阅
2 篇文章 0 订阅

可能原因

1,驱动配置有误:driver=com.mysql.jdbc.Driver
2,数据库连接地址有误:url=jdbc:mysql://localhost:3306/test?3useUnicode=true&characterEncoding=utf8
3,密码或帐号有误:username=root
      password=root

4,数据库未启动或无权访问

5,项目未引入对应的驱动jar包mysql-connector-java-5.1.6-bin.jar

6,mysql root没有远程访问的权限,需要增加权限

检查连接配置:db.properties

#data
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名称?characterEncoding=utf8
jdbc.username=用户名
jdbc.password=密码

# c3p0
c3p0.minPoolSize=3
c3p0.maxPoolSize=50
c3p0.initialPoolSize=5
c3p0.maxIdleTime=120
c3p0.acquireIncrement=5
c3p0.idleConnectionTestPeriod=60

#hibernate

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update
hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
hibernate.connection.autocommit=true
hibernate.javax.persistence.validation.mode=none

注意:每一行后面不可以有空格

检查spring配置信息如下:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:db/*.properties</value>
			</list>
		</property>
	</bean>
	<!-- c3p0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClassName}"/>  
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>  
        <property name="password" value="${jdbc.password}"/>
        <property name="minPoolSize" value="${c3p0.minPoolSize}"/>  
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>  
        <property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>  
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>  
        <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>  
        
        <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>  
	</bean>
	<!-- 配置sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hiberate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
				<prop key="hiberate.javax.persistence.validation.mode">${hibernate.javax.persistence.validation.mode}</prop>
			</props>
		</property>
		<!-- 扫描实体类包 -->
		<property name="packagesToScan" value="com.evaluationmanager.entiy"></property>
	</bean>
	
	<!-- 配置hibernatTemplate 模板 -->
	 <bean id="hibernatTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
	 	<property name="sessionFactory" ref="sessionFactory"></property>
	 </bean>
	 <!-- 配置事务管理 --> 
	 <!-- transationManager -->
	 <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
	 	<property name="sessionFactory" ref="sessionFactory"></property>
	 </bean>
	 <!-- 开启事务注解 -->
	 <tx:annotation-driven transaction-manager="transactionManager"/>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值