自己的工具类和分页查询

目录

类:

获取UUID:

获取指定长度的UUID:

对集合进行分页:


先上类

import com.ujcmsitems.core.domain.Notice;
import org.junit.Test;

import java.util.*;

@SuppressWarnings("all")
public class YangUtils {

    public static String getUUID32() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * (获取指定长度uuid)
     */
    public static String getUUID(int len) {
        if (len <= 0) {
            return null;
        }
        String uuid = getUUID32();
        //    System.out.println(uuid);
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < len; i++) {
            str.append(uuid.charAt(i));
        }
        return str.toString();
    }

    @Test
    public void test() {
//        System.out.println("密码:"+ com.credit.util.IDutils.getUUID(12));
//        System.out.println("密码:"+ com.credit.util.IDutils.getUUID(16));
//        System.out.println("密码:"+ com.credit.util.IDutils.getUUID(24));
    }

    public static Map queryPages(List userList, int pages, int num) {
        Map map = new HashMap<>();
        if (userList.size() > num) {
            List listIn = new LinkedList<>();//用来存放分页后获取的数据
            int count = userList.size();//集合中数据总数量
            int totalPages;
            int total;
            if (count % num == 0) {
                totalPages = count / num;
            } else {
                total = count / num;
                totalPages = total + 1;
            }
            List list2 = new LinkedList<>();
            list2.add(totalPages);
            map.put("totalPages", list2);
            int thePage = (pages - 1) * num;
            //使用listIn来存放分页查询数据
            for (int i = thePage; i < thePage + num && i < userList.size(); i++) {
                listIn.add(userList.get(i));
            }
            map.put("data", listIn);
            List listCounts = new LinkedList<>();
            listCounts.add(userList.size());
            map.put("totals", listCounts);
            return map;
        } else {
            List integerList = new LinkedList<>();
            integerList.add(userList.size());
            map.put("totals", integerList);
            map.put("data", userList);
            List list2 = new LinkedList<>();
            list2.add(1);
            map.put("totalPages", list2);
            return map;
        }
    }
}

获取UUID

public static String getUUID32() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

获取指定长度的UUID

/**
     * (获取指定长度uuid)
     */
    public static String getUUID(int len) {
        if (len <= 0) {
            return null;
        }
        String uuid = getUUID32();
        //    System.out.println(uuid);
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < len; i++) {
            str.append(uuid.charAt(i));
        }
        return str.toString();
    }

对集合进行分页

public static Map queryPages(List userList, int pages, int num) {
        Map map = new HashMap<>();
        if (userList.size() > num) {
            List listIn = new LinkedList<>();//用来存放分页后获取的数据
            int count = userList.size();//集合中数据总数量
            int totalPages;
            int total;
            if (count % num == 0) {
                totalPages = count / num;
            } else {
                total = count / num;
                totalPages = total + 1;
            }
            List list2 = new LinkedList<>();
            list2.add(totalPages);
            map.put("totalPages", list2);
            int thePage = (pages - 1) * num;
            //使用listIn来存放分页查询数据
            for (int i = thePage; i < thePage + num && i < userList.size(); i++) {
                listIn.add(userList.get(i));
            }
            map.put("data", listIn);
            List listCounts = new LinkedList<>();
            listCounts.add(userList.size());
            map.put("totals", listCounts);
            return map;
        } else {
            List integerList = new LinkedList<>();
            integerList.add(userList.size());
            map.put("totals", integerList);
            map.put("data", userList);
            List list2 = new LinkedList<>();
            list2.add(1);
            map.put("totalPages", list2);
            return map;
        }
    }

Mybaties-plus分页查询:

1.首先创建一个配置类

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置MP的分页插件
 *
 * @author a1002
 */
@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }
}

2.在service层进行测试

    @Override
    public R list(int page, int size, String name) {
        Page pageInfo = new Page<>(page, size);
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, User::getUsername, name);
        queryWrapper.orderByDesc(User::getUpdateTime);
        userService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

就可以获得分页数据了。

 

近日总结:

最近又被封在了宿舍里面,遥遥无期的封寝之路又开启了,其他人全都在卷,而我在干嘛,在宿舍思考人生 ,加上最近加入小组的孩子突然增多,忙着处理一些事务,同时还要紧紧盯着孩子们的进度,月底就会进行考核。

于是乎,就又水了一次博客,嗯。。。最近学的东西的确不多,还有就是快期末了,我早就应该去预习了。对,就是这样。

加油加油学习学习。

 自己的工具类和分页查询_第1张图片

你可能感兴趣的:(笔记,java,开发语言,macos,数据结构)