Maven4MyEclipse 搭建ssh2+extjs项目(4)struts2.3.4+hibernate4.2.0+spring3.1.2

1编辑pom.xml

   1.1加入第3方依赖包


		
			javax.servlet
			javax.servlet-api
			3.0.1
			provided
		
		
			org.aspectj
			aspectjweaver
			1.6.8.RELEASE
		
		
			log4j
			log4j
			1.2.14
		
		
			javassist
			javassist
			3.9.0
		
		
			mysql
			mysql-connector-java
			5.0.5
		
		
			cglib
			cglib
			2.2.2
		

     

1.2 加入struts2相关包


			org.apache.struts
			struts2-spring-plugin
			2.3.4
		
		
			org.apache.struts
			struts2-core
			2.3.4
		


1.3加入hibernate相关包


			org.hibernate
			hibernate-ehcache
			4.2.0.Final
		
		
			org.hibernate
			hibernate-c3p0
			4.2.0.Final
		
		
			org.hibernate.common
			hibernate-commons-annotations
			4.0.1.Final
		


1.4加入spring相关包


			org.springframework
			spring-context
			3.1.2.RELEASE
		
		
			org.springframework
			spring-tx
			3.1.2.RELEASE
		
		
			org.springframework
			spring-orm
			3.1.2.RELEASE
		
		
			org.springframework
			spring-web
			3.1.2.RELEASE
		
		
			org.springframework
			spring-asm
			3.1.2.RELEASE
		
		
			org.springframework
			spring-core
			3.1.2.RELEASE
		
		
			org.springframework
			spring-beans
			3.1.2.RELEASE
		


2.编辑web.xml

	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
	

	
		hibernateOpenSessionInViewFilter
		org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
	
  
  	struts2
  	
  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	
  
  
  	
		encodingFilter
		/*
	
	
		hibernateOpenSessionInViewFilter
		*.action
	
	
		struts2
		*.action
	
  
  
		contextConfigLocation
		classpath:applicationContext.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
		org.springframework.web.util.IntrospectorCleanupListener
	
(编码,struts2过滤器,spring监听器,防止内存泄露过滤器,OpenSessionInViewFilter)


3.编辑application.xml


	

	
	
		
		
		
		
		
		
		
		
		
		
	

	
		
		
			
                        com.jjee.entity.User
			
		

		
		
			
				
					org.hibernate.dialect.MySQLDialect
				
				true
				true
			
		
	

	
	
		
	

	
		
			
			
			
			
			
		
	

	
		
	
*component-scan包扫描    *dataSource数据源   *sessionFactory    *transactionManager事务管理  *tx:advice  aop方式配置事务处理


4.配置struts.xml




	
	
		
			/success.jsp
			/error.jsp
		
	
    
顺便配置要测试的action




5.测试
5.1.保存数据
分3层的包




Action_.java

package com.jjee.web;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;


import com.jjee.service.UserService;
import com.opensymphony.xwork2.ActionSupport;


public class Action_ extends ActionSupport {
	private static final Logger logger = Logger.getLogger(Action_.class);
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */


	@Autowired
	protected UserService userService;


	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub\
		try {
			if (userService.save()) {
				return "success";
			}
		} catch (Exception e) {
			return "error";
		}
		return "success";
	}
}


UserService.java

package com.jjee.service;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.jjee.dao.BaseDao;
import com.jjee.entity.User;
import com.jjee.web.Action_;


@Service
public class UserService {


	private static final Logger logger = Logger.getLogger(Action_.class);


	@Autowired
	protected BaseDao baseDao;


	public boolean save() throws Exception {


		// TODO Auto-generated method stub
		logger.debug("debug===============");
		User user = new User();
		user.setName("这是用户名11111111111111");
		baseDao.save(user);
		return true;
	}
}


BaseDao.java

package com.jjee.dao;

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

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 
 * @ClassName:BaseDao
 * @Description:dao 基类,包括增删改查
 * @author:gaoFeng
 * @date:2013-2-27 下午2:31:42
 */
@Repository
public class BaseDao extends ReadDao implements Serializable {

	@Autowired
	private SessionFactory sessionFactory;

	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	

	public  void save(T t) throws Exception {
		getSession().save(t);
		//throw new Exception("==================asdf");
	}
只贴上来保存数据的代码


User.java

package com.jjee.entity;

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

/**
 * User entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "user", catalog = "jjee")
public class User implements java.io.Serializable {

	// Fields

	private Integer id;
	private String name;

	// Constructors

	/** default constructor */
	public User() {
	}

	/** full constructor */
	public User(String name) {
		this.name = name;
	}

	// Property accessors
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

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

	@Column(name = "name", length = 10)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
部署测试一下


查看数据表



5.2测试事务回滚

加上抛异常代码



页面测试


看数据表



看后台显示



ok测试成功


项目代码链接http://download.csdn.net/detail/zsstudio/5259903










你可能感兴趣的:(设计,ssh2,java,maven,extjs)