最新的ssh整合最新版,spring4.2和stuts2.3及hibernate4.2

整合时建议一步步来,把每一个框架都做通,然后在处理整合的问题整合环境是struts2.2和hibernate4.2和spring4.2,jdk1.6

第一步:列举需要的jar包:

hibernate4.2jar包:
最新的ssh整合最新版,spring4.2和stuts2.3及hibernate4.2_第1张图片

c3p0数据库jar包,当然也可以是使用spring提供的一个c3p0包:

最新的ssh整合最新版,spring4.2和stuts2.3及hibernate4.2_第2张图片

数据库驱动,这里采用mysql数据库:mysql-connector-java-5.1.28-bin.jar


struts2jar包,注意有重复的jar,要删除,javassist,一般保留版本高的:

最新的ssh整合最新版,spring4.2和stuts2.3及hibernate4.2_第3张图片

整合包spring和struts2:struts2-spring-plugin-2.3.28

做分布整合时,建议先不要导入,不然struts会无法起作用

spring4.2包:
最新的ssh整合最新版,spring4.2和stuts2.3及hibernate4.2_第4张图片

aop联盟:




最后要注意的是如果使用注解开发,struts需要导入注解包:

第二步:各个层次的代码接口就不在写了:

po类:

public class User {
        private Integer id;
	private String name;
	private Double money;
sett..
getter..
}
hibernate映射文件:





    
              
              
              
              
              
    




dao层:

package com.leige.dao.impl;



import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.leige.dao.UserDao;
import com.leige.domain.User;

public class UserDaoImpl  implements UserDao {
private  HibernateTemplate hibernateTemplate;
		public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
			this.hibernateTemplate = hibernateTemplate;
		}

	public void addUser(User user) {
	//保存对象
		hibernateTemplate.save(user);
	}

	@Override
	public void update(User user) {
	
	    this.hibernateTemplate.update(user);
	}

	@Override
	public User findById(Integer id) {
		// TODO Auto-generated method stub
		return this.hibernateTemplate.get(User.class, id);
	}

	

}

service层:

package com.leige.service.impl;

import javax.ejb.FinderException;

import com.leige.dao.UserDao;
import com.leige.dao.impl.UserDaoImpl;
import com.leige.domain.User;
import com.leige.service.UserService;

public class UserServiceImpl implements UserService {
	UserDao userDao;
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
/*	实现注册业务*/
	@Override
	public void regist(User form) {
		
		userDao.addUser(form);
	}
	//实现转账业务
	@Override
	public void transfer(Integer from, Integer to, Double money)  {
		/*调用dao层实现转账业务:
		 * 
		 * 
		 
		 * */

		User fr=userDao.findById(from);
		fr.setMoney(fr.getMoney()-money);
		userDao.update(fr);
		User tt=userDao.findById(to);
		//int i=5/0;
		tt.setMoney(tt.getMoney()+money);
		userDao.update(tt);
	}

}

action层:
package com.leige.web.action;

import com.leige.domain.User;
import com.leige.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
/*	service由spring注入*/
	UserService userService;
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	private User user;
	//转账业务所需参数
	private Integer userTo;
	private Integer userFrom;
	private Double money;
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public Integer getUserTo() {
		return userTo;
	}

	public void setUserTo(Integer userTo) {
		this.userTo = userTo;
	}

	public Integer getUserFrom() {
		return userFrom;
	}

	public void setUserFrom(Integer userFrom) {
		this.userFrom = userFrom;
	}

	public Double getMoney() {
		return money;
	}
	public void setMoney(Double money) {
		this.money = money;
	}
	
	public String add(){
		userService.regist(user);
		return "success";
	}
	public String pay(){
		/*System.out.println("from----"+userFrom);
		System.out.println("to----"+userTo);
		System.out.println("money----"+money);*/
	/*	调用service层实现转账*/
		userService.transfer(userFrom, userTo, money);
		return "success";
	}
}



第三步各个层次的配置文件配置,详细解释都在配置文件中:
web.xml


	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_3_0.xsd">
  SSh
  

      
		contextConfigLocation
		classpath:applicationContext.xml
	
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
	    
    
    
        Struts2.0  
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
      
  
      
   
        Struts2.0  
                
        /*  
      
 	
  
    index.jsp
  

struts2.的配置文件:






  
    
    

    

        

        
            /WEB-INF/jsp/error.jsp
        

        
            
        

        
        /show.jsp
        
       
    






hibernate配置这一步,我们可以将在spring配置文件中配置也可以在hibernate中配置:





	
	
	


com.mysql.jdbc.Driver
jdbc:mysql:///test
root

org.hibernate.dialect.MySQLDialect
true
update
4


10
3
4000
3
100





	



spring配置




       				
       	
       	
       	
       	
       	
       	
       	
       	
       	
       	
       	
    
       	
       	
       	
       	
       	
    
    
    
 
    
    

    
    
      

 
		 
		 
		 
		 
 

 
 
 
 



业务表单:

  
姓名:
金钱:

转账人:
收账人:
金钱:



hibernate配置在spring中配置的方式:




       				
       	
       	
       	
       	
       	
	
	
	
		
		
		
		
	
       	
       	
       	
       	
       	
       	
       	
       	
    
    

				
  

  
  
  	           org.hibernate.dialect.MySQLDialect
				true
				true
				update
				none
		
  
  
  

		
		
    
    

    
    
      

 
		 
		 
		 
		 
 

 
 
 
 

dao层继承 HibernateDaoSupport

package com.leige.dao.impl;




import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;


import com.leige.dao.UserDao;
import com.leige.domain.User;

public class UserDaoImpl  extends HibernateDaoSupport implements UserDao {


	public void addUser(User user) {
	//保存对象
		super.getHibernateTemplate().save(user);
	}

	@Override
	public void update(User user) {
	
		super.getHibernateTemplate().update(user);
	}

	@Override
	public User findById(Integer id) {
	
		return super.getHibernateTemplate().get(User.class, id);
	}

	

}



你可能感兴趣的:(web)