SSH学习记要(6)-整合Hibernate
SSH学习记录(6)-整合Hibernate
Spring 中整合Hibernate
1. Spring中加入一个DataSource和一个SessionFactory,搞定。
2. 现在还没用上,在Logic中加一个SessionFactory的变量和get,set方法。在spring中配置。
3. Student类和相应的映射文件Student.hbm.xml.(同5中的一样)
4. 在bean sessionFactory中配置对映射文件的引用。
5. 在Action中添加个hbTest方法调用logic的saveStudent方法,在页面添加个提交到Action的hbTest方法的按钮,测试一下,成功。
附加1:
可以将数据库的相关信息放到一个database.properties文件中统一管理,在spring中引用这个文件。方便数据库切换。
database.properties内容:
spring中加入:
引用方法:
附加2: 一个典型的事务AOP的配置,以后用到
spring 的注解sessionFactory
附加3:
当 Hibernate 遇上 Spring http://www.ibm.com/developerworks/cn/java/wa-spring2/
Spring 中整合Hibernate
1. Spring中加入一个DataSource和一个SessionFactory,搞定。
<!-- dbcpDataSource ========================================================================== --> <bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="123456"/> <property name="maxActive" value="1000"/> <property name="maxIdle" value="5000"/> <property name="minIdle" value="5"/> <property name="removeAbandoned" value="true"/> <property name="removeAbandonedTimeout" value="180"/> <property name="maxWait" value="9000"/> </bean> <!-- sessionFactory ================================================================== --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dbcpDataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
2. 现在还没用上,在Logic中加一个SessionFactory的变量和get,set方法。在spring中配置。
package com.zhch.logic; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.learn.hibernate.dto.Student; public class AOPLogicImpl implements AOPLogic{ private SessionFactory sessionFactory; public String getWord(){ return "I am aopLogicImpl."; } public void print(){ System.out.println("aopLogicImpl print"); } public void saveStudent(){ try { Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for (int i = 0; i < 20; i++) { Student customer = new Student(); customer.setAge(20); customer.setName("Li2"); customer.set_class("three"); session.save(customer); } tx.commit(); session.close(); } catch (HibernateException e) { e.printStackTrace(); } } /** * @return the sessionFactory */ public SessionFactory getSessionFactory() { return sessionFactory; } /** * @param sessionFactory the sessionFactory to set */ public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
<!-- hbAction --> <bean id="hbAction" class="com.zhch.action.AOPAction"> <property name="logic" ref="hbService"/> </bean> <bean id="hbService" class="com.zhch.logic.AOPLogicImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
3. Student类和相应的映射文件Student.hbm.xml.(同5中的一样)
4. 在bean sessionFactory中配置对映射文件的引用。
<!-- sessionFactory ================================================================== --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 其它的内容 --> <property name="mappingResources"> <list> <value>com/zhch/db/dto/Student.hbm.xml</value> </list> </property> </bean>
5. 在Action中添加个hbTest方法调用logic的saveStudent方法,在页面添加个提交到Action的hbTest方法的按钮,测试一下,成功。
附加1:
可以将数据库的相关信息放到一个database.properties文件中统一管理,在spring中引用这个文件。方便数据库切换。
database.properties内容:
database.connection.driver_class=com.mysql.jdbc.Driver database.connection.url=jdbc:mysql://localhost:3306/test database.connection.username=root database.connection.password=123456 database.connection.dialect=org.hibernate.dialect.MySQLDialect
spring中加入:
<!-- propertyConfigurer ================================================================ --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>/WEB-INF/database.properties</value> </list> </property> </bean>
引用方法:
<property name="driverClassName" value="${database.connection.driver_class}"/> <property name="url" value="${database.connection.url}"/> <property name="username" value="${database.connection.username}"/>
附加2: 一个典型的事务AOP的配置,以后用到
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="baseServiceproxyBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true"> <property name="transactionManager"> <ref local="transactionManager"/> </property> <property name="transactionAttributes"> <props> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="InitModePopedomInfo">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean>
spring 的注解sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--<property name="mappingLocations" value="classpath*:com/creawor/demo/model/*.hbm.xml"/>--> <!-- packagesToScan可以自动搜索某个package的全部标记@Entity class --> <property name="packagesToScan"> <list> <value>com.**.model</value> </list> </property> <property name="hibernateProperties"> <props> <!--常用数据库方言 MySQL5Dialect,SQLServerDialect,OracleDialect,SybaseDialect,DB2Dialect --> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.query.substitutions">true 1, false 0</prop> <prop key="hibernate.default_batch_fetch_size">4</prop> </props> </property> </bean>
附加3:
当 Hibernate 遇上 Spring http://www.ibm.com/developerworks/cn/java/wa-spring2/