Spring 自动事务管理配置文件

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rolemanager.service;

import java.io.Serializable;
import java.util.Collection;
import org.springframework.dao.DataAccessException;

/**
 *
 * @author liuqing
 */
public interface IBaseService<T extends Serializable> {

    public T queryById(int id) throws DataAccessException;

    public Collection<T> queryAll() throws DataAccessException;

    public void add(T t) throws DataAccessException;

    public void remove(T t) throws DataAccessException;

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rolemanager.service.impl;

import com.rolemanager.dao.IBaseDao;
import com.rolemanager.service.IBaseService;
import java.io.Serializable;
import java.util.Collection;
import org.springframework.dao.DataAccessException;

/**
 *
 * @author liuqing
 */
public class BaseServiceImpl<T extends Serializable> implements IBaseService<T> {

    private IBaseDao<T>  baseDao;

    public T queryById(int id) throws DataAccessException {
        return this.baseDao.queryById(id);
    }

    public Collection<T> queryAll() throws DataAccessException {
        return this.baseDao.queryAll();
    }

    public void add(T t) throws DataAccessException {
        this.baseDao.add(t);
    }

    public void remove(T t) throws DataAccessException {
        this.baseDao.remove(t);
    }

    public IBaseDao<T> getBaseDao() {
        return baseDao;
    }

    public void setBaseDao(IBaseDao<T> baseDao) {
        this.baseDao = baseDao;
    }




}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rolemanager.service;

import com.rolemanager.GroupInfo;

/**
 *
 * @author liuqing
 */
public interface IGroupInfoService extends IBaseService<GroupInfo> {

}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rolemanager.service.impl;

import com.rolemanager.GroupInfo;
import com.rolemanager.service.IGroupInfoService;

/**
 *
 * @author liuqing
 */
public class GroupInfoServiceImpl extends BaseServiceImpl<GroupInfo> implements IGroupInfoService{

    public String queryName() {
        return this.getBaseDao().queryById(2).getUserInfoes().iterator().next().getPassword();
    }

}


 web.xml 文件

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
			classpath:/com/rolemanager/conf/applicationContext-*.xml
		</param-value>
    </context-param>
    <filter>
        <filter-name>login</filter-name>
        <filter-class>com.baseaction.LoginFilter</filter-class>
    </filter>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--Struts Filter-->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>com.rolemanager.Ng2Filter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>login</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

  applicationContext-app 项目核心文件

 

  

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <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.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
        <property name="configLocation">
            <value>
                classpath:hibernate.cfg.xml
            </value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- 配置事务拦截器 -->
    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
     <!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
        <property name="transactionManager" ref="transactionManager" />
        <property name="transactionAttributes">
          <!-- 下面定义事务传播属性-->
            <props>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <!-- 定义BeanNameAutoProxyCreator-->
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
     <!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
        <property name="beanNames">
            <!-- 下面是所有需要自动创建事务代理的bean-->
            <list>
                <value>*Service</value>
            </list>
            <!-- 此处可增加其他需要自动创建事务代理的bean-->
        </property>
        <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
        <property name="interceptorNames">
            <list>
                <!-- 此处可增加其他新的Interceptor ,下面的拦截器仅用于生成 事务代理-->
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean>


    

</beans>

 

  applicationContext-dao Dao层

  

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
    
    <bean id="userInfoDao" class="com.rolemanager.dao.impl.UserInfoDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="groupInfoDao" parent="userInfoDao" class="com.rolemanager.dao.impl.GroupInfoDaoImpl">
    </bean>
    <bean id="menuInfoDao" parent="userInfoDao" class="com.rolemanager.dao.impl.MenuInfoDaoImpl">
    </bean>
    <bean id="menuItemDao" parent="userInfoDao" class="com.rolemanager.dao.impl.MenuItemDaoImpl">
    </bean>
    <bean id="resourceTypeDao" parent="userInfoDao" class="com.rolemanager.dao.impl.ResourceTypeDaoImpl">
    </bean>
    <bean id="resourceInfoDao" parent="userInfoDao" class="com.rolemanager.dao.impl.ResourceInfoDaoImpl">
    </bean>
    <bean id="roleInfoDao" parent="userInfoDao" class="com.rolemanager.dao.impl.RoleInfoDaoImpl">
    </bean>


</beans>

 

  applicationContext-service

 

  

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
    
    <bean id="userInfoService" class="com.rolemanager.service.impl.UserInfoServiceImpl">
        <property name="baseDao" ref="userInfoDao" />
    </bean>
    <bean id="groupInfoService" class="com.rolemanager.service.impl.GroupInfoServiceImpl">
        <property name="baseDao" ref="groupInfoDao" />
    </bean>
    <bean id="menuInfoService" class="com.rolemanager.service.impl.MenuInfoServiceImpl">
        <property name="baseDao" ref="menuInfoDao" />
    </bean>
    <bean id="menuItemService" class="com.rolemanager.service.impl.MenuItemServiceImpl">
        <property name="baseDao" ref="menuItemDao" />
    </bean>
    <bean id="resourceTypeService" class="com.rolemanager.service.impl.ResourceTypeServiceImpl">
        <property name="baseDao" ref="resourceTypeDao" />
    </bean>
    <bean id="resourceInfoService" class="com.rolemanager.service.impl.ResourceInfoServiceImpl">
        <property name="baseDao" ref="resourceInfoDao" />
    </bean>
    <bean id="roleInfoService" class="com.rolemanager.service.impl.RoleInfoServiceImpl">
        <property name="baseDao" ref="roleInfoDao" />
    </bean>

</beans>

 

  反射与泛性应用

 

  

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rolemanager.dao;

import java.io.Serializable;
import java.util.Collection;
import org.springframework.dao.DataAccessException;

/**
 *
 * @author liuqing
 */
public interface IBaseDao<T extends Serializable> {

    public T queryById(int id) throws DataAccessException;

    public Collection<T> queryAll() throws DataAccessException;

    public void add(T t) throws DataAccessException;

    public void remove(T t) throws DataAccessException;



}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rolemanager.dao.impl;

import com.rolemanager.dao.IBaseDao;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author liuqing
 */
public class BaseDaoImpl<T extends Serializable> extends HibernateDaoSupport implements IBaseDao<T> {

    private Class<T> entityClass;

    public BaseDaoImpl() {
        entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public T queryById(int id) throws DataAccessException {
        return this.getHibernateTemplate().get(entityClass, id);
    }

    public Collection<T> queryAll() throws DataAccessException {
        DetachedCriteria detached = DetachedCriteria.forClass(entityClass);
        return this.getHibernateTemplate().findByCriteria(detached);
    }

    public void add(T t) throws DataAccessException {
        this.getHibernateTemplate().save(t);
    }

    public void remove(T t) throws DataAccessException {
        this.getHibernateTemplate().delete(t);
    }

}



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rolemanager.dao;

import com.rolemanager.GroupInfo;

/**
 *
 * @author liuqing
 */
public interface IGroupInfoDao extends IBaseDao<GroupInfo> {

}




/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rolemanager.dao.impl;

import com.rolemanager.GroupInfo;
import com.rolemanager.dao.IGroupInfoDao;

/**
 *
 * @author liuqing
 */
public class GroupInfoDaoImpl extends BaseDaoImpl<GroupInfo> implements IGroupInfoDao {

}

 

  

同理Service

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值