一个整合了ssh框架的实现简单注册的例子

这次使用struts2+hibernate+spring三个框架整合实现一个简单的注册功能,是基于idea这个智能的java IDE。

一,创建一个Project,如下图

勾选红框内的选项


一个整合了ssh框架的实现简单注册的例子_第1张图片

向下翻,还有hibernate


一个整合了ssh框架的实现简单注册的例子_第2张图片

一共四个地方,选好后OK,静静等待idea下载jar包

二,导入必须的包

  • spring-web-x.x.x.RELEASE.jar
  • org.springframework.web.jar
  • mysql-connector-java-x.x.x-bin.jar
    就这三个,其他的idea已经自动下载好了

三,准备工作也已完了,下面就是代码了

先将项目文件目录结构贴出来


一个整合了ssh框架的实现简单注册的例子_第3张图片
Selection_003.png

一,dao
UserDAO.java

package per.summer.dao;

/**
 * Created by summer on 17-4-6.
 */
public interface UserDAO {
    public void addUser(Object entity);
}

UserDAOImpl.java

package per.summer.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 * Created by summer on 17-4-6.
 */
public class UserDAOImpl implements UserDAO {

    SessionFactory sessionFactory;

    //为spring的注入提供setter方法
    public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
    }

    //添加一个用户    
    @Override
    public void addUser(Object entity) {
    Session session = sessionFactory.getCurrentSession();
    Transaction tx = session.beginTransaction();
    session.save(entity);
    tx.commit();
    session.close();
    }
}

二,action
UserAction.java

package per.summer.action;

import com.opensymphony.xwork2.ActionSupport;
import per.summer.dao.UserDAO;
import per.summer.entity.UserEntity;

/**
 * Created by summer on 17-4-6.
 */
public class UserAction extends ActionSupport{
    private String username;
    private String password;

    private UserDAO userDAO;

    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;
    }

    //为spring的注入提供setter方法
    public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
    }

    @Override
    public String execute() throws Exception {
    UserEntity userEntity = new UserEntity(username, password);
    userDAO.addUser(userEntity);
    return SUCCESS;
    }
}

三,entity
UserEntity.java

package per.summer.entity;

import javax.persistence.*;

/**
 * Created by summer on 17-4-6.
 */
@Entity
@Table(name = "user_inf")
public class UserEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    private String password;

    public UserEntity() {
    }

    public UserEntity(String name, String password) {
    this.name = name;
    this.password = password;
    }

    public String getName() {
    return name;
    }

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

    public String getPassword() {
    return password;
    }

    public void setPassword(String password) {
    this.password = password;
    }
}

对于hibernate持久化实体,这里我使用的是注解,就没有UserEntity.hbm.xml文件
四,spring-config.xml



    
    
        
            per.summer.entity.UserEntity
        
    
    
        
            jdbc:mysql://localhost:3306/ssh
            com.mysql.jdbc.Driver

            org.hibernate.dialect.MySQL5Dialect
            root
            manager
            thread
        
    
    

    

    

五,strusts.xml






    
    
    
        /WEB-INF/jsp/success.jsp
    
    

六,web.xml



    
    struts2
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    
    
    struts2
    /*
    
    
    org.springframework.web.context.ContextLoaderListener
    
    
    contextConfigLocation
    classpath:spring-config.xml
    

七,index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: summer
  Date: 17-4-6
  Time: 下午6:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  
    Login
  
  
    
      
      
      
    
  

结果


一个整合了ssh框架的实现简单注册的例子_第4张图片

你可能感兴趣的:(一个整合了ssh框架的实现简单注册的例子)