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

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 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号