学习使用springboot整合mybatis碰到的问题

上面是启动时报错日志   提示:controller层注入service失败

controller层代码



service接口



service接口实现类



Application 启动类


项目结构目录



菜鸟一个 求大神们帮忙看看问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
整合MyBatisSpringBoot可以让我们更加方便地使用MyBatis进行数据库操作,下面是使用Java SpringBoot整合MyBatis的步骤: 1.在pom.xml文件中添加MyBatisMyBatis-Spring的依赖项: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 2.创建一个application.properties文件来配置数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3.创建一个Mapper接口,用于定义SQL语句的执行方法: ```java @Mapper public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") User getUserById(Integer id); } ``` 4.创建一个Service层,调用Mapper中的方法: ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(Integer id) { return userMapper.getUserById(id); } } ``` 5.在SpringBoot的启动类中添加@MapperScan注解,用于扫描Mapper接口: ```java @SpringBootApplication @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 6.使用@Service注解将Service层添加到Spring容器中: ```java @Service public class UserService { // ... } ``` 现在,你已经成功地使用Java SpringBoot整合MyBatis。在Controller层中,你可以使用@Autowired注解将Service层注入到Controller中,然后调用Service层中的方法来完成数据库操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值