package com.rx.dao;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.rx.entity.User;
public class BaseDao<T> implements AbstractDao<T> {
@Resource(name = "hibernateTemplate")
protected HibernateTemplate hibernateTemplate;
private Class<T> clazz;
@SuppressWarnings("unchecked")
public BaseDao() {
clazz = (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
public void delete(T entity) {
hibernateTemplate.delete(entity);
}
@SuppressWarnings("unchecked")
public List<T> list() {
return hibernateTemplate.loadAll(clazz);
}
@SuppressWarnings("unchecked")
public T save(T entity) {
return (T) hibernateTemplate.save(entity);
}
public void update(T entity) {
hibernateTemplate.update(entity);
}
@SuppressWarnings("unchecked")
public T get(Long id) {
return (T) hibernateTemplate.get(clazz, id);
}
@SuppressWarnings("unchecked")
public int verify(String username,String password){
List<User> users = hibernateTemplate.find("from User where username = '"+username+"'");
if(users.size()>0){
User user = users.get(0);
if(user.getPassword().equals(password)){
return 1;
}else{
return 0;
}
}else{
return -1;
}
}
}