no_russion 2020-02-11 22:32 采纳率: 33.3%
浏览 1089

spring整合使用mybatis的接口,但使用时显示在spring容器内找不到dao的接口对象。

本来已经整合了spring和springmvc,然后我再添加了mybatis的依赖,数据库的配置等等。在test类里mybatis可以单独运作。但是和spring整合的时候就出现问题.

这是我的目录结构
图片说明

我的dao中的类如下

@Repository
public interface IAccountDao
{
    @Select("select * from account")
    List<Account> findAll();

    @Insert("insert into account(name,money) values (#{name},#{money})")
    void saveAccount(Account account);
}

service中的类如下

@Service("accountService")
public class AccountServiceImpl implements IAccountService
{
    @Autowired
    private IAccountDao accountDao;

    @Override
    public List<Account> findAll()
    {
        System.out.println("findAll_service");
        return accountDao.findAll();
    }

    @Override
    public void saveAccount(Account account)
    {
        System.out.println("saveAccount_service");
        accountDao.saveAccount(account);
    }
}

而且我的spring的配置文件已经按照教程中那样引入了mybatis的工厂什么的

<?xml version="1.0" encoding="UTF-8"?>
<beans 一堆约束省略了>


    <context:component-scan base-package="com.HSY">

        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--    整合Mybatis-->
    <!-- 配置C3P0的连接池对象 -->
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/ssm" />
        <property name="username" value="root" />
        <property name="password" value="123456" />

    </bean>
    <!-- 配置SqlSession的工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>
<!--     配置扫描dao的包-->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.HSY.dao"/>
    </bean>

</beans>

SqlMapConfig的数据库配置也配了,但是最后仍然报错

beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.HSY.dao.IAccountDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

请问大神们怎么解决

  • 写回答

2条回答 默认 最新

  • yanweijie0317 2020-02-12 10:25
    关注

    试试把IAccountDao类上的注解去掉。

    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题