`

spring + hibernate3

阅读更多

spring+hibernate3:

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:webflow="http://www.springframework.org/schema/webflow-config"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        "
	default-lazy-init="false"  
	default-autowire="no">

	<!-- open annotation  -->
	<context:annotation-config />
	<!-- where to scan  -->
	<context:component-scan base-package="com.xxx.ssh" />
	<!-- PropertyPlaceholderConfigurer -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                <value>/WEB-INF/jdbc.properties</value>  
            </list>  
        </property>  
    </bean>  
	<!-- c3p0 dataSource -->	
	<bean id="dataSource" class="com.mchange.v2.c3p0. ComboPooledDataSource" destroy-method="close" >
		<!-- 指定连接数据库的驱动 -->   
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>   
        <!-- 指定连接数据库的URL -->   
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sshdb"/>   
        <!-- 指定连接数据库的用户名 -->   
        <property name="user" value="root"/>   
        <!-- 指定连接数据库的密码 -->   
        <property name="password" value="123"/>   
        <!-- 指定连接数据库连接池的最大连接数 -->   
        <property name="maxPoolSize" value="40"/>   
        <!-- 指定连接数据库连接池的最小连接数 -->   
        <property name="minPoolSize" value="1"/>   
        <!-- 指定连接数据库连接池的初始化连接数 -->   
        <property name="initialPoolSize" value="1"/>   
        <!-- 指定连接数据库连接池的连接最大空闲时间 -->   
        <property name="maxIdleTime" value="20"/>   
	</bean>
	<!-- dbcp dataSource -->
	<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/sshdb" />
		<property name="username" value="root" />
		<property name="password" value="123" />
		<!-- 最大连接数据库连接数,设置为0时,表示没有限制  -->
		<property name="maxActive" value="" />
		<!-- 最大等待连接中的数量,设置为0时,表示没有限制 -->
		<property name="maxIdle" value="" />
		<!-- 最大等待秒数,单位为毫秒, 超过时间会报出错误信息 -->
		<property name="maxWait" value="" />
		<!--  设置从数据源中返回的连接是否采用自动提交机制,默认值为 true -->
		<property name="defaultAutoCommit" value="false" />	
		<!-- 用于验证连接是否成功的查询SQL语句,SQL语句必须至少要返回一行数据, 如你可以简单地设置为:“select xxx ” -->
		<property name="validationQuery" value="select xxx" />
		<property name="testOnBorrow" value="true" />
		<!-- 数据源是否仅能执行只读操作, 默认值为 false  -->
		<property name="defaultReadOnly" value="false" />
	</bean>
	<!-- JNDI dataSource -->
	<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" >
		<property name="jndiName">
			<!-- 指定数据源的JNDI -->
			<value>java:com/env/jdbc/myds</value>
		</property>
	</bean>
	
	<!-- sessionFactory -->   
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
		<property name="dataSource" ref="dataSource" />
		<property name="mappingResources">
			<list>
				<!-- 以下以value标签形式列出所有的PO映射文件 -->
				<value>config/User.hbm.xml</value>
			</list>
		</property>
		<!-- SessionFactory Properties -->
		<property name="hibernateProperties">
			<props>
				<!-- 指定Hibernate的连接方言 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="show_sql">true</prop>
				<prop key="format_sql">true</prop>
				<!-- 配置启动应用时,是否根据Hibernate映射自动创建数据表 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>
	<!-- hibernate3 annotation sessionfactory -->
	<bean id="annotationSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="packagesToScan" value="com.xxx.ssh.model"></property>
		<!--  
		<property name="mappingLocations">
			<list>
				<value>classpath:hibernateOrm/*.hbm.xml</value>	
			</list>
		</property>
		<property name="annotatedClasses" value="com.xxx.ssh.model.User" />
		-->
	</bean>
	
	<!-- 声明式事务管理 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:advice id="txAdvice"  transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="update*" propagation="REQUIRED" />	<!-- add、update、delte开头的方法事物传播特性 -->
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
<aop:config>
		<aop:pointcut id="interceptorPointCuts" expression="execution(* com.xxx.ssh.service.impl.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
	</aop:config>
	<!-- HibernateTemplate -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
<bean id="userDao" class="com.xxx.ssh.dao.impl.UserDao" >
	 	<property name="hibernateTemplate" ref="hibernateTemplate" />
	 </bean>
	
	<import resource="applicationContext-module.xml"/>
</beans>

 

另做一些补充:

一、事物传播特性PROPAGATION:

    1.PROPAGATION_REQUIRED :如果当前没有事物,新建一个,如果当前有事物,加入到这个事物中,此种最常见

    2.PROPAGATION_SUPPORTS :支持当前事物,如果当前没有事物,就以非实物方式执行

    3.PROPAGATION_MANDATORY :使用当前事物,如果当前没有事物,则抛出异常

    4.PROPAGATION_REQUIRES_NEW :新建事物,如果当前存在事物,则把当前事务挂起

    5.PROPAGATION_NOT_SUPPORTED :以非事务方式执行,如果存在事物,在挂起

    6.PROPAGATION_NEVER :以非事务方式执行,如果存在事物,则抛出异常

    7.PROPAGATION_NESTED :如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与 PROPAGATION_REQUIRED 类似的操作

二、事物隔离级别ISOLATION:

    1.ISOLATION_DEFAULT:这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别.

    2.ISOLATION_READ_UNCOMMITTED:这是事务最低的隔离级别,它充许令外一个事务可以看到这个事务未提交的数据.【这种隔离级别会产生脏读,不可重复读和幻像读】

    3.ISOLATION_READ_COMMITTED:保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据

    4.ISOLATION_REPEATABLE_READ:这种事务隔离级别可以防止脏读,不可重复读。【但是可能出现幻像读.】

它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。

    5.ISOLATION_SERIALIZABLE:这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。【除了防止脏读,不可重复读外,还避免了幻像读】

三、涉及到的概念:

    1.脏读:指当一个事务正在访问数据,并且对数据进行了修改,而这种修改还没有提交到数据库中,这时,另外一个事务也访问这个数据,然后使用了这个数据。

因为这个数据是还没有提交的数据, 那么另外一 个事务读到的这个数据是脏数据,依据脏数据所做的操作可能是不正确的。

    2.幻读:指当事务不是独立执行时发生的一种现象,例如第一个事务对一个表中的数据进行了修改,这种修改涉及 到表中的全部数据行。同时,第二个事务也修改

这个表中的数据,这种修改是向表中插入一行新数据。那么,以后就会发生操作第一个事务的用户发现表中还有没有修改的数据行,就好象发生了幻觉一样。

    3.不可重复读:指在一个事务内,多次读同一数据。在这个事务还没有结束时,另外一个事务也访问该同一数据。 那么,在第一个事务中的两次读数据之间,由于第二

个事务的修改,那么第一个事务两次读到的数据可能是不一样的。这样就发生了在一个事务内两次读到的数据是不一样的,因此称为是不可重复读。

 

四、关于HibernateTemplate:

如何获取hibernateTemplate:

继承HibernateDaoSupport,使用getHibernateTemplate()

hibernateTemplate提供的方法:

1. void delete(Object entity),删除指定持久化实例。

2. deleteAll(Collection entities),删除集合内全部持久化类实例。

3. find(String queryString),根据HQL查询字符串来返回实例集合。

4. findByNamedQuery(String queryName),根据命名查询返回实例集合。

5. get(Class entityClass, int id),根据主键加载特定持久化类的实例。

6. save(Object entity),保存新的实例。

7. saveOrUpdate(Object entity),根据实例状态,选择保存或者更新。

8. update(Object entity),更新实例的状态,要求entity是持久状态。

9. setFirstResult(int first) 设置offset。

10.setMaxResults(int maxResults),设置分页的大小

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics