springboot 3 - 加入分页功能、redis配置使用

前言:基于pagehelper的分页、reidis

 

1、分页实现

pom:

	<dependency>
		<groupId>com.github.pagehelper</groupId>
		<artifactId>pagehelper-spring-boot-starter</artifactId>
		<version>1.2.5</version>
		<!-- <exclusions> <exclusion> <groupId>org.mybatis.spring.boot</groupId> 
			<artifactId>mybatis-spring-boot-starter</artifactId> </exclusion> </exclusions> -->
	</dependency>

 

public class PageVO {

    private Integer page =1;
    private Integer rows =10;
    private String type ;
    private String value ;
}
public class CustomerVo extends PageVO{

	private Integer id;
	
	private String zid;
	
	private String name

 

package com.example.yxl.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.yxl.mapper.CustomerMapper;
import com.example.yxl.model.Customer;
import com.example.yxl.model.CustomerVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class CustomerService {

	@Autowired
	private CustomerMapper CustomerMapper; 
	
	public List<Customer> getAll(){
		List<Customer> list = CustomerMapper.selectAll();
		return list ;
	}
	
	public Map getListBypage(CustomerVo vo ) {
		if (vo.getPage() != null && vo.getRows() != null) {
			PageHelper.startPage(vo.getPage(), vo.getRows());
		}
		List<Customer> list = CustomerMapper.selectAll();
		
		PageInfo pageinfo = new PageInfo(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageCount",pageinfo.getTotal());
        map.put("customers",list);
		return map;
	}
}
	@RequestMapping("/getListBypage.jsp")
	public Map getListBypage(@RequestBody CustomerVo vo) {
		return JsonUtil.toJsonSuccess("成功", customerService.getListBypage(vo));
	}

2、redis 配置

application.properties

# REDIS基础配置
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active = 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait = -1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle = 8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle = 0
# 连接超时时间(毫秒)
spring.redis.timeout = 1000
#rspring.session.store-type=redis
#spring.session.redis.namespace=bootsession
spring.redis.host=localhost
spring.redis.port=6379

package com.example.yxl.commom;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfiguration {

    private Logger LOG = LoggerFactory.getLogger(RedisConfiguration.class);
    @Autowired
    LettuceConnectionFactory lettuceConnectionFactory;




    @Bean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        initDomainRedisTemplate(redisTemplate, lettuceConnectionFactory);
        return redisTemplate;
    }


    private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, LettuceConnectionFactory factory) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer(Object.class));
        redisTemplate.setConnectionFactory(factory);
    }

//    @Bean
//    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
//        return redisTemplate.opsForHash();
//    }


//    @Bean
//    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
//        return redisTemplate.opsForValue();
//    }


}

 即可简单实用:

@Autowired
private RedisTemplate redisTemplate;

redisTemplate.opsForValue().set("ks", 1,2,TimeUnit.HOURS);

redisTemplate.opsForValue().get("ks")

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值