📄 agentdaoimpl.java
字号:
package com.shunshi.ssh.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.shunshi.ssh.entity.Agent;
import com.shunshi.ssh.exception.AgentDaoException;
import com.shunshi.ssh.exception.AgentServiceException;
public class AgentDaoImpl extends HibernateDaoSupport implements IAgentDao {
public List findByState(final int state,final int start, final int max){
List agentInfos=(List)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query q=session.createQuery("from Agent a where a.state=:state");
q.setInteger("state",state);
q.setMaxResults(max);
q.setFirstResult(start);
List<Agent> agents=q.list();
return agents;
}
});
return agentInfos;
}
public void add(Agent agent) throws AgentDaoException {
try{
getHibernateTemplate().save(agent);
}catch (RuntimeException e) {
throw new AgentDaoException(e.getMessage());
}
}
public void delete(int id) throws AgentDaoException {
try{
Agent a=this.findById(id);
if(a!=null){
getHibernateTemplate().delete(a);
}
}catch(RuntimeException e){
throw new AgentDaoException(e.getMessage());
}
}
public Agent findById(int id) throws AgentDaoException {
Agent agent=null;
try{
agent=(Agent)getHibernateTemplate().get(com.shunshi.ssh.entity.Agent.class, id);
}catch(RuntimeException e){
throw new AgentDaoException(e.getMessage());
}
return agent;
}
public List findByUserId(int uid) throws AgentDaoException {
List l=null;
try{
l=getHibernateTemplate().find("from Agent a where a.userId=?",uid);
}catch(RuntimeException e){
throw new AgentDaoException(e.getMessage());
}
return l;
}
public void updateAgent(Agent a) throws AgentDaoException {
try{
getHibernateTemplate().saveOrUpdate(a);
}catch(RuntimeException e){
throw new AgentDaoException(e.getMessage());
}
}
public void updateState(int id, int state) throws AgentDaoException {
Agent a=null;
try {
a = this.findById(id);
if(a!=null){
a.setState(state);
}
getHibernateTemplate().saveOrUpdate(a);
}catch(RuntimeException e){
throw new AgentDaoException(e.getMessage());
}
}
public Long getTotalRowsByState(int state) throws AgentDaoException {
try{
return (Long)getHibernateTemplate().find("select count(*) from Agent a where a.state=?",state).get(0);
}catch(RuntimeException e){
throw new AgentDaoException(e.getMessage());
}
}
public Agent findByMaxId() throws AgentDaoException {
String hql="select Max(id) from Agent agent";
Integer id=(Integer)getHibernateTemplate().find(hql).get(0);
Agent agent=null;
try {
agent=findById(id.intValue());
} catch (AgentDaoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return agent;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -