⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 powerdao.java

📁 用hibernate实现用户权限管理模块
💻 JAVA
字号:
package dao;

import java.util.List;

import org.hibernate.*;

import vo.*;
import factory.*;

public class PowerDao {
	public void insertPower(Power power) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(power);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println(e);
		} finally {
			session.close();
		}
	}

	public void updatePower(Power power) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(power);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println(e);
		} finally {
			session.close();
		}
	}

	public void deletePower(Power power) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.delete(power);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println(e);
		} finally {
			session.close();
		}
	}

	public Power searchPower(int id) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		Power power = null;
		try {
			tx = session.beginTransaction();
			power = (Power) session.get(Power.class, Integer.valueOf(id));
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println(e);
		} finally {
			session.close();
		}
		return power;
	}

	public List searchAll() {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		List list = null;
		try {
			tx = session.beginTransaction();
			list = session.createQuery("from Power").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println(e);
		} finally {
			session.close();
		}
		return list;
	}

	public List searchByTitle(String title) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		List list = null;
		try {
			tx = session.beginTransaction();
			list = session.createQuery(
					"from Power p where p.title like '%"+title+"%'").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			System.out.println(e);
		} finally {
			session.close();
		}
		return list;
	}

	public static void main(String args[]) {
		Power power = new Power();
		// power.setTitle("用户管理");
		// power.setContent("userManager");
		PowerDao pdao = new PowerDao();
		// pdao.insertPower(power);
		// power=pdao.searchPower(1);
		// power.setContent("userManage");
		// pdao.updatePower(power);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -