Spring + hibernate5 搭建web配置事务处理

方法一:
Spring配置文件中添加:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>META-INF/hibernate.propertiesvalue>     //hibernate的配置文件
        property>
    bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="annotatedClasses">
            <list>
                <value>包名.Personvalue>
            list>
        property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}prop>                
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContextprop>        **没有配置报createSQLQuery is not valid without active transaction错误**
            props>
        property>
    bean>
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"/>
    bean>
    <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    bean>

web.xml文件中添加:

<context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:springApplicationContext.xmlparam-value>
    context-param>
    **必须配置在struts的前面 不添加报Could not obtain transaction-synchronized Session for current thread错误**
    <filter>   
        <filter-name>SpringOpenSessionInViewFilterfilter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>SpringOpenSessionInViewFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    
    <filter>
        <filter-name>struts2filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>struts2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>  

方式二:
Spring的配置文件添加如方法一,web.xml中添加

param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:springApplicationContext.xmlparam-value>
    param>

在Service层(业务逻辑层)使用@Service和@Transactional注解

@Service
public class PersonServiceImp<T extends Person> extends ServiceImp<T> implements IPersonService<T> {
    @Transactional
    @Override
    public T findPersonByAccount(String account) {
        return null;
    }
}

你可能感兴趣的:(web-java)