THEBUGKING 2022-03-08 18:32 采纳率: 100%
浏览 66
已结题

springboot集成mybatis报错

//dao
@Repository
@Mapper
public interface TeacherDao {
    Teacher queryById(Integer id);
}
//mapper
<mapper namespace="com.thebugking.bugstart.dao.TeacherDao">

    <resultMap id="BaseResultMap" type="com.thebugking.bugstart.bean.Teacher">
        <result column="TId" jdbcType="VARCHAR" property="tid" />
        <result column="Tname" jdbcType="VARCHAR" property="tname" />
    </resultMap>

    <select id="queryById" resultMap="BaseResultMap">
        select * from Teacher where TId = #{id}
    </select>

</mapper>
//service
@Service
public interface TeacherService  {
    Teacher queryById(Integer id);
}
@Service
public class TeacherServiceimpl implements TeacherService {
    @Autowired
    TeacherDao teacherDao;

    @Override
    public Teacher queryById(Integer id) {
        Teacher teacher = teacherDao.queryById(id);
        return teacher;
    }
}
//control
@RestController
@RequestMapping("/")
public class TeacherController {
    @Autowired
    TeacherService teacherService;

    @RequestMapping("get/{id}")
    @ResponseBody
    public Teacher GetUser(@PathVariable Integer id){
        return teacherService.queryById(id);
    }

}
//启动类
@SpringBootApplication
//@MapperScan("com.thrbugking.bugstart.dao")
//@ComponentScan(basePackages = {"com.thebugking.bugstart.dao"})    加上这行不会报错,但请求不到接口报404
public class BugstartApplication {

    public static void main(String[] args) {
        SpringApplication.run(BugstartApplication.class, args);
    }

}
//配置
server:
  port: 80

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/Test?useUnicode=true & characterEncoding=utf-8 &useSSL=true & serverTimezone=Asia/Shanghai
    username: root
    password: root
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.thebugking.bugstart.bean

//报错
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-03-08 18:19:44.329 ERROR 22484 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field teacherService in com.thebugking.bugstart.control.TeacherController required a bean of type 'com.thebugking.bugstart.service.TeacherService' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.thebugking.bugstart.service.TeacherService' in your configuration.

img

  • 写回答

1条回答 默认 最新

  • 粒方336 2022-03-08 20:11
    关注

    img


    这个 @Service 去掉试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏

报告相同问题?

问题事件

  • 系统已结题 3月17日
  • 已采纳回答 3月9日
  • 修改了问题 3月8日
  • 创建了问题 3月8日

悬赏问题

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