Java 通用DAO,类<;T>;是空的
我正在实现GenericDao。我有两个方法的问题-getAll()和getById(长id),实体类有空值。看起来类没有设置。我怎样才能解决这个问题Java 通用DAO,类<;T>;是空的,java,spring,hibernate,generics,dao,Java,Spring,Hibernate,Generics,Dao,我正在实现GenericDao。我有两个方法的问题-getAll()和getById(长id),实体类有空值。看起来类没有设置。我怎样才能解决这个问题 @Repository public class GenericDaoImpl<T> implements GenericDao<T> { private Class<T> clazz; @Autowired SessionFactory sessionFactory; public void set
@Repository
public class GenericDaoImpl<T> implements GenericDao<T> {
private Class<T> clazz;
@Autowired
SessionFactory sessionFactory;
public void setClazz(final Class<T> clazzToSet) {
this.clazz = clazzToSet;
}
public T getById(final Long id) {
return (T) this.getCurrentSession().get(this.clazz, id);
}
public List<T> getAll() {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
this.clazz);
return criteria.list();
}
protected final Session getCurrentSession() {
return this.sessionFactory.getCurrentSession();
}
}
@存储库
公共类GenericDaoImpl实现GenericDao{
私人课堂;
@自动连线
会话工厂会话工厂;
公共无效集合(最终类别集合){
this.clazz=clazzToSet;
}
公共T getById(最终长id){
返回(T)this.getCurrentSession().get(this.clazz,id);
}
公共列表getAll(){
Criteria=sessionFactory.getCurrentSession().createCriteria(
这是我的名字;
返回条件。list();
}
受保护的最终会话getCurrentSession(){
返回此.sessionFactory.getCurrentSession();
}
}
潘松道
public interface PersonDao extends GenericDao<Person> { }
公共接口PersonDao扩展了GenericDao{}
个人的
@Repository("PersonDAO")
public class PersonDaoImpl extends GenericDaoImpl<Person> implements PersonDao {}
@Repository(“PersonDAO”)
公共类PersonDaoImpl扩展了GenericDaoImpl实现PersonDao{}
服务:
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonDao personDao;
@Transactional
public List<Person> getAll() {
return personDao.getAll();
}
@Transactional
public Person getById(Long id) {
return personDao.getById(id);
}
}
@服务
公共类PersonServiceImpl实现PersonService{
@自动连线
私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人;
@交易的
公共列表getAll(){
return personDao.getAll();
}
@交易的
公众人物getById(长id){
返回personDao.getById(id);
}
}
必须设置PersonDao
的clazz
属性。这可以通过使用@PostConstruct
注释声明一个
@Repository("PersonDAO")
public class PersonDaoImpl extends GenericDaoImpl<Person> implements PersonDao {
@PostConstruct
public void init(){
super.setClazz(Person.class);
}
}
@Repository(“PersonDAO”)
公共类PersonDaoImpl扩展了GenericDaoImpl实现PersonDao{
@施工后
公共void init(){
super.setClazz(Person.class);
}
}
@kxyz很高兴我能帮上忙!