`

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

 

 

 

分享到:
评论

相关推荐

    spring mvc+hibernate实现事务管理(配置文件版)

    spring mvc hibernate整合,采用MyEclipse自动生成包和配置文件,jar包可能有多余,但是不影响整体效果。服务器tomcat。数据库mysql。

    spring applicationContext 配置文件

    &lt;description&gt;Spring公共配置文件 &lt;!-- mes 的數據庫 --&gt; &lt;property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/&gt; ...

    spring2.5学习PPT 传智博客

    让Spring自动扫描和管理Bean 15.使用JDK中的Proxy技术实现AOP功能 16.使用CGLIB实现AOP功能与AOP概念解释 17.使用Spring的注解方式实现AOP入门 18.使用Spring的注解方式实现AOP的细节 19.使用Spring配置文件...

    spring jar 包详解

    (2) spring-beans.jar 这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,...

    Spring的学习笔记

    二、 建立spring的配置文件 8 三、 引入spring的jar包 8 四、 测试代码: 8 五、 注意接口的使用: 8 第五课:IOC(DI)配置及应用 9 一、 什么是IOC、DI 9 二、 编辑xml文件时,没有提示 9 三、 注入类型(Injecting ...

    高级开发spring面试题和答案.pdf

    SPI 机制(Java SPI 实际上是“基于接口的编程+策略模式+配置文件”组合实现的动态加载机制), 很多地方有用到: AOP Spring的AOP的底层实现原理; 为什么jdk动态代理是必须是接口 两种动态代理的区别 AOP实现方式:...

    Spring 2.0 开发参考手册

    2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA 2.4.3. 异步的JMS 2.4.4. JDBC 2.5. Web层 2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. ...

    Spring面试题含答案.pdf

    12. Spring 配置文件 13. 什么是 Spring IOC 容器? 14. IOC 的优点是什么? 15. ApplicationContext 通常的实现是什么? 16. Bean 工厂和 Application contexts 有什么区别? 17. 一个 Spring 的应用看起来象什么...

    spring4.3.9相关jar包

    spring-beans.jar(必须):这 个jar 文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean 以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI ...

    spring in action英文版

     5.1.2 理解Spring对事务管理的支持  5.1.3 介绍Spring的事务管理器  5.2 在Spring中编写事务  5.3 声明式事务  5.3.1 理解事务属性  5.3.2 声明一个简单的事务策略  5.4 通过方法名声明事务 ...

    尚硅谷佟刚Spring4代码及PPT.rar

    代码及ppt涵盖 Spring4.0 的所有核心内容:在 Eclipse 中安装 SpringIDE 插件、IOC & DI、在 Spring 中配置 Bean、自动装配、Bean 之间的关系(依赖、继承)、Bean 的作用域、使用外部属性文件、SpEL、管理 Bean 的...

    Spring-Reference_zh_CN(Spring中文参考手册)

    2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA 2.4.3. 异步的JMS 2.4.4. JDBC 2.5. Web层 2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. 动态语言...

    Spring.3.x企业应用开发实战(完整版).part2

    2.2.3 类包及Spring配置文件规划 2.3 持久层 2.3.1 建立领域对象 2.3.2 UserDao 2.3.3 LoginLogDao 2.3.4 在Spring中装配DAO 2.4 业务层 2.4.1 UserService 2.4.2 在Spring中装配Service 2.4.3 单元测试 2.5 展现层 ...

    最新最全的spring开发包

    这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,引入spring-core.jar及...

    springMVC+Spring+Mybatis+Maven整合代码案例

    5、测试Spring+mybatis的框架搭建,写单元测试JUnit,测试事务配置等:model--&gt;dao(mapper)--&gt;service--&gt;test 6、映入SpringMVC:配置SpringMVC配置信息。--&gt;配置文件:spring-mvc.xml(扫描controller) 7...

    Spring中文帮助文档

    2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. 对Websphere 事务管理的完整支持 2.4.3. JPA 2.4.4. 异步的JMS 2.4.5. JDBC 2.5. Web层 2.5.1. Spring MVC合理的默认值 2.5.2. Portlet 框架 2.5.3. 基于...

    Spring API

    2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. 对Websphere 事务管理的完整支持 2.4.3. JPA 2.4.4. 异步的JMS 2.4.5. JDBC 2.5. Web层 2.5.1. Spring MVC合理的默认值 2.5.2. Portlet 框架 2.5.3. 基于...

    spring chm文档

    2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA 2.4.3. 异步的JMS 2.4.4. JDBC 2.5. Web层 2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. ...

    spring.net中文手册在线版

    14.5.1.理解Spring.NET声明式事务管理的实现 14.5.2.第一个例子 14.5.3.Transaction特性的设置 14.5.4.通过AutoProxyCreator使用声明式事务 14.5.5.通过TransactionProxyFactoryObject使用声明式事务 14.5.6. 通过...

Global site tag (gtag.js) - Google Analytics