spring的HibernateDaoSupport类的用法

package dao.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import util.Pager;

import mode.User;
import dao.UserDao;

public class UserDaoHibernateImpl extends HibernateDaoSupport implements UserDao {

	public void delete(User user) {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(user);
	}

	public User getUserById(Serializable id) {
		// TODO Auto-generated method stub
		return (User) getHibernateTemplate().get(User.class, id);
	}

	public User getUserByNameAndPassword(String name, String password) {
		// TODO Auto-generated method stub
		List<User> list = getHibernateTemplate().find("from User where name='"+name+"' and password='"+password+"'");
		if(list.size()==1){
			return list.get(0);
		}
		return null;
	}

	public String save(User user) {
		// TODO Auto-generated method stub
		return (String) getHibernateTemplate().save(user);
	}

	public void update(User user) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(user);
	}

	@SuppressWarnings("unchecked")
	public List<User> list(final Pager pager) {
		// TODO Auto-generated method stub
		pager.setRowSize(getHibernateTemplate().loadAll(User.class).size());
		
		List<User> list = (List<User>) getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Query query = session.createQuery("from User u order by u.id");
				System.out.println(pager.getStart()+"----"+pager.getPageSize()+">>>>>>>"+pager);
				query.setFirstResult(pager.getStart()-1);
				query.setMaxResults(pager.getPageSize());
				return query.list();
			}
		});
		//List<User> list = getHibernateTemplate().loadAll(User.class).subList(pager.getStart(), pager.getStart()+pager.getPageSize());
		return list;
	}

}
 

你可能感兴趣的:(DAO,spring,sql,Hibernate,orm)