启动文件
@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)}