首页>代码>jsp+struts2权限管理系统>/第24章/lams/src/sunyang/functions/dao/FunctionDAO.java
package sunyang.functions.dao;

import java.util.List;

import javax.persistence.*;

import sunyang.functions.domain.Function;
import sunyang.persistence.EntityManagerHelper;

public class FunctionDAO implements IFunctionDAO {
	// 声明静态常量
	public static final String URL = "url";
	public static final String FUNCTIONNAME = "functionname";

	// 得到实体管理器
	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	// 新增数据
	public void save(Function entity) {
		EntityManagerHelper.beginTransaction();
		try {
			getEntityManager().persist(entity);
			EntityManagerHelper.commit();
		} catch (RuntimeException re) {
			EntityManagerHelper.rollback();
			throw re;
		}
	}

	// 删除数据
	public void delete(Function entity) {
		EntityManagerHelper.beginTransaction();
		try {
			entity = getEntityManager().getReference(Function.class,
					entity.getId());
			getEntityManager().remove(entity);
			EntityManagerHelper.commit();
		} catch (RuntimeException re) {
			EntityManagerHelper.rollback();
			throw re;
		}
	}

	// 修改数据
	public Function update(Function entity) {
		EntityManagerHelper.beginTransaction();
		try {
			Function result = getEntityManager().merge(entity);
			EntityManagerHelper.commit();
			return result;
		} catch (RuntimeException re) {
			EntityManagerHelper.rollback();
			throw re;
		}
	}

	// 通过id查询数据
	public Function findById(Integer id) {
		try {
			Function instance = getEntityManager().find(Function.class,
					id);
			return instance;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	// 通过表中一个字段查询数据
	@SuppressWarnings("unchecked")
	public List<Function> findByProperty(String propertyName,
			final Object value) {
		try {
			final String queryString = "select model from Function model where model."
					+ propertyName + "= :propertyValue";
			Query query = getEntityManager().createQuery(queryString).setHint(
					"toplink.refresh", true);
			query.setParameter("propertyValue", value);
			return query.getResultList();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	// 查询所有数据
	@SuppressWarnings("unchecked")
	public List<Function> findAll() {
		try {
			final String queryString = "select model from Function model";
			Query query = getEntityManager().createQuery(queryString).setHint(
					"toplink.refresh", true);
			return query.getResultList();
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
最近下载更多
hesu2020  LV1 2023年3月21日
微信网友_6044559891320832  LV2 2022年7月14日
2022982038  LV1 2022年1月27日
lizhiquan_119  LV2 2020年9月4日
281273445  LV3 2020年6月18日
qijie_nm  LV1 2020年5月29日
luojiaqi  LV1 2019年11月9日
11ssjhw  LV1 2019年10月16日
A1231Q  LV1 2019年6月18日
122582456  LV1 2019年6月17日
最近浏览更多
操作者 4月12日
暂无贡献等级
朱俪的邮件及存储  LV8 3月11日
故里逢春  LV2 2023年12月25日
微信网友_6714345438400512 2023年10月30日
暂无贡献等级
kxjh星辰  LV6 2023年10月25日
jiemomo  LV12 2023年10月19日
微信网友_6698455742353408 2023年10月19日
暂无贡献等级
1997akkk  LV5 2023年8月8日
hesu2020  LV1 2023年3月21日
微信网友_6248713511227392  LV11 2022年12月5日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友