📄 roledaoimpl.java
字号:
package yyxtong.hibernate.dao.baseimpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import yyxtong.hibernate.dao.base.IRoleDAO;
import yyxtong.hibernate.pojo.Role;
import yyxtong.hibernate.pojo.RoleModule;
/**
* A data access object (DAO) providing persistence and search support for Role
* entities. Transaction control of the save(), update() and delete() operations
* can directly support Spring container-managed transactions or they can be
* augmented to handle user-managed Spring transactions. Each of these methods
* provides additional information for how to configure it for the desired type
* of transaction control.
*
* @see yyxtong.hibernate.pojo.Role
* @author MyEclipse Persistence Tools
*/
public class RoleDAOImpl extends HibernateDaoSupport implements IRoleDAO {
private static final Log log = LogFactory.getLog(RoleDAOImpl.class);
// property constants
public static final String ROLE = "role";
protected void initDao() {
// do nothing
}
/*
* (non-Javadoc)
*
* @see yyxtong.hibernate.dao.baseimpl.IRoleDAO#save(yyxtong.hibernate.pojo.Role)
*/
public void save(Role transientInstance) {
log.debug("saving Role instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
/*
* (non-Javadoc)
*
* @see yyxtong.hibernate.dao.baseimpl.IRoleDAO#delete(yyxtong.hibernate.pojo.Role)
*/
public void delete(Role persistentInstance) {
log.debug("deleting Role instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
/*
* (non-Javadoc)
*
* @see yyxtong.hibernate.dao.baseimpl.IRoleDAO#findById(java.lang.Integer)
*/
public Role findById(java.lang.Integer id) {
log.debug("getting Role instance with id: " + id);
try {
Role instance = (Role) getHibernateTemplate().get(
"yyxtong.hibernate.pojo.Role", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
/*
* (non-Javadoc)
*
* @see yyxtong.hibernate.dao.baseimpl.IRoleDAO#findByExample(yyxtong.hibernate.pojo.Role)
*/
public List findByExample(Role instance) {
log.debug("finding Role instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
/*
* (non-Javadoc)
*
* @see yyxtong.hibernate.dao.baseimpl.IRoleDAO#findByProperty(java.lang.String,
* java.lang.Object)
*/
public List findByProperty(String propertyName, Object value) {
log.debug("finding Role instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Role as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
/*
* (non-Javadoc)
*
* @see yyxtong.hibernate.dao.baseimpl.IRoleDAO#findByRole(java.lang.Object)
*/
public List findByRole(Object role) {
return findByProperty(ROLE, role);
}
/*
* (non-Javadoc)
*
* @see yyxtong.hibernate.dao.baseimpl.IRoleDAO#findAll()
*/
public List findAll() {
log.debug("finding all Role instances");
try {
String queryString = "from Role";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
/*
* (non-Javadoc)
*
* @see yyxtong.hibernate.dao.baseimpl.IRoleDAO#merge(yyxtong.hibernate.pojo.Role)
*/
public Role merge(Role detachedInstance) {
log.debug("merging Role instance");
try {
Role result = (Role) getHibernateTemplate().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
/*
* (non-Javadoc)
*
* @see yyxtong.hibernate.dao.baseimpl.IRoleDAO#attachDirty(yyxtong.hibernate.pojo.Role)
*/
public void attachDirty(Role instance) {
log.debug("attaching dirty Role instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
/*
* (non-Javadoc)
*
* @see yyxtong.hibernate.dao.baseimpl.IRoleDAO#attachClean(yyxtong.hibernate.pojo.Role)
*/
public void attachClean(Role instance) {
log.debug("attaching clean Role instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
/**
* Role和Module 连接查询角色名称和所属模块
*
* @return
*/
public List findRoleModule(){
Session session = getHibernateTemplate().getSessionFactory().openSession();//打开数据库会话
String hql = "select r.id,r.role,m.id,m.module from Role r join r.module m";
Query query = session.createQuery(hql);//执行查询语句
List list = query.list();//获取list集合
try {
if (!list.isEmpty()) {//判断不为空
Iterator it = list.iterator();//获得迭代器
List list_return = new ArrayList();
while (it.hasNext()) {//while存在下一个值
RoleModule re = new RoleModule();
Object[] obj = (Object[]) it.next();//迭代器返回一个Object[]数组
re.setRoleid_id((Integer) obj[0]);
re.setRole_name((String)obj[1]);
re.setModule_id((Integer)obj[2]);
re.setModule_name((String)obj[3]);//为RoleEmp对象赋植
list_return.add(re);
}
return list_return;//返回对象集合
}else{
return null;//否则返回空
}
} catch (Exception e) {
e.printStackTrace();//打印异常信息
return null;
} finally {
session.close();//关闭数据库会话
}
}
public void addRole(){
Session session = getHibernateTemplate().getSessionFactory().openSession();//打开数据库会话
String hql = "select r.id,r.role,m.id,m.module from Role r join r.module m";
Query query = session.createQuery(hql);//执行查询语句
}
public static IRoleDAO getFromApplicationContext(ApplicationContext ctx) {
return (IRoleDAO) ctx.getBean("RoleDAOImpl");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -