SpringBoot-注解方式整合Mybatis

Myth丶恋晨 2022-10-06 00:36 341阅读 0赞

SpringBoot整合Mybatis

一.注解方式整合Mybatis

1.创建Air的Mapper接口

  1. @ComponentScan
  2. public interface AirMapper {
  3. List<Air> selectAll();
  4. Air selectOneById(Integer id);
  5. }

2.添加Mybatis注解

针对增删改查:@Insert,@Delete,@Update,@Select

还是需要在启动类中添加@MapperScan注解

  1. @ComponentScan
  2. public interface AirMapper {
  3. @Select("select * from air where id =#{id}")
  4. Air selectOneById(@Param("id") Integer id);
  5. }

3.配置日志

// yml文件
logging:
level:
XXX.mapper: DEBUG

  1. # 连接数据库的信息
  2. spring:
  3. datasource:
  4. driver-class-name: com.mysql.jdbc.Driver
  5. url: jdbc:mysql:///air
  6. username: root
  7. password: 123
  8. type: com.alibaba.druid.pool.DruidDataSource
  9. # mvc:
  10. # # 视图的前缀和后缀
  11. # view:
  12. # prefix: /
  13. # suffix: .html
  14. #禁用缓存
  15. thymeleaf:
  16. cache: false
  17. mybatis:
  18. # 扫描映射文件
  19. mapper-locations: classpath:mapper/*.xml
  20. # 配置别名扫描的包
  21. type-aliases-package: boot.entity
  22. configuration:
  23. # 开启驼峰映射配置
  24. map-underscore-to-camel-case: true
  25. logging:
  26. level:
  27. boot.mapper: DEBUG

4.测试查看日志

  1. @SpringBootTest
  2. class AirMapperTest {
  3. @Autowired
  4. private AirMapper airMapper;
  5. @Test
  6. void selectAll() {
  7. List<Air> airs = airMapper.selectAll();
  8. for (Air air : airs) {
  9. System.out.println(air);
  10. }
  11. }
  12. @Test
  13. void selectOneById() {
  14. Air air = airMapper.selectOneById(1);
  15. System.out.println(air);
  16. }
  17. }

20210610174820648.png

发表评论

表情:
评论列表 (有 0 条评论,341人围观)

还没有评论,来说两句吧...

相关阅读