@Resource("userDao")出现 属性值是未定义类型 错误提示

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.kfz.surveypark.dao.BaseDao;
import com.kfz.surveypark.model.User;
import com.kfz.surveypark.service.UserService;

@Service("userService")
public class UserServiceImpl extends BaseServiceImplimplements UserService{

@Resource(value="userDao")
public void setDao(BaseDao<User> dao) {
    super.setDao(dao);
}

}
以下是 BaseServiceImpl,BaseDaoImpl
import java.util.List;
import javax.annotation.Resource;
import com.kfz.surveypark.dao.BaseDao;
import com.kfz.surveypark.service.BaseService;

public abstract class BaseServiceImpl implements BaseService {

private BaseDao<T> dao;

//注入dao
@Resource
public void setDao(BaseDao dao) {
this.dao = dao;
}

@Override
public void saveEntity(T t) {
    dao.saveEntity(t);

}


@Override
public void deleteEntity(T t) {
    dao.deleteEntity(t);;

}
@Override
public void saveOrUpdateEntity(T t) {
    dao.saveOrUpdateEntity(t);

}

@Override
public void updateEntity(T t) {
    dao.updateEntity(t);

}

@Override
public void batchEntityByHQL(String hql, Object... objects) {
    dao.batchEntityByHQL(hql, objects);

}

@Override
public T loadEntity(Integer id) {

    return dao.loadEntity(id);
}

@Override
public T getEntity(Integer id) {

    return dao.getEntity(id);
}

@Override
public List<T> findEntityByHQL(String hql, Object... objects) {

    return dao.findEntityByHQL(hql, objects);
}
}

BaseDaoImpl类
import java.lang.reflect.ParameterizedType;

import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.kfz.surveypark.dao.BaseDao;
/*

  • 抽象的实现, / @SuppressWarnings("unchecked") public abstract class BaseDaoImpl implements BaseDao{ //注入 SessionFactory @Resource private SessionFactory sf; /*
  • 利用反射得到 T 实例对象 如,User, Survey
  • */
    private Class clazz;

    public BaseDaoImpl(){
    ParameterizedType pt=(ParameterizedType) this.getClass().getGenericSuperclass();
    clazz=(Class)pt.getActualTypeArguments()[0];
    }

    @Override
    public void saveEntity(T t) {
    sf.getCurrentSession();
    }

    @Override
    public void deleteEntity(T t) {
    sf.getCurrentSession();

    }
    @Override
    public void saveOrUpdateEntity(T t) {
    sf.getCurrentSession();
    }

    @Override
    public void updateEntity(T t) {
    sf.getCurrentSession();

    }

    @Override
    public void batchEntityByHQL(String hql, Object... objects) {
    Query q=sf.getCurrentSession().createQuery(hql);
    for(int i=0;i<objects.length;i++){
    q.setParameter(i, objects[i]);
    }
    q.executeUpdate();
    }

    @Override
    public T loadEntity(Integer id) {
    return (T) sf.getCurrentSession().load(clazz, id);
    }

    @Override
    public T getEntity(Integer id) {
    return (T)sf.getCurrentSession().get(clazz,id);
    }

    @Override
    public List findEntityByHQL(String hql, Object... objects) {
    Query q=sf.getCurrentSession().createQuery(hql);
    for(int i=0;i<objects.length;i++){
    q.setParameter(i, objects[i]);
    }
    return q.list();
    }

1个回答

把@Resource(value="userDao")改为@Resource(name="userDao"),
1.另外说明 @Resource若要指定值必须是 @Resuouce(name="xxx")的形式
2.@Resource 什么都不写,取值为 属性名,setxxx()方法 的名字xxx,首字母小写

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问