SpringBoot集成mybatis-plus, 分页插件的使用


pom.xml坐标

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/>
 </parent>
 <dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

     <!-- mybatis-plus begin -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>${mybatisplus-spring-boot-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>${mybatisplus.version}</version>
        </dependency>
        <!-- mybatis-plus end -->
    </dependencies>

yml配置文件集成Mybatis-puls

mybatis-plus:
  #mapper-locations: classpath:/mapper/*Mapper.xml
  #实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: #填写实体类路径
  global-config:
    id-type: 1  #0:数据库ID自增   1:用户输入id
    db-column-underline: false
    refresh-mapper: true
    configuration:
      map-underscore-to-camel-case: true
      cache-enabled: true #配置的缓存的全局开关
      lazyLoadingEnabled: true #延时加载的开关
      multipleResultSetsEnabled: true #开启延时加载,否则按需加载属性
      log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句,调试用

启动类注解

@SpringBootApplication
@MapperScan("dao的包路径")   //配置mapper包扫描
@EnableEurekaClient
@EnableFeignClients
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

创建config包和MyBatisPlusConfig类

@Configuration
public class MyBatisPlusConfig {
    //分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

controller层,模拟对用户进行分页查询

    @RequestMapping(value = "search/{page}/{size}", method = RequestMethod.POST)
    public Result findBypage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String, Object> map) {
        //   @PathVariable Integer page, @PathVariable Integer size 是条件
        //   @RequestBody Map<String, Object> map  本应该使用pojo来进行接收遍历,但是需要使用反射的方式,成本高,效率低,
        //   而是用map 直接遍历
        //根据条件分页查询
        Page<User> userPage = userService.findByPage(map, page, size);
        //封装分页返回对象
        PageResult<Article> pageResult = new PageResult<>(
                userPage.getTotal(), userPage.getRecords()
        );
        //返回数据
        return new Result("查询成功", pageResult);
    }

1 service层 ,   对map集合进行处理

public Page<Article> findByPage(Map<String, Object> map, Integer page, Integer size) {
        //设置查询条件
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        Set<String> keySet = map.keySet();
        //在多条件查询中 ,如果字段为空,就不加入条件查询中,如果有值那就加入条件查询中
        for (String kye : keySet) {
        /* if (map.get(kye) != null) {
                wrapper.eq(kye, map.get(kye));
            }*/
            //实现了动态sql
            wrapper.eq(map.get(kye) != null, kye, map.get(kye));
        }
        //设置分页参数
        Page<User> userPage = new Page<>(page, size);
        //执行查询
        //分页参数, 查询条件
        List<User> users = userDao.selectPage(userPage, wrapper);
        userPage.setRecords(users);
        //返回
        return userPage;
    }

2.Service层 ,因为controller接收的是对象,所以不需要像处理map集合一样处理

 //分页查询
    public Page<User> selectByPage(User user, Integer page, Integer size) {
        //设置分页条件
        Page<User> pageData = new Page<>(page, size);
        //执行分页查询
        List<User> users = userDao.selectPage(pageData, new EntityWrapper<>(user));
//设置结果集到分页对象中
        pageData.setRecords(users);
        //返回结果
        return pageData;
    }

dao层直接集成BaseMapper即可

public interface UserDao extends BaseMapper<User> {
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM