public interface DistrictMapper {
@Select("select * from district")
List<District> findAll();
@Select("select * from district where id= #{id}")
District findOneById(@Param("id") Integer id);
}
针对增删改查:@Insert,@Delete,@Update,@Select
还是需要在启动类中添加@MapperScan注解
public interface DistrictMapper {
@Select("select * from district")
List<District> findAll();
@Select("select * from district where id= #{id}")
District findOneById(@Param("id") Integer id);
}
在application.yml文件中添加
# 查看sql语句
logging:
level:
com.company.firstspringboot: DEBUG
class DistrictMapperTest extends FirstSpringbootApplicationTests {
@Autowired
private DistrictMapper mapper;
@Test
void findAll() {
List<District> list = mapper.findAll();
for (District district : list) {
System.out.println(district);
}
}
@Test
void findOneById() {
District oneById = mapper.findOneById(5);
System.out.println(oneById);
}
}
可以看到执行的sql语句,其实就是debug模式

导入依赖
https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter
<!-- 分页助手pageHelper依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
AirMapperTest.java
import com.company.firstspringboot.FirstSpringbootApplicationTests;
import com.company.firstspringboot.entity.Air;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
class AirMapperTest extends FirstSpringbootApplicationTests {
@Autowired
private AirMapper airMapper;
@Test
void findAll() {
List<Air> all = airMapper.findAll();
for (Air air : all) {
System.out.println(air);
}
}
@Test
public void findByPage() {
// 1.执行分页
PageHelper.startPage(1, 5);
// 2.执行查询
List<Air> list = airMapper.findAll();
// 3.封装PageInfo对象
PageInfo<Air> pageInfo = new PageInfo<>(list);
// 4.输出
for (Air air : pageInfo.getList()) {
System.out.println(air);
}
}
}

注解方式整合mybatis & SpringBoot整合分页助手
原文:https://www.cnblogs.com/GumpYan/p/14752616.html