为什么我们总是先声明interface
来实现我们的DAO类
界面:
public interface IRegion<T extends Serializable> {
List<T> findAll();
}
DAO班级:
@Transactional
@Repository
public class RegionDAO implements IRegion < Region > {
@Autowired
private SessionFactory sessionFactory;
@Override
public List<Region> findAll() {
return sessionFactory.getCurrentSession().createQuery("from Region").list();
}
}
控制器:
@Controller
public class welcome {
@Autowired
private IRegion<Region> regionI;
....
}
但是当我保留我的DAO类并删除界面
时它也有效@Repository
@Transactional
public class RegionDAO {
@Autowired
private SessionFactory sessionFactory;
public List<Region> findAll() {
return sessionFactory.getCurrentSession().createQuery("from Region").list();
}
}
控制器:
@Controller
public class welcome {
@Autowired
private RegionDAO regionDAO;
....
}
那么我们为什么要添加接口?