日期:2014-05-19  浏览次数:20655 次

J2EE SSH2架构(3)

第三个架构,与第二个相似性很大,唯一不同的是采用的是hibernate.cfg.xml配置文件,如下:

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="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"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 采用配置文件形式配置数据源和sessionfactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hiberante.cfg.xml</value>
		</property>
	</bean>
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
    <!-- 采用AOP(面向方面编程)方式配置事物 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice>

	<!-- 哪些类的哪些方法参与事物, (* com.evan.crm.service.*.*(..))中几个通配符的含义:
		第一个 * —— 通配 任意返回值类型
		第二个 * —— 通配 包com.evan.crm.service下的任意class
		第三个 * —— 通配 包com.evan.crm.service下的任意class的任意方法
		第四个 .. —— 通配 方法可以有0个或多个参数
		综上:包com.evan.crm.service下的任意class的具有任意返回值类型、任意数目参数和任意名称的方法-->
	<aop:config>
		<aop:pointcut id="allManagerMethod"
			expression="execution(* com.bing.dao.*.*(..))" />
		<aop:advisor pointcut-ref="allManagerMethod"
			advice-ref="txAdvice" />
	</aop:config>
	<!-- Clob 类型处理配置 BEGIN -->
	<bean id="defaultLobHandler" lazy-init="true"
		class="org.springframework.jdbc.support.lob.DefaultLobHandler" />

	<!-- hibernate 基本配置  
	<bean id="hibernateProperties"
		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="properties">
			<props>
				<prop key="hibernate.connection.charSet">UTF-8</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>
	-->
</beans>

?

hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- Database connection settings -->
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/ssh3
		</property>
		<property name="connection.username">root</property>
		<property name="connection.password">123456</property>
		<!-- 数据库言 -->
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="show_sql">true</property>
		<property name="hbm2ddl.auto">update</property>
		<mapping cla