第一种方式:
hiberante.cfg.xml配置如下:
- <!DOCTYPEhibernate-configurationPUBLIC
- "-//Hibernate/HibernateConfigurationDTD3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <propertyname="hibernate.hbm2ddl.auto">update</property>
- <propertyname="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <propertyname="hibernate.show_sql">true</property>
- <mappingresource="com/oristand/hibernate/pojo/User.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
接着配置applicationContext-hibernate.xml
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <beanid="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <propertyname="driverClassName">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <propertyname="url">
- <value>jdbc:mysql://localhost:3306/ssh</value>
- </property>
- <propertyname="username">
- <value>root</value>
- </property>
- <propertyname="password">
- <value>123456</value>
- </property>
- </bean>
- <beanid="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <propertyname="dataSource">
- <reflocal="dataSource"/>
- </property>
- <propertyname="configLocation">
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- </bean>
- </beans>
第二种方式,不要hiberante.cft.xml,直接配置;
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/langhttp://www.springframework.org/schema/lang/spring-lang-2.5.xsd
- http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.5.xsd">
- <context:property-placeholderlocation="db.properties"></context:property-placeholder>
- <context:annotation-config/>
- <beanname="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <propertyname="driverClassName"value="${driverClassName}"></property>
- <propertyname="url"value="${url}"/>
- <propertyname="username"value="${username}"/>
- <propertyname="password"value="${password}"/>
- </bean>
- <beanname="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <propertyname="dataSource"ref="dataSource"/>
- <propertyname="hibernateProperties">
- <props>
- <propkey="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </prop>
- <propkey="hibernate.show_sql">true</prop>
- <propkey="hibernate.hbm2ddl.auto">create-drop</prop>
- </props>
- </property>
- <propertyname="mappingResources">
- <list>
- <value>cn/iceway/entity/myuser.hbm.xml</value>
- </list>
- </property>
- </bean>
- <beanname="baseHibernateDAO"class="cn.iceway.BaseHibernateDAO">
- <propertyname="sessionFactory"ref="sessionFactory"></property>
- </bean>
- </beans>
倘若实体类使用了annotation,则需要对sessionFacotory做写改动:
- <beanname="sessionFactory"
- class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <propertyname="dataSource"ref="dataSource"></property>
- <propertyname="hibernateProperties">
- <props>
- <propkey="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </prop>
- <propkey="hibernate.show_sql">true</prop>
- <propkey="hibernate.hbm2ddl.auto">create</prop>
- </props>
- </property>
- <propertyname="packagesToScan">//指明查找的annotatedclass路径,User类的路径为cn.iceway.entity.User
- <value>cn.iceway.entity</value>
- </property>
- </bean>