hibernateDAO层基本的增删改查

完整的学习项目放在了我的github上,是一个半成品的在线音乐网站。

hibernate版本1.4 下面是userDAO 即对user表进行增删改查

package DAO;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import Bean.HibernateSessionFactory;
import Bean.User;

public class UserDAO {
    
    public static void insertUser(User user) {
        Transaction tx = null;
        try {
            Session session = HibernateSessionFactory.getSessionFactory()
                    .openSession();
            tx = session.beginTransaction();
            session.save(user);

            tx.commit();

        } catch (HibernateException e) {
            e.printStackTrace();
            tx.rollback();
        }
        HibernateSessionFactory.closeSession();
    }//add
    
    
    public static void deleteUser(String userId) {
        Transaction tx = null;
        try {
            User user = getUser(userId);
            Session session = HibernateSessionFactory.getSessionFactory()
                    .openSession();
            tx = session.beginTransaction();
            session.delete(user);
            tx.commit();

        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        }
        HibernateSessionFactory.closeSession();
    }//delete
    
    
    public static void updateUser(User user) {
        Transaction tx = null;
        try {
            Session session = HibernateSessionFactory.getSessionFactory()
                    .openSession();
            tx = session.beginTransaction();
            session.update(user);
            tx.commit();

        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        }
        HibernateSessionFactory.closeSession();
    }//update
    
    
    public static User getUser(String userId) {
        Transaction tx = null;
        User user = null;
        try {
            Session session = HibernateSessionFactory.getSessionFactory()
                    .openSession();
            tx = session.beginTransaction();
            user = (User) session.get(User.class, userId);
            tx.commit();

        } catch (HibernateException e) {
            e.printStackTrace();
            tx.rollback();
        }
        HibernateSessionFactory.closeSession();
        return user;
    }//get one
    
    public static List<User> getUsers() {
        Transaction tx = null;
        List<User> list = null;
        try {
            Session session = HibernateSessionFactory.getSessionFactory()
                    .openSession();
            tx = session.beginTransaction();
            Query query = session.createQuery("from User order by userId desc");
            list = query.list();
            tx.commit();

        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        }
        HibernateSessionFactory.closeSession();
        return list;
    }// get all
    
    public static List<User> checkUser(String userId,String userPwd) {
        Transaction tx = null;
        List<User> list = null;
        try {
            Session session = HibernateSessionFactory.getSessionFactory()
                    .openSession();
            tx = session.beginTransaction();
            Query query = session.createQuery("from User as u where u.userId=:userId and u.userPwd=:userPwd");
            query.setString("userId", userId);    
            query.setString("userPwd", userPwd);
            list = query.list();                            
            tx.commit();}
        catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        }
        HibernateSessionFactory.closeSession();
        return list;
    }
    
    
    //测试DAO
    public static void main(String[] args){
        List<User> list = UserDAO.getUsers();
        
        System.out.println("½øÈë²âÊÔº¯Êý");
        
        for(int i=0;i<list.size();i++){
            User user= new User();
            user = list.get(i);
            System.out.println(user.getUserId());
        }
    }

}

 

转载于:https://www.cnblogs.com/ryan255/p/5426847.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值