首页 > 编程语言 > 详细

注解方式整合mybatis & SpringBoot整合分页助手

时间:2021-05-10 22:53:11      阅读:25      评论:0      收藏:0      [点我收藏+]

1.注解方式整合mybatis

1.1 创建District的Mapper接口

public interface DistrictMapper {

    @Select("select * from district")
    List<District> findAll();

    @Select("select * from district where id= #{id}")
    District findOneById(@Param("id") Integer id);
}

1.2 添加mybatis注解

针对增删改查:@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);
}

1.3 测试,看到执行的sql语句

 在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模式

技术分享图片

2. SpringBoot整合分页助手

导入依赖

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!