Maven整合SSH(Struts2+Spring+Hibernate)

1 整合步骤

1.1 手动创建web.xml

    首先新建一个maven工程,在创建的webapp路径下新建一个WEB-INF文件夹,并在该文件夹下新建一个web.xml文件

Maven整合SSH(Struts2+Spring+Hibernate)_第1张图片

1.2  添加依赖

    在pom.xml中添加关于Struts2、Spring、Hibernate等完成的依赖,添加依赖时注意它的四个原则(https://blog.csdn.net/W2612888/article/details/86726664)


  4.0.0
  com.itykd
  ssh-parent
  0.0.1-SNAPSHOT
  pom
  
    
	
		4.2.4.RELEASE
		5.0.7.Final
		2.3.24
	

	
	
		
			
				org.springframework
				spring-context
				${spring.version}
			
			
				org.springframework
				spring-aspects
				${spring.version}
			
			
				org.springframework
				spring-orm
				${spring.version}
			
			
				org.springframework
				spring-test
				${spring.version}
			
			
				org.springframework
				spring-web
				${spring.version}
			
			
				org.hibernate
				hibernate-core
				${hibernate.version}
			
			
				org.apache.struts
				struts2-core
				${struts.version}
			
			
				org.apache.struts
				struts2-spring-plugin
				${struts.version}
			
		
	
	
	
		
		
			org.springframework
			spring-context
		
		
			org.springframework
			spring-aspects
		
		
			org.springframework
			spring-orm
		
		
			org.springframework
			spring-test
		
		
			org.springframework
			spring-web
		
		
		
			org.hibernate
			hibernate-core
		

		
		
			mysql
			mysql-connector-java
			5.1.6
			runtime
		
		

		
			c3p0
			c3p0
			0.9.1.2
		


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

		
		
			javax.servlet
			servlet-api
			2.5
			provided
		
		
			javax.servlet
			jsp-api
			2.0
			provided
		
		
		
			org.slf4j
			slf4j-log4j12
			1.7.2
		
		
		
			junit
			junit
			4.9
			test
		
		
		
			javax.servlet
			jstl
			1.2
		
	

	
		
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.5.1  
				
					1.8
					1.8
					UTF-8
				
			

 			
				org.apache.tomcat.maven
				tomcat7-maven-plugin
				2.2
				
				    
					/ssh
					
					8081	
				
			
		
	

1.3 Hibernate文件的配置

1.3.1 Hibernate.cfg.xml





	
	
		
	    org.hibernate.dialect.MySQL5Dialect

		
		true
		
		true

		none
		
		
		
	

1.3.2 mapper.hbm.xml





    
        
            
            
        
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
    

1.4 applicationContext.xml



	
	
	
		
		
		
		
	
	
	
	
	
		
		
		
		
	
	
	
	 
	
	
	

1.5 struts.xml




	
	    
	        /jsp/demo2.jsp    
	    
	

1.6 web.xml

    在web.xml文件中添加struts2的核心过滤器(StrutsPrepareAndExecuteFilter)spring的核心监听器(ContextLoaderListener)



  maven_start
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
    struts2
  	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
  
  	struts2
  	/*
  
  
  
      org.springframework.web.context.ContextLoaderListener
  	
  
      contextConfigLocation
      classpath:applicationContext.xml
  	

2 示例

2.1 web层

package com.itykd.web.action;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.itykd.entity.Customer;
import com.itykd.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport{
	
	@Resource(name="customerService")
	private CustomerService customerService;		
	private Customer customer;	
	private Long custId;

		public Customer getCustomer() {
			return customer;
		}
		public void setCustomer(Customer customer) {
			this.customer = customer;
		}
		
		public Long getCustId() {
			return custId;
		}
		public void setCustId(Long custId) {
			this.custId = custId;
		}
		public String findById(){
			customer = customerService.getById(custId);
			System.out.println("哈哈哈哈...");
			return SUCCESS;
		}
}

2.2 service层

package com.itykd.service.impl;

import javax.annotation.Resource;

import com.itykd.dao.CustomerDao;
import com.itykd.entity.Customer;
import com.itykd.service.CustomerService;

public class CustomerServiceImpl implements CustomerService{
	
	@Resource(name="customerDao")
	private CustomerDao customerDao;
	
	@Override
	public Customer getById(Long id) {
		
		return customerDao.getById(id);
	}

}

2.3 dao层

package com.itykd.dao.impl;

import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.itykd.dao.CustomerDao;
import com.itykd.entity.Customer;

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao{

	@Override
	public Customer getById(Long id) {
		return this.getHibernateTemplate().get(Customer.class, id);
	}

}

 

你可能感兴趣的:(Maven,Framework,integration)