关于注解问题
package com.imooc.sm.dao;
import com.imooc.sm.entity.Department;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("departmentDao")
public interface DepartmentDao {
void insert(Department department);
void delete(Integer id);
void update(Department department);
Department selectById(Integer id);
List<Department> selectAll();
}
package com.imooc.sm.service.Impl;
import com.imooc.sm.dao.DepartmentDao;
import com.imooc.sm.entity.Department;
import com.imooc.sm.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {
/*@Autowired是按类型匹配,如果有多个DepartmentDao类的对象就会报错,
可以配合@Qualifier("departmentDao")使用, 指定对象名,与@Resource效果一样,
也可以直接使用@Resource*/
@Qualifier("departmentDao")
private DepartmentDao departmentDao;
public void add(Department department) {
departmentDao.insert(department);
}
public void remove(Integer id) {
departmentDao.delete(id);
}
public void edit(Department department) {
departmentDao.update(department);
}
public Department get(Integer id) {
return departmentDao.selectById(id);
}
public List<Department> getAll() {
return departmentDao.selectAll();
}
}
这两个注解可以这样子用吗?
正在回答
同学你好!
1.同学指的是这里描述的吗?
2.如果是的话,同学的思路是正确的,但是使用 @Qualifier时,@Autowired也要写上呢
@Autowired @Qualifier("personDaoxxx")
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星