SpringBoot-注解方式整合Mybatis
Posted 永旗狍子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot-注解方式整合Mybatis相关的知识,希望对你有一定的参考价值。
SpringBoot整合Mybatis
一.注解方式整合Mybatis
1.创建Air的Mapper接口
@ComponentScan
public interface AirMapper
List<Air> selectAll();
Air selectOneById(Integer id);
2.添加Mybatis注解
针对增删改查:@Insert,@Delete,@Update,@Select
还是需要在启动类中添加@MapperScan注解
@ComponentScan
public interface AirMapper
@Select("select * from air where id =#id")
Air selectOneById(@Param("id") Integer id);
3.配置日志
// yml文件
logging:
level:
XXX.mapper: DEBUG
# 连接数据库的信息
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///air
username: root
password: 123
type: com.alibaba.druid.pool.DruidDataSource
# mvc:
# # 视图的前缀和后缀
# view:
# prefix: /
# suffix: .html
#禁用缓存
thymeleaf:
cache: false
mybatis:
# 扫描映射文件
mapper-locations: classpath:mapper/*.xml
# 配置别名扫描的包
type-aliases-package: boot.entity
configuration:
# 开启驼峰映射配置
map-underscore-to-camel-case: true
logging:
level:
boot.mapper: DEBUG
4.测试查看日志
@SpringBootTest
class AirMapperTest
@Autowired
private AirMapper airMapper;
@Test
void selectAll()
List<Air> airs = airMapper.selectAll();
for (Air air : airs)
System.out.println(air);
@Test
void selectOneById()
Air air = airMapper.selectOneById(1);
System.out.println(air);
以上是关于SpringBoot-注解方式整合Mybatis的主要内容,如果未能解决你的问题,请参考以下文章
springboot整合mybatis(映射文件方式和注解方式)