Struts2+Hibernate 个人信息管理项目

  • I9_547815
    了解作者
  • 5.4MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-14 05:16
    上传日期
非常容易上手的Struts2+Hibernate框架开发的个人信息管理项目,MySQL,MyEclipse
PIM.zip
内容介绍
package cn.edu.ldu.dao; import java.util.List; import javax.swing.JOptionPane; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import cn.edu.ldu.factory.HibernateSessionFactory; import cn.edu.ldu.model.Friends; import cn.edu.ldu.model.User; public class FriendsInfo { private Session session; private Query query; private Transaction transaction; private List<Friends> fri_list; public FriendsInfo() { } // 查询所有好友 public List<Friends> selectAllFriends(String userName) { session = HibernateSessionFactory.getSession(); try { String hql = "from Friends as f where f.userName=:userName"; transaction = session.beginTransaction(); query = session.createQuery(hql); query.setString("userName", userName); fri_list = query.list(); transaction.commit(); return fri_list; } catch (Exception e) { e.printStackTrace(); return null; } } // 插入好友 public String insertFriend(Friends friends) { String mess = "error"; session = HibernateSessionFactory.getSession(); try { transaction = session.beginTransaction(); session.save(friends); transaction.commit(); mess = "success"; return mess; } catch (Exception e) { message("insertFriend.error:" + e); e.printStackTrace(); return "input"; } } // 模糊查询好友 public List<Friends> selectFriends(String friendName) { session = HibernateSessionFactory.getSession(); try { String hql = "from Friends as f where f.friendName like :friendName"; transaction = session.beginTransaction(); query = session.createQuery(hql); query.setString("friendName", "%" + friendName + "%"); fri_list = query.list(); transaction.commit(); return fri_list; } catch (Exception e) { e.printStackTrace(); return null; } } // 根据id查询好友 public List<Friends> selectFriendsById(int id) { session = HibernateSessionFactory.getSession(); try { String hql = "from Friends as f where f.id=:id"; transaction = session.beginTransaction(); query = session.createQuery(hql); query.setInteger("id",id); fri_list = query.list(); transaction.commit(); return fri_list; } catch (Exception e) { e.printStackTrace(); return null; } } // 删除好友 public String deleteFriend(int id) { String mess = "error"; session = HibernateSessionFactory.getSession(); try { String hql = "delete Friends as f where f.id=:id"; transaction = session.beginTransaction(); query = session.createQuery(hql); query.setInteger("id", id); int count = query.executeUpdate(); transaction.commit(); if (count > 0) { mess = "success"; } mess = "input"; return mess; } catch (Exception e) { message("deleteFriend.error:" + e); e.printStackTrace(); return "input"; } } // 更新好友信息 public String updateFriInfo(Friends friends) { String mess = "error"; session = HibernateSessionFactory.getSession(); try { String hql = "update Friends as f set f.friendName=:friendName,f.friendPhone=:friendPhone,f.friendEmail=:friendEmail,f.friendPlace=:friendPlace where f.id=:id"; transaction = session.beginTransaction(); query = session.createQuery(hql); query.setString("friendName", friends.getFriendName()); query.setString("friendPhone", friends.getFriendPhone()); query.setString("friendEmail", friends.getFriendEmail()); query.setString("friendPlace", friends.getFriendPlace()); query.setInteger("id", friends.getId()); query.executeUpdate(); transaction.commit(); mess = "success"; return mess; } catch (Exception e) { message("updateFriInfo.error:" + e); e.printStackTrace(); return "input"; } } // 信息提示 public void message(String mess) { int type = JOptionPane.YES_NO_OPTION; String title = "提示信息"; JOptionPane.showMessageDialog(null, mess, title, type); } }
评论
    相关推荐