Hibernate实现最基本的增删改查

Hibernate提供了丰富的API对数据库进行操作。

其中Session是对数据库进行操作一个非常重要的类,他提供了对数据库增删改查的基本操作。

下面看一些具体示例,实例中实现了对数据库增加,删除,更新,根据ID查询,查询全部,分页查询,以及模糊查询

package dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import sessionfactory.HibernateSessionFactory;
import entity.Login;

public class BaseDao {
	// 保存用户
	public void save(Login login) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.save(login);
			tran.commit();
			System.out.println("添加成功!");
		} catch (HibernateException e) {
			tran.rollback();
			System.out.println("插入失败!");
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	// 删除用户
	public void delete(Login login) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.delete(login);
			System.out.println("删除成功");
			tran.commit();
		} catch (Exception e) {
			System.out.println("删除失败");
			tran.rollback();
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	// 更新用户信息
	public void update(Login login) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.update(login);
			tran.commit();
			System.out.println("更新成功!");
		} catch (Exception e) {
			tran.rollback();
			System.out.println("更新失败!");
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	// 根据用户id查询
	public Login queryById(int id) {
		Login login =null;
		Session session = HibernateSessionFactory.getSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			login = (Login) session.get(Login.class, id);
			tran.commit();
		} catch (Exception e) {
			tran.rollback();
			System.out.println("未找到用户!");
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return login;
	}

	// 查询全部
	public List<Login> queryAll() {
		Session session = HibernateSessionFactory.getSession();
		String hql = "FROM Login";
		List<Login> list = null;
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			Query query = session.createQuery(hql);
			list = query.list();
			tran.commit();
		} catch (Exception e) {
			tran.rollback();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return list;
	}

	// 模糊查询
	public List<Login> queryLike(String name) {
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Login as l where l.username like ?";
		Query query = session.createQuery(hql);
		query.setString(0, "%" + name + "%");
		List<Login> list = query.list();
		return list;
	}

	// 分页查询
	public QueryResult queryAll(int firstResult, int maxResult) {
		QueryResult qr = null;
		Session session = HibernateSessionFactory.getSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			Query query = session.createQuery("FROM Login");
			List<Login> list = query.list();
			int totalCount = list.size();
			int pageSize = (totalCount%maxResult==0)?(totalCount/maxResult):(totalCount/maxResult+1);
			query.setFirstResult(firstResult);
			query.setMaxResults(maxResult);
			List<Login> listSize = query.list();
			qr = new QueryResult(pageSize, listSize);
			tran.commit();
		} catch (Exception e) {
			tran.rollback();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return qr;
	}
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。