• Hibernate+Struts2进行数据的修改


    1.先把userid传给修改的页面

    2.跳转到修改的页面

    3.用request接收传入输入需改信息的页面

    传到action

    Action,  通过request获取id

    service层

    DAO层

    <%@page import="com.hanqi.entity.User"%>
    <%@page import="java.util.List"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>查询所有</title>
    </head>
    <body>
    <%
    List<User> lu = (List<User>)request.getAttribute("userlist");
    
    
    for(User u : lu)
    {
    	out.print(u+"【<a href='updateUser?userid="+u.getUserId()+
    			"'>修改</a>】"+"【<a href='deleteUser?userid="+u.getUserId()+"'>删除</a>】 <br>");
    }
    
    %>
    </body>
    </html>
    

      

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    	"http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    	<!-- 覆盖默认的过滤的扩展名 -->
    	<constant name="struts.action.extension" value="do,action,,"></constant>
    	
    	<!-- 定义包 -->
    	<package name="text" extends="struts-default" >
    	
    		<action name="addUser">
    			<result>/WEB-INF/pages/addUser.jsp</result>
    		</action>
    		
    		<!--保存user  -->
    		<action name="saveUser" class="com.hanqi.action.UserAction" method="saveUser">
    		
    			<result name="fail">/WEB-INF/pages/fail.jsp</result>
    			<result name="success">/WEB-INF/pages/success.jsp</result>
    		</action>
    		
    		<!-- 查询用户 -->
    		<action name="selectUser" class="com.hanqi.action.UserAction" method="selectUser">
    			<result name="fail">/WEB-INF/pages/fail.jsp</result>
    			<result name="success">/WEB-INF/pages/selectUser.jsp</result>
    		</action>
    		
    		<!-- 删除 -->
    		<action name="deleteUser" class="com.hanqi.action.UserAction" method="deleteUser">
    			<result name="fail">/WEB-INF/pages/fail.jsp</result>
    			<result name="success" type="redirectAction">selectUser</result>
    		</action>
    		
    		<!-- 修改 ,跳转到修改页面-->
    		<action name="updateUser" >
    			<result>/WEB-INF/pages/updateUser.jsp</result>
    			<result name="fail">/WEB-INF/pages/fail.jsp</result>
    		</action>
    		<!-- 修改输入 -->
    		<action name="updateUser2"  class="com.hanqi.action.UserAction" method="updateUser">
    			<result name="fail">/WEB-INF/pages/fail.jsp</result>
    			<result name="success" type="redirectAction">selectUser</result>
    		</action>
    		
    		
    	</package>
    
    </struts>
    

      

    <%@page import="com.hanqi.dao.UserDAO"%>
    <%@page import="com.hanqi.service.UserService"%>
    <%@page import="com.hanqi.entity.User"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>请输入修改内容</title>
    </head>
    <body>
    <%
    //得到selectUser传过来的userid,用request接收再向下传
    int userID = Integer.parseInt(request.getParameter("userid"));
    out.print(userID);
    UserDAO ud =new UserDAO();
    User u=ud.getUser(userID);//利用获取到的userid得到user对象,便于在文本框里显示
    %>
    <form action="updateUser2" method="post">
    用户id:<input type="text" name="user.userID"value="<%=u.getUserId() %>" readonly><br>
    用户姓名:<input type="text" name="user.userName"value="<%=u.getUserName() %>"><br>
    用户生日:<input type="text" name="user.birthday"value="<%=u.getUserName() %>"><br>
    用户薪酬:<input type="text" name="user.money"value="<%=u.getMoney() %>"><br>
    <br><br>
    <input type="submit" value="提交修改">
    
    
    </form>
    
    </body>
    </html>
    

      

    package com.hanqi.action;
    
    import java.util.List;
    import java.util.Map;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.hanqi.entity.User;
    import com.hanqi.service.UserService;
    import com.opensymphony.xwork2.ActionContext;
    
    public class UserAction {
    	
    	//域模型方式
    	private User user;
    	
    	public User getUser() {
    		return user;
    	}
    
    	public void setUser(User user) {
    		this.user = user;
    	}
    
    
    	//处理保存user的方法
    	public String saveUser()
    	{
    		String rtn="fail";
    		
    		System.out.println(user);
    		
    		try
    		{
    		//调用业务逻辑层Service(模型层)
    		User u1 = new UserService().add(user);
    		
    		//使用request转发到下一个页面去
    		ActionContext  ac = ActionContext.getContext();
    		Map<String, Object> mo = (Map<String, Object>)ac.get("request");
    		mo.put("user", u1);
    		
    		rtn="success";
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		return rtn;
    	}
    	
    	//查询用户
    	public String selectUser()
    	{
    		String rtn ="fail";
    		try
    		{
    			//调用查询的方法
    			
    			List<User>  lu = new UserService().getAll();
    			//得到原生 的request
    			HttpServletRequest hsr = ServletActionContext.getRequest();
    			hsr.setAttribute("userlist", lu);
    			
    			rtn="success";
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		return rtn;
    		
    	}
    	
    	
    	
    	//删除用户
    	public String deleteUser()
    	{
    		String rtn ="fail";
    		try
    		{
    			//获取userid
    			//得到原生 的request
    			HttpServletRequest hsr = ServletActionContext.getRequest();
    			String  uid = hsr.getParameter("userid");
    			//执行删除
    			new UserService().delete(Integer.parseInt(uid));
    			
    			rtn="success";
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		
    		return rtn;
    	}
    	
    	//修改用户
    	
    	public String updateUser()
    	{
    		String rtn ="fail";
    		
    		try
    		{
    
    			//获取userid
    			//得到原生 的request
    			HttpServletRequest hsr = ServletActionContext.getRequest();
    			String userid = hsr.getParameter("user.userID");//通过request获取页面传过的id
    
    			//调用业务逻辑层Service(模型层)
    			 int userID = Integer.parseInt(userid) ;//吧页面传过来的userid转换成DAO层的int型进行对应
    			 //System.out.println(userID);
    			 new UserService().update(userID, user);
    			
    			
    			rtn="success";
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		
    		return rtn;
    	}
    	
    	//查询单个
    	public String selectOneUser()
    	{
    		String rtn ="fail";
    		try
    		{
    			//调用查询单个的方法
    			//获取userid
    			//得到原生 的request
    			HttpServletRequest hsr = ServletActionContext.getRequest();
    			String userid = hsr.getParameter("user.userID");
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		
    		return rtn;
    	}
    
    }
    

      

    package com.hanqi.service;
    
    import java.util.List;
    
    import com.hanqi.dao.UserDAO;
    import com.hanqi.entity.User;
    
    public class UserService {
    	
    	//添加User
    	public User add(User user)
    	{
    		return new UserDAO().insert(user);
    	}
    
    	
    	//查询全表
    	public List<User>  getAll()
    	{
    		return new UserDAO().getAll();
    	}
    	
    	
    	//删除
    	public void delete(int userID)
    	{
    		new UserDAO().delete(userID);
    	}
    	
    	//修改
    	public void update(int userID, User user)
    	{
    		new UserDAO().updateUser(userID, user);
    	}
    	//查询单个
    	public User getOntUser(int userID)
    	{
    		return new UserDAO().getUser(userID);
    	}
    }
    

      

    package com.hanqi.dao;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.ServiceRegistry;
    
    import com.hanqi.entity.User;
    
    public class UserDAO {
    	private Configuration cfg =null;
    	private ServiceRegistry  sr =null;
    	private SessionFactory  sf =null;
    	private Session  se =null;
    	private Transaction  tr =null;
    	//构造方法
    	public UserDAO()
    	{
    		//初始化Hibernate
    		cfg= new Configuration().configure();//获取配置文件
    		sr= new StandardServiceRegistryBuilder()//注册
    				.applySettings(cfg.getProperties())
    				.build();
    	}
    	
    	//
    	private void init()
    	{
    		sf = cfg.buildSessionFactory(sr);
    		se =sf.openSession();
    		tr = se.beginTransaction();
    	}
    	private void destory()
    	{
    		tr.commit();
    		se.close();
    		sf.close();
    	}
    	
    	//保存user
    	public User insert(User user)//添加
    	{
    	
    		init();
    		se.save(user);
    		destory();
    		return user;
    		
    	}
    	
    	//查询列表
    	public List<User> getAll()
    	{
    		List<User> rtn = new ArrayList<>();
    		init();
    		rtn = se.createQuery("from User").list();  //类名
    		
    		destory();
    		return rtn;
    	}
    	
    	//删除
    	public void delete(int userID)
    	{
    		init();
    		//获取对象
    		User u = (User)se.get(User.class, userID);
    		se.delete(u);
    		
    		destory();
    	}
    
    	
    	//修改
    	public void updateUser(int userID, User user)
    	{
    		init();
    		//获取对象
    		User u = (User)se.get(User.class, userID);//通过session获取userID的信息
    		u.setUserId(userID);
    		u.setUserName(user.getUserName());
    		u.setMoney(user.getMoney());
    		u.setBirthday(user.getBirthday());
    		
    		destory();
    		
    	}
    	//查询单条记录
    	public User getUser (int userID)
    	{
    		User rtn = new User();
    		init();
    		rtn = (User)se.get(User.class, userID);
    		
    		
    		destory();
    		return rtn;
    		
    	}
    	
    }
    

      

  • 相关阅读:
    BZOJ3631: [JLOI2014]松鼠的新家
    网络流24题题目总会+题解
    BZOJ3930: [CQOI2015]选数
    BZOJ4816: [Sdoi2017]数字表格
    Launcher类源码分析
    平台特定的启动类加载器深入分析与自定义系统类加载器详解
    类加载器命名空间总结与扩展类加载器要点分析
    类加载器命名空间深度解析与实例分析
    类加载器实战剖析与疑难点解析
    类加载器命名空间实战剖析与透彻理解
  • 原文地址:https://www.cnblogs.com/liuyanzeng/p/6073010.html
Copyright © 2020-2023  润新知