ssh集成(配置文件)

1、集成环境(spring4、hibernate5、struts2)

2、引入包,maven管理,pom如下


  4.0.0
  wyj
  ssh
  war
  0.0.1-SNAPSHOT
  ssh Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
    
    
      	mysql
   		mysql-connector-java
  		5.1.21
   	
   	
   	    
    org.springframework    
    spring-core    
    4.1.4.RELEASE    
    
    
    
    org.springframework    
    spring-beans    
    4.1.4.RELEASE    
    
    
    
    org.springframework    
    spring-context    
    4.1.4.RELEASE    
    
    
    
    org.springframework    
    spring-context-support    
    4.1.4.RELEASE    
    
    
    
    org.springframework    
    spring-web    
    4.1.4.RELEASE    
    
    
    
    org.springframework    
    spring-webmvc    
    4.1.4.RELEASE    
    
    
    
    org.springframework    
    spring-tx    
    4.1.4.RELEASE    
    
    
    
    org.springframework    
    spring-aop    
    4.1.4.RELEASE    
    
        
    
    org.springframework    
    spring-aspects    
    4.1.4.RELEASE    
    
    
    
    org.springframework    
    spring-jdbc    
    4.1.4.RELEASE    
   


	org.springframework
	spring-orm
	4.3.0.RELEASE
 
   	  
        org.apache.struts  
        struts2-core  
        2.3.34  
    
    
    
  		org.apache.struts
  		struts2-spring-plugin
        2.3.34
     
   	
            org.hibernate
            hibernate-core
            5.2.6.Final
    
    
    
  
  
    ssh
  

3、项目结构

ssh集成(配置文件)_第1张图片

建一个数据库

ssh集成(配置文件)_第2张图片

4、entity


package ssh.entity;

public class User {
    public String username;
    public String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

5、dao

package ssh.dao;

import java.util.List;

import ssh.entity.User;

public interface IndexDao {
	 public List getAllUser();
}

package ssh.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import ssh.entity.User;

public class IndexDaoImpl implements IndexDao {

	 private SessionFactory sessionFactory;
	 
     public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
     }
    
	public List getAllUser() {	
		/*Configuration cfg=new Configuration();  
	    cfg.configure("hibernate.cfg.xml");
	    SessionFactory sessionFactory=cfg.buildSessionFactory();  */
		
		/*ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");        
		sessionFactory =(SessionFactory)context.getBean("sessionFactory");*/
		
		Session session = sessionFactory.openSession();
       
		Query query = session.createQuery("from User");//这是实体,不是表名
        List list = query.getResultList();
        /*for(User u:list){
            System.out.println(u);
        }*/
        session.close();
        sessionFactory.close();
        return list;
	}

}

6、service

package ssh.service;

import java.util.List;

import ssh.entity.User;

public interface IndexService {
	public List getAllUser();
}
package ssh.service;

import java.util.List;

import ssh.dao.IndexDao;
import ssh.dao.IndexDaoImpl;
import ssh.entity.User;

public class IndexServiceImpl implements IndexService {
	private IndexDao id;

    public void setId(IndexDao id) {
        this.id = id;
    }
    
	public List getAllUser() {
		 List userList = id.getAllUser();
		 return userList;
	}

}

7、action

package ssh.action;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import ssh.entity.User;
import ssh.service.IndexService;
import ssh.service.IndexServiceImpl;

public class IndexAction extends ActionSupport{

    private IndexService is;

    public void setIs(IndexService is) {
        this.is = is;
    }

    public String execute() { 
    	/*ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml"); 
    	is =(IndexService)context.getBean("myIndexService");*/
        List myUserList = is.getAllUser();
//        System.out.println("结果集:"+myUserList.size());
        ActionContext ac = ActionContext.getContext();
        ac.put("myUserList", myUserList);
        return SUCCESS;
    }
}

8、User.hbm.xml





    
        
            
            
        
        
            
        
    


9、hibernate.cfg.xml




    
        com.mysql.jdbc.Driver
        填写自己的密码
        jdbc:mysql://localhost:3306/test
        填写自己的登录名
        org.hibernate.dialect.MySQLDialect
        true
         
    


10、applicationContext.xml



    
        
    
    
    
    
        
    
    
    
        
        
    
    
     
    
        
        
    
  

11、struts.xml




	
     
    	//注意这里是填applicationContext里的值
 		    /hello.jsp		    	
 						
 		

12、web.xml




  Archetype Created Web Application
  
      
        contextConfigLocation  
        classpath:applicationContext.xml  
    
    
   
    
        struts2
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    
    
        struts2
        /*
    
    
    
        org.springframework.web.context.ContextLoaderListener
    

13、index.jsp



Hello World!


14、hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>




Insert title here



        
用户名 密码


你可能感兴趣的:(javaweb)