📄 userdao.java
字号:
package crm.dao.sys;
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 crm.entity.sys.UserEntity;
/**
* Data access object (DAO) for domain model class UserEntity.
*
* @see crm.entity.sys.UserEntity
* @author MyEclipse Persistence Tools
*/
public class UserDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(UserDAO.class);
// property constants
public static final String USR_NAME = "usrName";
public static final String USR_PASSWORD = "usrPassword";
public static final String USR_FLAG = "usrFlag";
protected void initDao() {
// do nothing
}
/**
* 根据用户的编号查找用户的权限ID
*/
public List getByUseRolerId(int userId)
{
Session session=this.getSession();
String hql="select user from UserEntity as user where user.usrId=?";
Query query=session.createQuery(hql);
query.setInteger(0, userId);
return query.list();
}
/**
* HQL 连接查询 inner join
* 根据权限ID(RoleEntity) 得到其所有的用户信息(UserEntity)
*/
public List getByRoleId(Long id)
{
String hql="select user from UserEntity as user inner join user.sysRole as role where role.roleId=?";
return (List)getHibernateTemplate().find(hql, id);
}
/**
* 根据客户经理的名字去查找客户经理的编号
*/
public List getByCustManagerId(String custManagerName)
{
Session session=this.getSession();
String hql="select user.usrId from UserEntity as user where user.usrName=?";
Query query=session.createQuery(hql);
query.setString(0, custManagerName);
List list=query.list();
return list;
}
public void save(UserEntity transientInstance) {
log.debug("saving UserEntity instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(UserEntity persistentInstance) {
log.debug("deleting UserEntity instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
/**
* 删除方法(根据id删除)
*/
public void delete(int id)
{
long lid=id; //将int型转换成long型
this.delete(this.findById(lid));
}
//按用户名查找编号
public int findName(String uname)
{
int id=0;
String hql="select user.usrId from UserEntity as user where user.usrName=?";
Query query=this.getSession().createQuery(hql);
query.setString(0, uname);
Iterator t=query.list().iterator();
if(t.hasNext())
{
t.next();
System.out.println(id);
}
return id;
}
public UserEntity findById(java.lang.Long id) {
log.debug("getting UserEntity instance with id: " + id);
try {
UserEntity instance = (UserEntity) getHibernateTemplate().get(
"crm.entity.sys.UserEntity", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(UserEntity instance) {
log.debug("finding UserEntity 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;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding UserEntity instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from UserEntity as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByUsrName(Object usrName) {
return findByProperty(USR_NAME, usrName);
}
public List findByUsrPassword(Object usrPassword) {
return findByProperty(USR_PASSWORD, usrPassword);
}
public List findByUsrFlag(Object usrFlag) {
return findByProperty(USR_FLAG, usrFlag);
}
public List findAll() {
log.debug("finding all UserEntity instances");
try {
String queryString = "from UserEntity";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public UserEntity merge(UserEntity detachedInstance) {
log.debug("merging UserEntity instance");
try {
UserEntity result = (UserEntity) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(UserEntity instance) {
log.debug("attaching dirty UserEntity instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(UserEntity instance) {
log.debug("attaching clean UserEntity instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static UserDAO getFromApplicationContext(ApplicationContext ctx) {
return (UserDAO) ctx.getBean("UserEntityDAO");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -