SSH 配置实例: Spring 3.1 + Hibernate 4.2 + Struts 2.3

阅读更多

搞JAVA的小伙伴们都遇到这样的面试官:

 

问:你会SSH吗?

答:会

问:说说怎么用的?

答:。。。 (内心爆出一句脏话)

 

很久没有弄过SSH了,还记得当初用的是hibernate 3, strut2,然而JAVA发展太快了,我们今天要讲述的是 Spring 3.1 + Hibernate 4.2 + Struts 2.3。 搞SSH的都知道,这个版本太蛋疼了,各种不兼容,各种不知道怎么配置。 度娘被蹂躏了千百次,终于将新版的SSH搞定了,分享一下。

 

首先是架包,搞JAVA最头疼的就是乱七八糟的jar,想用下SSH,发现居然要导入那么多jar,而且架包之间还有依赖,所以最好的就是使用Maven去找架包,但是有些小伙伴没有MAVEN环境怎么办?只能一个一个的去找,一个一个去导入到项目中。苦逼的程序员。

 

所有依赖的架包我都已经上传,有需要的到附件里下载,a-d.7z, f-h.7z, j-p.7z 和 s-z.7z。

 

项目结构如图:


SSH 配置实例: Spring 3.1 + Hibernate 4.2 + Struts 2.3_第1张图片
 配置文件有3个:web.xml, dispatcher-servlet.xml 和 struts.xml

 

先看最简单的struts.xml, 我把他放在src下面了,就直接打到classpath下面了。

 

  

    
  	 
       
          
            /bank.jsp  
          
     
       
   
 很简单的配置,就陪着了一个city,成功返回bank.jsp

 

 

再看看spring的配置:

 




	
		
			
				
					
						text/html;charset=UTF-8
						text/plain;charset=UTF-8
					
				
			
		
	

	

	
		
		
		
		
		
		
		
		
		
		
		
		
		
	

	
		
	

	
	
		
		
			
				com.bank.credit.entity
			
		
		
			
				org.hibernate.dialect.MySQLDialect
				true
				true
			
		
	
	

	
	
		
	

	
		
			
			
			
			
			
		
	

	
		
	

 主要是配置了数据源和hibernate的sessionFactory还有事务。和网上的一样。

 

 

有一个要注意的地方:

 

     

             com.bank.credit.entity

     

 

这个一定要配置正确,不然查询时会报错 【hibernate4 xxx is not mapped】,这个指定对了就不要hibernate的配置文件了,不需要做mapping了,通过注解的方式就可以。另外事务一定要配置。

 

再看看web.xml

 



  credit
  
  CharacterEncoding
  org.springframework.web.filter.CharacterEncodingFilter
  
   encoding
   UTF-8
  
  
   forceEncoding
   true
  
 
  
  CharacterEncoding
  /*
 

 
 
    contextConfigLocation
    
        /WEB-INF/dispatcher-servlet.xml
    


 
  org.springframework.web.context.ContextLoaderListener
 

   
        struts2  
          
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter              
          
      
      
        struts2  
        *.action  
      
 定义了spring的监听器和配置文件,定义了struts的拦截器。这个没什么好讲的。

 

 

看下City这个Entity的类:

 

package com.bank.credit.entity;

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 = "BANK_CITY")
public class City {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;

	@Column(name = "NAME")
	private String name;

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

}
 

 

指定table name 和 column name。 

数据库的建表语句:

 

CREATE TABLE `bank_city` (
	`ID` INT(9) NULL DEFAULT NULL,
	`NAME` VARCHAR(20) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
 

 

看Action类:

 

package com.bank.credit.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.bank.credit.entity.City;
import com.bank.credit.service.CityService;
import com.opensymphony.xwork2.ActionSupport;

@Controller
public class CityAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	@Resource
	private CityService service;

	public String execute() throws Exception {
		List citys = service.list();
		ServletActionContext.getRequest().setAttribute("CITYLIST", citys);
		return SUCCESS;
	}
}
 

 

类加@Controller 注解, 用到的Service使用@Resource注入

 

再看下Service的实现类:

 

package com.bank.credit.service.impl;

import java.util.List;

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

import com.bank.credit.dao.CityDao;
import com.bank.credit.entity.City;
import com.bank.credit.service.CityService;

@Service
public class CityServiceImpl implements CityService{

	@Autowired
	private CityDao dao;
	
	@Override
	public List list() {
		return dao.listCitys();
	}

}
 

 

类使用@Service注解,使用的DAO使用@Autowired注入

 

再看下DAO的实现类:

 

package com.bank.credit.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.bank.credit.dao.CityDao;
import com.bank.credit.entity.City;

@Repository 
public class CityDaoImpl extends BaseDao implements CityDao {

	@Override
	public List listCitys() {
		
		 return this.findAll(City.class);
	}

}
 

 

类使用@Repository 注解,继承了BaseDao

 

再看看BaseDao这个类:

package com.bank.credit.dao.impl;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseDao {

	@Autowired  
    private SessionFactory sessionFactory;  
      
    public Session getSession(){  
        return sessionFactory.getCurrentSession();  
    }
    
    protected void save(Object obj) {
        getSession().save(obj);
    }

    @SuppressWarnings("unchecked")
	protected  T get(Class clazz, int id) {
        return (T) getSession().get(clazz, id);
    }
    
    @SuppressWarnings("unchecked")
    protected  List findAll(Class clazz) {
        return getSession().createCriteria(clazz).list();
    }

    protected void update(Object obj) {
        getSession().update(obj);
    }

    protected void delete(Class clazz, int id) {
        getSession().delete(get(clazz, id));
    }


    protected void delete(Object obj) {
        getSession().delete(obj);
    }
}
 

 

注入了SessionFacotry,使用的都是Session(Hibernate4)的方法

最后看看页面:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>




City List



City ID City Name
 非常简单,就是把数据显示在页面上:

SSH 配置实例: Spring 3.1 + Hibernate 4.2 + Struts 2.3_第2张图片
丑的很。
项目我也上传,有需要的可以去附件里下载:credit.7z。
谢谢。
 
 
 
  • a-d.7z (3.3 MB)
  • 下载次数: 8
  • f-h.7z (4.9 MB)
  • 下载次数: 4
  • j-p.7z (4.3 MB)
  • 下载次数: 6
  • s-x.7z (5.1 MB)
  • 下载次数: 6
  • SSH 配置实例: Spring 3.1 + Hibernate 4.2 + Struts 2.3_第3张图片
  • 大小: 36.1 KB
  • SSH 配置实例: Spring 3.1 + Hibernate 4.2 + Struts 2.3_第4张图片
  • 大小: 22.5 KB
  • credit.7z (6.4 KB)
  • 下载次数: 8
  • 查看图片附件

你可能感兴趣的:(Spring,3.1,Hibernate,4.2,Struts,2.3,ssh)