weixin_48050721 2021-02-09 22:56 采纳率: 50%
浏览 177
已结题

springboot在用@Mapper导致注入出错

启动文件

@MapperScan("com.mapper")
@SpringBootApplication
public class Ch1105SpringbootApplication {

    public static void main(String[] args) {

        SpringApplication.run(Ch1105SpringbootApplication.class, args);
    }

}

com.domain.Account

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Account {
    private Integer id;
    private String username;
    private String balance;

}

com.mapper.AccountMapper

@Mapper
public interface AccountMapper {

    @Select("select * from account")
    public Account getById(Integer id);
}

com.servlet.AccountService

@Service
public class AccountService {

    @Autowired
    private AccountMapper accountMapper;

    public Account getId(Integer id){
        return accountMapper.getById(id);
    }

}

resources:application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=0213
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

测结果报错:

。。

主要错误:No qualifying bean of type 'com.servlet.AccountService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

  • 写回答

10条回答 默认 最新

  • 编号灬9527 2021-02-09 23:40
    关注

    错误信息是bean找不到,十有八九是启动类不是在最外面

    评论

报告相同问题?