Hibernate+Maven+Struts2——实现增删查改CURD的步骤详解

首先,让我们来看一下我们要实现的效果图

Hibernate+Maven+Struts2——实现增删查改CURD的步骤详解_第1张图片

接下来,上博主的代码架构

Hibernate+Maven+Struts2——实现增删查改CURD的步骤详解_第2张图片


接下来直接进入主题:

1.创建数据库的表

Hibernate+Maven+Struts2——实现增删查改CURD的步骤详解_第3张图片

2.创建项目

用Eclipse创建一个Maven Project项目

3.导入hibernate相关的架包到项目

pom.xml


  4.0.0
  Maven_Hibenate01
  Maven_Hibenate01
  war
  0.0.1-SNAPSHOT
  Maven_Hibenate01 Maven Webapp
  http://maven.apache.org
  
  
  		-Dfile.encoding=UTF-8
  
  
  
	    
	         junit
	         junit
	         4.12
	         test
	    
	    
	    
	    	javax.servlet
	    	javax.servlet-api
	    	3.1.0
	    	provided
		
		
		
		
	   		org.hibernate
	   		hibernate-core
	   		5.2.10.Final
		
	
		
		
	    	org.apache.struts
	    	struts2-core
	    	2.5.12
		
		
	    	org.apache.struts
	    	struts2-convention-plugin
	    	2.5.12
		
	
		   
		  
		    net.sf.json-lib  
		    json-lib  
		    2.4  
		    jdk15  
		
		
		
		
	   		mysql
	   		mysql-connector-java
	   		5.1.18
		
	    
	    
  
  
  
    Maven_Hibenate01
  

 4.配置hibernate.cfg.xml




    
            
            root
            123
            jdbc:mysql://localhost:3306/test?characterEncoding=utf8
            com.mysql.jdbc.Driver
            
            
            
    
    


5.创建实体类User.java和映射文件 User.hbm.xml(映射文件和实体对象必须在同一个包下)

package com.yhl.entity;

public class User {
	   private int u_id;//编号
	   private String u_name;//用户名
	   private String u_sex;//性别
	   
	   public User() {
		
	   }
	   public User(int u_id) {
		   this.u_id = u_id;
	   }
	   public User(String u_name, String u_sex) {
		   super();
		   this.u_name = u_name;
		   this.u_sex = u_sex;
	   }
	   public User(int u_id, String u_name, String u_sex) {
		   this.u_id = u_id;
		   this.u_name = u_name;
		   this.u_sex = u_sex;
	   }
	   
	   public int getU_id() {
		   return u_id;
	   }
	   public void setU_id(int u_id) {
		   this.u_id = u_id;
	   }
	   public String getU_name() {
		   return u_name;
	   }
	   public void setU_name(String u_name) {
		   this.u_name = u_name;
	   }
	   public String getU_sex() {
		   return u_sex;
	   }
	   public void setU_sex(String u_sex) {
		   this.u_sex = u_sex;
	   }   
	   
}
Hibernate+Maven+Struts2——实现增删查改CURD的步骤详解_第4张图片
Hibernate+Maven+Struts2——实现增删查改CURD的步骤详解_第5张图片
6.写Dao层

接口UserDao.java

package com.yhl.dao;

import java.util.List;
import com.yhl.entity.User;

public interface UserDao {

	/**
	 * 查询所有
	 */
	public List getAll();
	
	/**
	 * 添加一行信息
	 */
	public int AddUser(User user);
	
	/**
	 * 根据ID删除一行信息
	 */
	public int DeleteUser(int u_id);
	
	/**
	 * 修改用户信息
	 */
	public int UpadteUser(User user);
}
实现类UserDaoImp.java
package com.yhl.dao;

import java.io.Serializable;
import java.util.List;

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

import com.yhl.entity.User;

public class UserDaoImp implements UserDao{
	
	
    private Configuration configuration;//1.实例化配置对象 加载映射文件hibernate.cfg.xml
	private SessionFactory sessionFactory;//2.创建会话工厂
	private Session session;//3.创建会话
	private Transaction transaction;//4.开启事务
	
	/**
	 * 查看所有
	 */
	public List getAll() {
			 configuration = new Configuration().configure();
			 sessionFactory = configuration.buildSessionFactory();
			 session = sessionFactory.openSession();
			 transaction = session.beginTransaction();
			 //查询所有
		     List list=session.createCriteria(User.class).list();
			 //提交事物
	    	 transaction.commit();
	    	 session.close();
	    	 sessionFactory.close();
		     return list;
	}

	/**
	 * 添加
	 */
	public int AddUser(User user) {
		   configuration = new Configuration().configure();
		   sessionFactory = configuration.buildSessionFactory();
		   session = sessionFactory.openSession();
		   transaction = session.beginTransaction();
		   //添加一行信息
		   User users=new User(user.getU_name(), user.getU_sex());
		   Serializable Serializable = session.save(users);
	       int a=(Integer) Serializable;
	       System.out.println(a);
		   //提交事物
	   	   transaction.commit();
	       session.close();
	   	   sessionFactory.close();
		   return 0;
	}

	/**
	 * 删除
	 */
	public int DeleteUser(int uid) {
		   configuration = new Configuration().configure();
		   sessionFactory = configuration.buildSessionFactory();
		   session = sessionFactory.openSession();
		   transaction = session.beginTransaction();
		   //删除一行信息
		   User user=new User();
		   user.setU_id(uid);
		   session.delete(user);
		   //提交事物
	   	   transaction.commit();
	   	   session.close();
	   	   sessionFactory.close();
		   return 0;
	}

	/**
	 * 修改
	 */
	public int UpadteUser(User user) {
		   configuration = new Configuration().configure();
		   sessionFactory = configuration.buildSessionFactory();
		   session = sessionFactory.openSession();
		   transaction = session.beginTransaction();
		   //修改
		   User users=session.get(User.class, user.getU_id());
		   users.setU_name(user.getU_name());
		   users.setU_sex(user.getU_sex());
		   session.update(users);
		   //提交事物
	       transaction.commit();
	       session.close();
	   	   sessionFactory.close();
		   return 0;
	}
}

7.写Action

package com.yhl.action;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.yhl.dao.UserDao;
import com.yhl.dao.UserDaoImp;

import net.sf.json.JSONArray;

public class GetAllUserAtion extends ActionSupport{
		@Override
		public String execute(){
			   System.out.println("查看所有的Ation进来了。。。。。。");
			   UserDao userDao=new UserDaoImp();
			   try {
				     HttpServletResponse resp = ServletActionContext.getResponse();
				     resp.setContentType("text/html;charset=UTF-8");
				     JSONArray jsonArray=JSONArray.fromObject(userDao.getAll());
					 PrintWriter pw;
					 pw = resp.getWriter();
					 pw.write(jsonArray.toString());
					 pw.close();
			   } catch (Exception e) {
					 e.printStackTrace();
			   }
			   return null;
		}
	
	
}
package com.yhl.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.yhl.dao.UserDao;
import com.yhl.dao.UserDaoImp;
import com.yhl.entity.User;

public class AddAction extends ActionSupport{
	 HttpServletResponse resp=ServletActionContext.getResponse();
	 HttpServletRequest req=ServletActionContext.getRequest();
     UserDao dao=new UserDaoImp();
	 
     private int u_id;
     private String u_name;
     private String u_sex;
     
     @Override
	 public String execute() throws Exception {
    	    resp.setContentType("text/html; charset=UTF-8");
		    req.setCharacterEncoding("UTF-8");
		    dao.AddUser(new User(u_id, u_name, u_sex));
		    return null;
	 }
	
     public int getU_id() {
    	 return u_id;
     }
     public void setU_id(int u_id) {
    	 this.u_id = u_id;
     }
     public String getU_name() {
    	 return u_name;
     }
     public void setU_name(String u_name) {
    	 this.u_name = u_name;
     }
     public String getU_sex() {
    	 return u_sex;
     }
     public void setU_sex(String u_sex) {
    	 this.u_sex = u_sex;
     }
    
}
package com.yhl.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.yhl.dao.UserDao;
import com.yhl.dao.UserDaoImp;

public class DeleteAction extends ActionSupport{
	   HttpServletResponse resp=ServletActionContext.getResponse();
	   HttpServletRequest req=ServletActionContext.getRequest();
       UserDao dao=new UserDaoImp();
     
	   private int u_id;
	   @Override
	   public String execute() throws Exception {
			System.out.println("删除的Action进来了。。。。。。");
			 resp.setContentType("text/html; charset=UTF-8");
			 req.setCharacterEncoding("UTF-8");
			 System.out.println(u_id);
			 dao.DeleteUser(u_id);
			 return null;
	   }
	
		public int getU_id() {
			return u_id;
		}
		public void setU_id(int u_id) {
			this.u_id = u_id;
		}
	
}
package com.yhl.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.yhl.dao.UserDao;
import com.yhl.dao.UserDaoImp;
import com.yhl.entity.User;

public class UpdateAction extends ActionSupport {
	   HttpServletResponse resp=ServletActionContext.getResponse();
	   HttpServletRequest req=ServletActionContext.getRequest();
       UserDao dao=new UserDaoImp();
     
       private int u_id;
       private String u_name;
       private String u_sex;
       @Override
       public String execute() throws Exception {
    	   System.out.println("修改的Action进来了。。。。。。");
    	   resp.setContentType("text/html; charset=UTF-8");
    	   req.setCharacterEncoding("UTF-8");
    	   dao.UpadteUser(new User(u_id, u_name, u_sex));
    	   return null;
       }
	
       public int getU_id() {
    	   return u_id;
       }
       public void setU_id(int u_id) {
    	   this.u_id = u_id;
       }
       public String getU_name() {
    	   return u_name;
       }
       public void setU_name(String u_name) {
    	   this.u_name = u_name;
       }
       public String getU_sex() {
    	   return u_sex;
       }
       public void setU_sex(String u_sex) {
    	   this.u_sex = u_sex;
       }
}

8.写struts.xml配置



      
 	  
	     
		       
		       
		       
		       
	     

9.配置web.xml




  Archetype Created Web Application
  
  
  	
  		struts2
  		org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
  	
  	
  		struts2
  		*.action
  	


10.写jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Hibenate+Struts2+Maven+EasyUI实现增删查改


   

 
 







	


附:EasyUI下载地址







你可能感兴趣的:(Hibenate)