spring+hibernate的配置

第一种方式:

hiberante.cfg.xml配置如下:

Xml代码 复制代码
  1. <!DOCTYPE hibernate-configuration PUBLIC   
  2.  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"   
  3.  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  4. <hibernate-configuration>  
  5.  <session-factory>  
  6.   <property name="hibernate.hbm2ddl.auto">update</property>  
  7.   <property name="hibernate.dialect">  
  8.    org.hibernate.dialect.MySQLDialect   
  9.   </property>  
  10.   <property name="hibernate.show_sql">true</property>  
  11.   <mapping resource="com/oristand/hibernate/pojo/User.hbm.xml" />  
  12.  </session-factory>  
  13. </hibernate-configuration>  

 接着配置applicationContext-hibernate.xml

Xml代码 复制代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.  xmlns:aop="http://www.springframework.org/schema/aop"  
  4.  xmlns:tx="http://www.springframework.org/schema/tx"  
  5.  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
  6.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
  7.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  8.  <bean id="dataSource"  
  9.   class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  10.   <property name="driverClassName">  
  11.    <value>com.mysql.jdbc.Driver</value>  
  12.   </property>  
  13.   <property name="url">  
  14.    <value>jdbc:mysql://localhost:3306/ssh</value>  
  15.   </property>  
  16.   <property name="username">  
  17.    <value>root</value>  
  18.   </property>  
  19.   <property name="password">  
  20.    <value>123456</value>  
  21.   </property>  
  22.  </bean>  
  23.   
  24.  <bean id="sessionFactory"  
  25.   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  26.   <property name="dataSource">  
  27.    <ref local="dataSource" />  
  28.   </property>  
  29.   <property name="configLocation">  
  30.        <value>classpath:hibernate.cfg.xml</value>  
  31.   </property>  
  32.   
  33.  </bean>  
  34. </beans>  

 第二种方式,不要hiberante.cft.xml,直接配置;

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xmlns:context="http://www.springframework.org/schema/context"  
  5.         xsi:schemaLocation="http://www.springframework.org/schema/beans            
  6.          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  7.          http://www.springframework.org/schema/context    
  8.          http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  9.           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd   
  10.        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  11.        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">  
  12.         <context:property-placeholder location="db.properties"></context:property-placeholder>  
  13.         <context:annotation-config />  
  14.         <bean name="dataSource"  
  15.                 class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  16.                 <property name="driverClassName" value="${driverClassName}" ></property>  
  17.                 <property name="url" value="${url}" />  
  18.                 <property name="username" value="${username}" />  
  19.                 <property name="password" value="${password}" />  
  20.         </bean>  
  21.         <bean name="sessionFactory"  
  22.                 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  23.                 <property name="dataSource" ref="dataSource" />  
  24.                 <property name="hibernateProperties">  
  25.                         <props>  
  26.                                 <prop key="hibernate.dialect">  
  27.                                         org.hibernate.dialect.MySQLDialect   
  28.                                 </prop>  
  29.                                 <prop key="hibernate.show_sql">true</prop>  
  30.                                 <prop key="hibernate.hbm2ddl.auto">create-drop</prop>  
  31.                         </props>  
  32.                 </property>  
  33.                 <property name="mappingResources">  
  34.                         <list>  
  35.                                 <value>cn/iceway/entity/myuser.hbm.xml</value>  
  36.                         </list>  
  37.                 </property>  
  38.         </bean>  
  39.         <bean name="baseHibernateDAO" class="cn.iceway.BaseHibernateDAO">  
  40.                 <property name="sessionFactory" ref="sessionFactory"></property>  
  41.         </bean>  
  42. </beans>  

 倘若实体类使用了annotation,则需要对sessionFacotory做写改动:

Xml代码 复制代码
  1. <bean name="sessionFactory"  
  2.           class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  3.           <property name="dataSource" ref="dataSource"></property>  
  4.           <property name="hibernateProperties">  
  5.                   <props>  
  6.                           <prop key="hibernate.dialect">  
  7.                                   org.hibernate.dialect.MySQLDialect   
  8.                           </prop>  
  9.                           <prop key="hibernate.show_sql">true</prop>  
  10.                           <prop key="hibernate.hbm2ddl.auto">create</prop>  
  11.                   </props>  
  12.           </property>  
  13.           <property name="packagesToScan">  //指明查找的annotated class路径,User类的路径为cn.iceway.entity.User   
  14.           <value>cn.iceway.entity</value>  
  15.           </property>  
  16.   </bean>  

转自: http://jianchen.javaeye.com/?show_full=true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值