SSH学習記録(6)-統合Hibernate


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メソッドを追加します.スプリングで構成します.

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のsaveStudioメソッドを追加し、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>	

义齿

	<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/