spring mvc+hibernate 实现事务管理(全注解版)

为了方便项目变大配置文件变多,用注解代替 *.hbm.xml,<bean id="*dao" class="">,另外用反省实现dao操作,省去每个类一个dao,此处参考了鸵鸟的例子。

实现功能跟http://blog.csdn.net/waiwai4701/article/details/38270721这个项目是一样的,controller和页面就不再写

首先,jar包支持,为了方便jar包管理采用maven技术,服务器没有用tomcat用的jetty,不要在意这些细节

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Sand</groupId>
  <artifactId>mas</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>mas</name>
  <url>http://maven.apache.org</url>

   <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>3.2.5.RELEASE</spring.version>
	<curator_version>1.1.16</curator_version>
	<slf4j_version>1.6.2</slf4j_version>
	<jcl_version>1.1</jcl_version>
	<log4j_version>1.2.16</log4j_version>
	<jetty.version>8.1.8.v20121106</jetty.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
   	<dependency>
      <groupId>org.jdom</groupId>
      <artifactId>jdom</artifactId>
      <version>1.1.3</version>
    </dependency>

    	<dependency>
    		<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j_version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${slf4j_version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j_version}</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j_version}</version>
		</dependency>
		<!-- springframework -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.2.5.RELEASE</version>
			<exclusions>
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

    <!-- for jetty -->
    <dependency>
	  <groupId>org.eclipse.jetty</groupId>
	  <artifactId>jetty-server</artifactId>
	  <version>${jetty.version}</version>
	</dependency>

	<dependency>
	  <groupId>org.eclipse.jetty</groupId>
	  <artifactId>jetty-util</artifactId>
	  <version>${jetty.version}</version>
	</dependency>

    <dependency>
	  <groupId>org.eclipse.jetty</groupId>
	  <artifactId>jetty-xml</artifactId>
	  <version>${jetty.version}</version>
	</dependency>

	<dependency>
	  <groupId>org.eclipse.jetty</groupId>
	  <artifactId>jetty-webapp</artifactId>
	  <version>${jetty.version}</version>
	</dependency>
	<dependency>
	    <groupId>org.eclipse.jetty</groupId>
	    <artifactId>jetty-jsp</artifactId>
	    <version>${jetty.version}</version>
	</dependency>
	 <!-- spring mvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.2.5.RELEASE</version>
    </dependency>  

    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-webmvc</artifactId>
       <version>3.2.5.RELEASE</version>
    </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-orm</artifactId>
       <version>3.2.5.RELEASE</version>
    </dependency>
	<dependency>
		<groupId>javax.servlet.jsp.jstl</groupId>
		<artifactId>jstl-api</artifactId>
		<version>1.2-rev-1</version>
	</dependency>

     <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>runtime</scope>
     </dependency>
     <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
     </dependency>

    <dependency>
		<groupId>org.codehaus.jackson</groupId>
    	<artifactId>jackson-core-lgpl</artifactId>
    	<version>1.9.6</version>
	</dependency>
	<dependency>
		<groupId>org.codehaus.jackson</groupId>
    	<artifactId>jackson-core-asl</artifactId>
    	<version>1.9.4</version>
	</dependency>
	<dependency>
		<groupId>org.codehaus.jackson</groupId>
    	<artifactId>jackson-mapper-asl</artifactId>
    	<version>1.9.5</version>
	</dependency>
	<dependency>
  	  <groupId>org.codehaus.jackson</groupId>
   	  <artifactId>jackson-mapper-lgpl</artifactId>
  	  <version>1.9.6</version>
	</dependency>
	<!-- memcached -->
    <dependency>
		<groupId>com.danga</groupId>
  		<artifactId>java-memcached</artifactId>
  		<version>2.6.6</version>
	</dependency>
	<!-- hibernate -->
	<dependency>
		<groupId>org.hibernate</groupId>
  		<artifactId>hibernate</artifactId>
  		<version>3.3.2</version>
	</dependency>
	<!-- mysql -->
	 <dependency>
		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  		<version>5.1.21</version>
	</dependency>
	<!-- oracle -->
	<dependency>
		<groupId>oracle</groupId>
  		<artifactId>ojdbc</artifactId>
  		<version>14</version>
	</dependency>
	<!-- datasource -->
	<dependency>
		<groupId>commons-dbcp</groupId>
  		<artifactId>commons-dbcp</artifactId>
  		<version>1.4</version>
	</dependency>
	<!-- jta -->
	<dependency>
		<groupId>javax.transaction</groupId>
  		<artifactId>jta</artifactId>
  		<version>1.1</version>
	</dependency>
	<!-- dom4j -->
	<dependency>
	   <groupId>dom4j</groupId>
  		<artifactId>dom4j</artifactId>
	   <version>1.6.1</version>
	</dependency>
	<!-- commons-collections -->
	<dependency>
	    <groupId>commons-collections</groupId>
	    <artifactId>commons-collections</artifactId>
	    <version>3.1</version>
	</dependency>
	<!-- javaassist -->
	 <dependency>
      <groupId>javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.9.0</version>
    </dependency>
    <!-- hibernate-annotation -->

     <dependency>
      <groupId>javax.persistence</groupId>
      <artifactId>persistence-api</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-annotations</artifactId>
      <version>3.4.0.GA</version>
    </dependency>
    <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-commons-annotations</artifactId>
       <version>3.3.0.ga</version>
    </dependency>
     <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>ejb3-persistence</artifactId>
       <version>1.0.2.GA</version>
    </dependency>
  </dependencies>
</project>

实体类:

package Sand.mas.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "tuser")
public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	private String id;

	private String userName;

	private String password;

	public User(){

	}

	public User(String userName,String password){
		this.userName = userName;
		this.password = password;
	}

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	 @Column(name = "username", length = 20)
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Column(name = "password", length = 20)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String toString(){

		return "user:id="+id+",userName="+userName+",password="+password;
	}

}

一些基础用于扩展的东西:

BaseDao

package Sand.mas.sys;

import java.io.Serializable;
import java.util.List;

public interface BaseDao<T> {

	/**
	 * 保存一个对象
	 *
	 * @param o
	 * @return
	 */
	public Serializable save(T o);

	/**
	 * 删除一个对象
	 *
	 * @param o
	 */
	public void delete(T o);

	/**
	 * 更新一个对象
	 *
	 * @param o
	 */
	public void update(T o);

	/**
	 * 保存或更新对象
	 *
	 * @param o
	 */
	public void saveOrUpdate(T o);

	/**
	 * 查询
	 *
	 * @param hql
	 * @return
	 */
	public List<T> find(String hql);

	/**
	 * 查询集合
	 *
	 * @param hql
	 * @param param
	 * @return
	 */
	public List<T> find(String hql, Object[] param);

	/**
	 * 查询集合
	 *
	 * @param hql
	 * @param param
	 * @return
	 */
	public List<T> find(String hql, List<Object> param);

	/**
	 * 查询集合(带分页)
	 *
	 * @param hql
	 * @param param
	 * @param page
	 *            查询第几页
	 * @param rows
	 *            每页显示几条记录
	 * @return
	 */
	public List<T> find(String hql, Object[] param, Integer page, Integer rows);

	/**
	 * 查询集合(带分页)
	 *
	 * @param hql
	 * @param param
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<T> find(String hql, List<Object> param, Integer page, Integer rows);

	/**
	 * 获得一个对象
	 *
	 * @param c
	 *            对象类型
	 * @param id
	 * @return Object
	 */
	public T get(Class<T> c, Serializable id);

	/**
	 * 获得一个对象
	 *
	 * @param hql
	 * @param param
	 * @return Object
	 */
	public T get(String hql, Object[] param);

	/**
	 * 获得一个对象
	 *
	 * @param hql
	 * @param param
	 * @return
	 */
	public T get(String hql, List<Object> param);

	/**
	 * select count(*) from 类
	 *
	 * @param hql
	 * @return
	 */
	public Long count(String hql);

	/**
	 * select count(*) from 类
	 *
	 * @param hql
	 * @param param
	 * @return
	 */
	public Long count(String hql, Object[] param);

	/**
	 * select count(*) from 类
	 *
	 * @param hql
	 * @param param
	 * @return
	 */
	public Long count(String hql, List<Object> param);

	/**
	 * 执行HQL语句
	 *
	 * @param hql
	 * @return 响应数目
	 */
	public Integer executeHql(String hql);

	/**
	 * 执行HQL语句
	 *
	 * @param hql
	 * @param param
	 * @return 响应数目
	 */
	public Integer executeHql(String hql, Object[] param);

	/**
	 * 执行HQL语句
	 *
	 * @param hql
	 * @param param
	 * @return
	 */
	public Integer executeHql(String hql, List<Object> param);

}

BaseDao实现类BaseDaoImpl

package Sand.mas.sys;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
import org.springframework.stereotype.Repository;

@Repository("baseDao")
public class BaseDaoImpl<T> implements BaseDao<T> {

	private AnnotationSessionFactoryBean sessionFactory;//注意是AnnotationSessionFactoryBean

	public AnnotationSessionFactoryBean getSessionFactory() {
		return sessionFactory;
	}

	@Autowired
	public void setSessionFactory(AnnotationSessionFactoryBean sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession(){
		return sessionFactory.getObject().getCurrentSession();//还是可以得到session
	}

	public Serializable save(T o) {
		return this.getCurrentSession().save(o);
	}

	public void delete(T o) {
		this.getCurrentSession().delete(o);

	}

	public void update(T o) {
		this.getCurrentSession().update(o);

	}

	public void saveOrUpdate(T o) {
		this.getCurrentSession().saveOrUpdate(o);

	}

	@SuppressWarnings("unchecked")
	public List<T> find(String hql) {

		return this.getCurrentSession().createQuery(hql).list();
	}

	@SuppressWarnings("unchecked")
	public List<T> find(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if(param != null && param.length > 0){
			for(int i = 0;i<param.length;i++){
				q.setParameter(i, param[i]);
			}
		}
		return q.list();
	}

	@SuppressWarnings("unchecked")
	public List<T> find(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if(param != null && param.size() > 0){
			for(int i = 0;i<param.size();i++){
				q.setParameter(i, param.get(i));
			}
		}
		return q.list();
	}

	@SuppressWarnings("unchecked")
	public List<T> find(String hql, Object[] param, Integer page, Integer rows) {
		if(page == null || page < 1){
			page = 1;
		}
		if(rows == null || rows < 1){
			rows = 10;
		}

		Query q = this.getCurrentSession().createQuery(hql);

		if(param != null && param.length > 0 ){
			for(int i = 0;i< param.length;i++){
				q.setParameter(i, param[i]);
			}
		}
		return q.setFirstResult((page-1)*rows).setMaxResults(rows).list();
	}

	@SuppressWarnings("unchecked")
	public List<T> find(String hql, List<Object> param, Integer page,Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	@SuppressWarnings("unchecked")
	public T get(Class<T> c, Serializable id) {
		return (T)this.getCurrentSession().get(c , id);
	}

	public T get(String hql, Object[] param) {
		List<T> l = this.find(hql, param);
		if(l != null && l.size() > 0){
			return l.get(0);
		}else{
			return null;
		}
	}

	public T get(String hql, List<Object> param) {
		List<T> l = this.find(hql, param);
		if (l != null && l.size() > 0) {
			return l.get(0);
		} else {
			return null;
		}
	}

	public Long count(String hql) {
		return (Long)this.getCurrentSession().createQuery(hql).uniqueResult();
	}

	public Long count(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return (Long) q.uniqueResult();
	}

	public Long count(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return (Long) q.uniqueResult();
	}

	public Integer executeHql(String hql) {
		return this.getCurrentSession().createQuery(hql).executeUpdate();
	}

	public Integer executeHql(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.executeUpdate();
	}

	public Integer executeHql(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.executeUpdate();
	}

}

BaseService:

package Sand.mas.sys;

public interface BaseService {
//也就是直接性的什么都没有,只为了实现类节省每个扩展service的@transaction
}

实现类 BaseServiceImpl:

package Sand.mas.sys;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("baseService")
@Transactional(readOnly = false,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class BaseServiceImpl implements BaseService{

}

UserService:

package Sand.mas.service;

public interface UserService {

	public void test();
}

UserServiceImpl:

package Sand.mas.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import Sand.mas.model.User;
import Sand.mas.service.UserService;
import Sand.mas.sys.BaseDao;
import Sand.mas.sys.BaseServiceImpl;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl implements UserService{

	private BaseDao<User> userDao;//看到妙处没

	public BaseDao<User> getUserDao() {
		return userDao;
	}
	@Autowired
	public void setUserDao(BaseDao<User> userDao) {
		this.userDao = userDao;
	}

	@Transactional(rollbackFor=Exception.class)
	public void test() {
		User user1 = new User("zhangsan","111111");

		User user2 = new User("lisi","222222222222222222222222222222");

		userDao.save(user1);
		System.out.println("user1 success");

		userDao.save(user2);
		System.out.println("user2 success");

	}

}

最后,配置文件,折腾了姐好久

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:cache="http://www.springframework.org/schema/cache"
         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/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
          http://www.springframework.org/schema/tx
 		  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/cache
          http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
  <!--扫描的包-->
  <context:component-scan base-package="Sand.mas"/>
  <!--注解支持-->
  <mvc:annotation-driven/>
  <!--视图解析-->
  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    	<property name="prefix" value="/pages/"/>
    	<property name="suffix" value=".jsp"/>
  </bean>
  <!--静态文件的访问-->
  <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
  <mvc:resources mapping="/common/**" location="/common/" cache-period="31556926"/>
  <mvc:resources mapping="/jslib/**" location="/jslib/" cache-period="31556926"/>
  <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>
  <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>  

  <!-- 支持json返回 -->
  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list >
                <ref bean="mappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean>  

	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes">
        <list>
            <value>application/json;charset=UTF-8</value>
       </list>
    </property>
	</bean>
	 	<bean id="dataSource"
	        class="org.apache.commons.dbcp.BasicDataSource">
	         <property name="driverClassName"
	             value="com.mysql.jdbc.Driver">
	         </property>
	         <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
	       <property name="username" value="root"/>
	       <property name="password" value=""/>
	 	</bean>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><!--注解专用session -->
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>

		 <property name="annotatedClasses"><!--看,mappingresource消失了 -->
	      	<list>
	      		<value>Sand.mas.model.User</value>
	      	</list>
	      </property>
	</bean>

	<!-- 配置事务管理器bean,使用HibernateTransactionManager事务管理器 -->
	<bean id="transactionManager"
	    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	        <!-- 为事务管理器注入sessionFactory" -->
	        <property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	<!-- 注解实现事务 -->
	 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>

源码下载地址:http://download.csdn.net/detail/waiwai4701/7833547

时间: 2024-10-08 01:43:07

spring mvc+hibernate 实现事务管理(全注解版)的相关文章

Spring+Spring MVC+Hibernate增查(使用注解)

使用Spring+Spring MVC+Hibernate做增删改查开发效率真的很高.使用Hibernate简化了JDBC连接数据库的的重复性代码.下面根据自己做的一个简单的增加和查询,把一些难点分析出来: 首先项目目录结构:(Hibernate持久化数据连接信息交给Spring进行管理:别忘了加入Hibernate和Spring相关的架包.jar) 第一步:弄个用户实体类(配置Users.hbm.xml映射文件): 1 package com.ssh.SpringMVC.enity; 2 3

Spring + Spring MVC + Hibernate项目开发集成(注解)

在自己从事的项目中都是使用xml配置的方式来进行的,随着项目的越来越大,会发现配置文件会相当的庞大,这个不利于项目的进行和后期的维护.于是考虑使用注解的方式来进行项目的开发,前些日子就抽空学习了一下.在网上也查询了很多使用注解来搭建开发框架的文章,但是有一个问题就是,使用更新的软件版本会出错.这里我将使用最新的Spring,Hibernate来进行框架的搭建,经过测试,顺利运行.分享旨在与大家一起分享学习,共同进步,有不足之处,望不吝赐教,谢谢! 本项目使用maven构建,采用Spring +

spring与hibernate整合事务管理的理解

在谈Spring事务管理之前我们想一下在我们不用Spring的时候,在Hibernate中我们是怎么进行数据操作的.在Hibernate中我们每次进行一个操作的的时候我们都是要先开启事务,然后进行数据操作,然后提交事务,关闭事务,我们这样做的原因是因为Hibernate默认的事务自动提交是false,他是需要我们人为的手动提交事务,假如你不想每次都手动提交事务的话,你可以在hibernate.cfg.xml我文件中把它设置为事务自动提交: xml代码 1 <property name="d

Spring + Spring MVC + Hibernate

Spring + Spring MVC + Hibernate项目开发集成(注解) Posted on 2015-05-09 11:58 沐浴未来的我和你 阅读(307) 评论(0) 编辑 收藏 在自己从事的项目中都是使用xml配置的方式来进行的,随着项目的越来越大,会发现配置文件会相当的庞大,这个不利于项目的进行和后期的维护.于是考虑使用注解的方式来进行项目的开发,前些日子就抽空学习了一下.在网上也查询了很多使用注解来搭建开发框架的文章,但是有一个问题就是,使用更新的软件版本会出错.这里我将使

搭建基于全注解的Spring+Spring MVC+Hibernate框架

以实例讲解Spring+Spring MVC+Hibernate框架搭建步骤: 一.配置web.xml 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/XML

Spring事务管理-使用注解配置事务

一.概述 事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性.Spring Framework对事务管理提供了一致的抽象,其特点如下:为不同的事务API提供一致的编程模型,比如JTA(Java Transaction API), JDBC, Hibernate, JPA(Java Persistence API和JDO(Java Data Objects)支持声明式事务管理,特别是基于注解的声明式事务管理,简单易用 提供比其他事务API如JTA更简单的编程式事务管理

spring mvc + mybatis + spring aop声明式事务管理没有作用

在最近的一个项目中,采用springMVC.mybatis,发现一个很恼人的问题:事务管理不起作用!!网上查阅了大量的资料,尝试了各种解决办法,亦未能解决问题! spring版本:3.0.5 mybatis版本:3.2.2 1.applicationContext.xml配置: mvc + mybatis + spring aop声明式事务管理没有作用" title="spring mvc + mybatis + spring aop声明式事务管理没有作用">2.spr

Spring MVC + Hibernate + Maven: Crud操作示例

Alexey是一个在使用Java,TestNG 和Selenium的自动化WEB应用程序中有丰富经验的测试开发者.他如此的喜欢QA以至于在下班后他为初级QA工程师提供培训课程. 在这篇文章中我想介绍一个Spring MVC + Hibernate + Maven例子.这组技术主要涉及一些基础知识,我想在每一个必要的地方详细解释它.本篇话题范围以外的更多资源,我会提供链接方便你阅读.在文章的最后,我将发布一个GitHub的链接. 目标 示例web应用程序是基于Spring MVC, Hiberna

spring mvc+hibernate的基本配置

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p&