spring+hibernate的配置

第一种方式:

hiberante.cfg.xml配置如下:

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

接着配置applicationContext-hibernate.xml

Xml代码 复制代码
  1. <beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  6. http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  7. http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  8. <beanid="dataSource"
  9. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  10. <propertyname="driverClassName">
  11. <value>com.mysql.jdbc.Driver</value>
  12. </property>
  13. <propertyname="url">
  14. <value>jdbc:mysql://localhost:3306/ssh</value>
  15. </property>
  16. <propertyname="username">
  17. <value>root</value>
  18. </property>
  19. <propertyname="password">
  20. <value>123456</value>
  21. </property>
  22. </bean>
  23. <beanid="sessionFactory"
  24. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  25. <propertyname="dataSource">
  26. <reflocal="dataSource"/>
  27. </property>
  28. <propertyname="configLocation">
  29. <value>classpath:hibernate.cfg.xml</value>
  30. </property>
  31. </bean>
  32. </beans>

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

Xml代码 复制代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="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/langhttp://www.springframework.org/schema/lang/spring-lang-2.5.xsd
  10. http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  11. http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.5.xsd">
  12. <context:property-placeholderlocation="db.properties"></context:property-placeholder>
  13. <context:annotation-config/>
  14. <beanname="dataSource"
  15. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  16. <propertyname="driverClassName"value="${driverClassName}"></property>
  17. <propertyname="url"value="${url}"/>
  18. <propertyname="username"value="${username}"/>
  19. <propertyname="password"value="${password}"/>
  20. </bean>
  21. <beanname="sessionFactory"
  22. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  23. <propertyname="dataSource"ref="dataSource"/>
  24. <propertyname="hibernateProperties">
  25. <props>
  26. <propkey="hibernate.dialect">
  27. org.hibernate.dialect.MySQLDialect
  28. </prop>
  29. <propkey="hibernate.show_sql">true</prop>
  30. <propkey="hibernate.hbm2ddl.auto">create-drop</prop>
  31. </props>
  32. </property>
  33. <propertyname="mappingResources">
  34. <list>
  35. <value>cn/iceway/entity/myuser.hbm.xml</value>
  36. </list>
  37. </property>
  38. </bean>
  39. <beanname="baseHibernateDAO"class="cn.iceway.BaseHibernateDAO">
  40. <propertyname="sessionFactory"ref="sessionFactory"></property>
  41. </bean>
  42. </beans>

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

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

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

你可能感兴趣的:(spring,AOP,bean,Hibernate,xml)