📄 transportdaoimpl.java
字号:
package com.shunshi.ssh.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.shunshi.ssh.entity.Goods;
import com.shunshi.ssh.entity.Transport;
import com.shunshi.ssh.exception.GoodsDaoException;
import com.shunshi.ssh.exception.TransportDaoException;
import com.shunshi.ssh.exception.TransportServiceException;
public class TransportDaoImpl extends HibernateDaoSupport implements ITransportDao {
public void add(Transport transport) throws TransportDaoException {
try{
System.out.println("dao add() ....");
getHibernateTemplate().saveOrUpdate(transport);
}catch(DataAccessException e){
e.printStackTrace();
//throw new TransportServiceException(e.getMessage());
}
}
public void delete(long id) throws TransportDaoException{
try{
System.out.println("dao delete() ....");
getHibernateTemplate().delete(findById(id));
}catch(DataAccessException e){
throw new TransportDaoException(e.getMessage());
}
}
public List findAll() throws TransportDaoException {
//try{
//System.out.println("dao findAll() ....");
String queryString = "from Transport";
return getHibernateTemplate().find(queryString);
//}catch(DataAccessException e){
// throw new TransportServiceException(e.getMessage());
//}
}
public Transport findById(long id) throws TransportDaoException {
try{
System.out.println("dao findById() ....");
Transport t = (Transport) getHibernateTemplate().get(
"com.shunshi.ssh.entity.Transport", id);
return t;
}catch(DataAccessException e){
throw new TransportDaoException(e.getMessage());
}
}
public List findByUserId(long userId) throws TransportDaoException {
try{
System.out.println("dao findByUserId() ....");
String queryString = "from Transport t where t.userId=?";
return getHibernateTemplate().find(queryString,new Long(userId));
}catch(DataAccessException e){
throw new TransportDaoException(e.getMessage());
}
}
public void updateTransport(Transport transport) throws TransportDaoException{
try{
System.out.println("dao updateGoods() ....");
getHibernateTemplate().update(transport);
}catch(DataAccessException e){
throw new TransportDaoException(e.getMessage());
}
}
public void updateState(long id, int state) throws TransportDaoException {
try{
System.out.println("dao updateState() ....");
Transport t=findById(id);
if(t!=null)
t.setState(new Integer(state));
getHibernateTemplate().update(t);
}catch(DataAccessException e){
throw new TransportDaoException(e.getMessage());
}
}
public List findByState(final int state, final int start, final int max) throws TransportDaoException {
List transportInfos=(List)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query q=session.createQuery("from Transport t where t.state=:state");
q.setInteger("state",state);
q.setMaxResults(max);
q.setFirstResult(start);
List transportInfos =new ArrayList();
List<Transport> ts=q.list();
for(Transport t:ts){
Map transportMap=new HashMap();
transportMap.put("topic",t.getTopic());
transportMap.put("publishDate",t.getPublishDate());
transportMap.put("userName",t.getUserName());
transportMap.put("state",t.getState());
transportMap.put("id",t.getId());
transportInfos.add(transportMap);
}
return transportInfos;
}
});
return transportInfos;
}
public Long getTotalRowsByState(int state) throws TransportDaoException {
try{
return (Long)getHibernateTemplate().find("select count(*) from Transport t where t.state=?",state).get(0);
}catch(RuntimeException e){
throw new TransportDaoException(e.getMessage());
}
}
public Transport findTransportByMaxId() throws GoodsDaoException {
String hql="select Max(id) from Transport transport";
Long id=(Long)getHibernateTemplate().find(hql).get(0);
Transport transport=null;
try {
transport=findById(id.longValue());
} catch (TransportDaoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return transport;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -