关闭

mybatis封装dao servcie时碰到expected single matching bean but found 2:

标签: springmybatisbaseService
107人阅读 评论(0) 收藏 举报
</pre>package com.levelappro.web.core.common.service;import java.util.List;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.transaction.annotation.Transactional;import com.levelappro.web.core.common.mybatis.dao.IMybatisBaseDao;import com.levelappro.web.core.common.mybatis.query.MybatisExample;import com.levelappro.web.core.common.page.PageList;/** * DAO支持类实现 *  * @author ThinkGem * @version 2013-05-15 * @param <T> */@Transactional(readOnly = true)public abstract class IMybatisService<Dao extends IMybatisBaseDao<T>, T> {<span style="white-space:pre">	</span>/**<span style="white-space:pre">	</span> * 持久层对象<span style="white-space:pre">	</span> */<span style="white-space:pre">	</span>@Autowired<span style="white-space:pre">	</span>protected Dao dao;<span style="white-space:pre">	</span>/**<span style="white-space:pre">	</span> * 获取单条数据<span style="white-space:pre">	</span> * <span style="white-space:pre">	</span> * @param id<span style="white-space:pre">	</span> * @return<span style="white-space:pre">	</span> */<span style="white-space:pre">	</span>public T getById(String id) {<span style="white-space:pre">		</span>return dao.getById(id);<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>/**<span style="white-space:pre">	</span> * 查询所有数据列表<span style="white-space:pre">	</span> * <span style="white-space:pre">	</span> * @param entity<span style="white-space:pre">	</span> * @return<span style="white-space:pre">	</span> */<span style="white-space:pre">	</span>public List<T> findAll() {<span style="white-space:pre">		</span>return dao.findAll();<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>/**<span style="white-space:pre">	</span> * 分页查询<span style="white-space:pre">	</span> * <span style="white-space:pre">	</span> * @param entity<span style="white-space:pre">	</span> * @return<span style="white-space:pre">	</span> */<span style="white-space:pre">	</span>public PageList<T> findByPage(MybatisExample example, PageList<T> page) {<span style="white-space:pre">		</span>List<T> list = dao.findByPage(example);<span style="white-space:pre">		</span>page.setResultList(list);<span style="white-space:pre">		</span>page.setTotalCount(dao.count(example));<span style="white-space:pre">		</span>return page;<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>/**<span style="white-space:pre">	</span> * 插入数据<span style="white-space:pre">	</span> * <span style="white-space:pre">	</span> * @param entity<span style="white-space:pre">	</span> * @return<span style="white-space:pre">	</span> */<span style="white-space:pre">	</span>public int insert(T entity) {<span style="white-space:pre">		</span>return dao.insert(entity);<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>/**<span style="white-space:pre">	</span> * 更新数据<span style="white-space:pre">	</span> * <span style="white-space:pre">	</span> * @param entity<span style="white-space:pre">	</span> * @return<span style="white-space:pre">	</span> */<span style="white-space:pre">	</span>public int update(T entity) {<span style="white-space:pre">		</span>return dao.update(entity);<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>/**<span style="white-space:pre">	</span> * 删除数据<span style="white-space:pre">	</span> * <span style="white-space:pre">	</span> * @param id<span style="white-space:pre">	</span> * @see public int delete(T entity)<span style="white-space:pre">	</span> * @return<span style="white-space:pre">	</span> */<span style="white-space:pre">	</span>public int delete(T entity) {<span style="white-space:pre">		</span>return dao.delete(entity);<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>/**<span style="white-space:pre">	</span> * 批量删除<span style="white-space:pre">	</span> * <span style="white-space:pre">	</span> * @param list<span style="white-space:pre">	</span> */<span style="white-space:pre">	</span>public int deleteBatch(final List<T> list) {<span style="white-space:pre">		</span>return dao.deleteBatch(list);<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>/**<span style="white-space:pre">	</span> * 查询总数<span style="white-space:pre">	</span> */<span style="white-space:pre">	</span>public int count(MybatisExample example) {<span style="white-space:pre">		</span>return dao.count(example);<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>/**<span style="white-space:pre">	</span> * 条件查询所有数据列表(不分页)<span style="white-space:pre">	</span> * <span style="white-space:pre">	</span> * @param entity<span style="white-space:pre">	</span> * @return<span style="white-space:pre">	</span> */<span style="white-space:pre">	</span>public List<T> findList(MybatisExample example) {<span style="white-space:pre">		</span>return dao.findList(example);<span style="white-space:pre">	</span>}<p>}</p><p></p><p>一开始这样设计baseservice,结果继承它的时候,启动报这个错,expected single matching bean but found 2:iLinksDao </p><p>iOrigsvrinfoDao注解不能用。。。。<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif, tahoma, 'Microsoft YaHei'; font-size: 14px; line-height: 24px; background-color: rgb(243, 255, 236);">因为你的接口有多个不同的实现类,spring容器不知道要给你注入哪个</span>网上找的加<span style="white-space:pre">	</span>@Qualifier也不管用。</p><p>没办法 最后只能改成这样,不使用注解</p><p><pre name="code" class="java">/**
	 * 持久层对象
	 */
	// @Autowired
	// protected Dao dao;
	public abstract IMybatisBaseDao<T> getDao();

	/**
	 * 获取单条数据
	 * 
	 * @param id
	 * @return
	 */
	public T getById(String id) {
		return this.getDao().getById(id);
	}
。。。继承这个baseService给dao
@Service
@Transactional
public class LinksService extends IMybatisService<Links>{

	@Autowired
	private ILinksDao iLinksDao;

	@Override
	public IMybatisBaseDao<Links> getDao() {
		return iLinksDao;
	}

}
还不知道用@Autowired 的话怎么解决那个问题,在研究下。。。。



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:109次
    • 积分:12
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档