SSH

Spring整合Hibernate实践

一、配置hibernate

1)'hibernate.dialect' must be set when no Connection available

数据库的方言没有配置


2)Connections could not be acquired from the underlying database
原因是配置有问题的
参考这篇文章 
以上问题可能有一下原因造成:
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没有远程访问的权限,需要增加权限,增加权限的步骤如下:
进入mysql数据库:
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
flush privileges;


3)需要之得注意的是:配置文件:   hibernate.cfg.cml  和映射文件:  XXX.hbm.xml






二、配置spring     【保罗万象,海纳百川】
  bean 文件
测试 (为了测试方便使用javabean)


在测试的时候一定要注意点: BeanFactory not initialized or already closed - call 'refresh' before access
 


ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");// 加上自己配置的xml,不然的话回去你的本地找

Spring实例化BeanFactory的时候是默认到classPath下面查找名为applicationContext.xml的文件


三、spring整合hibernate

1 配置SessionFactory   javabean

2.配置事务管理器   (把hibernate的事务控制交给spring)【transaction】



1)配置数据库 c3p0  配置写  class的时候 在java文件中写一个 ComboPooledDataSource  然后截取相关的

在bean.xml下的配置:

1.<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        //不太好:<property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>

       <property name="jdbcUrl"  value="jdbc:mysql://localhost:3306/数据库名字?useUnicode=true&amp;characterEncoding=utf8" ></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxStatements" value="100"></property>
        <property name="acquireIncrement" value="2"></property>
    </bean>

2.

2)

需要配置的东西总结如下:

<!-- 配置C3P0数据库 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>

<property name="initialPoolSize" value="5"></property>
<property name="maxPoolSize" value="30"></property>
<property name="minPoolSize" value="3"></property>
  
  
</bean>
<!-- SessionFactory -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="configLocation" value="classpath:/hibernate.cfg.xml"></property>
   <property name="dataSource" ref="dataSource"></property>


</bean>
<!--配置事务管理器  -->
<bean name="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory"  ref="sessionFactory"></property>
 
</bean>
<!--配偶AOP事务管理  -->  
<!-- 配置aop事务管理 -->
<tx:advice id="transactionAdvice"  transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="remove*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="query*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="list*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="alter*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>         
<!-- 配置切入点,织入增强功能 -->
<aop:config>
<aop:pointcut  id="transactionPointcut" expression="execution( *  elec.service.*.*(..))" />
<aop:advisor advice-ref="transactionAdvice"  pointcut-ref="transactionPointcut" />
</aop:config>
 
<!-- 配置Hibernate模板 bean -->
<bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> 

今天在配置hibernate的时候总是出现      :Connections could not be acquired from the underlying database!  未解决啊?



在配置hibernate的时候和spring结合的时候需要加上这个属性

 <mapping resource="elec/domain/ElecText.hbm.xml"/> 自己配置的映射文件


3)关于dao与dao.impl的设计

为什用接口设计的方式?



4)service 不使用接口设计模式



5)引入struts2,struts2与spring的结合,1

1、先在web.xml文件中引入struts2 的过滤器与拦截器

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/beans.xml</param-value>  // 这个是与spring的结合需要登记一下不然结不了婚
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值