一、pom.xml配置
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
二、Redis配置类
@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间600秒
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(60000))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
三、 application.properties配置
spring.redis.host=192.168.44.64
spring.redis.password=123456
spring.redis.database=5
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/aaa?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.serialization.write-date-keys-as-timestamps=false
spring.main.allow-circular-references=true
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.mapper-locations=classpath:/mapper/*.xml
mybatis-plus.global-config.db-config.logic-not-delete-value=0
mybatis-plus.global-config.db-config.logic-delete-value=1
四、entity层
package com.aaa.entity;
import lombok.Data;
@Data
public class Student {
private Integer sid;
private String sname;
private Grade grade;
}
package com.aaa.entity;
import lombok.Data;
import java.util.List;
@Data
public class Grade {
private Integer gid;
private String gname;
private List<Student> student;
}
五、Mapper层
package com.aaa.mapper;
import com.aaa.entity.Dept;
import com.aaa.entity.Grade;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface DeptMapper {
public List<Dept> gets() ;
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aaa.mapper.GradeMapper">
<resultMap id="GradeResultMap" type="com.aaa.entity.Grade">
<id property="gid" column="gid" />
<result property="gname" column="gname" />
<collection property="student" ofType="com.aaa.entity.Student" autoMapping="true">
<id property="sid" column="sid" />
<result property="sname" column="sname" />
</collection>
</resultMap>
<select id="getGrades" resultMap="GradeResultMap">
SELECT g.gid, g.gname, s.sid, s.sname
FROM grade g
LEFT JOIN student s ON g.gid = s.gid
</select>
</mapper>
六、service层
package com.aaa.serivce;
import com.aaa.entity.Grade;
import javax.annotation.Resource;
import java.util.List;
public interface GradeService {
public List<Grade> getGrades();
List<Grade> getsByPage(int page, int size);
}
package com.aaa.serivce.impl;
import com.aaa.entity.Grade;
import com.aaa.mapper.DeptMapper;
import com.aaa.mapper.GradeMapper;
import com.aaa.serivce.GradeService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class GradeServiceImpl implements GradeService {
@Resource
private GradeMapper gradeMapper;
@Override
// 使用@Cacheable注解,将结果缓存,key为'student'
@Cacheable(value = "mygrade",key = "'grade'")
public List<Grade> getGrades() {
return gradeMapper.getGrades();
}
@Override
public List<Grade> getsByPage(int page, int size) {
List<Grade> grades = getGrades();
List<Grade> gradelist = grades.stream()
.skip((page - 1) * size) // 跳过前一页的数据
.limit(size) // 限制获取的数据数量
.collect(Collectors.toList());
return null;
}
}
七、controller层
package com.aaa.controller;
import com.aaa.entity.Grade;
import com.aaa.serivce.GradeService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/grade")
public class GradeController {
@Resource
private GradeService gradeService;
@GetMapping("/gets")
public List<Grade> gets(){
List<Grade> list = gradeService.getGrades();
return list;
}
@GetMapping("/getsByPage")
public List<Grade> getsByPage(@RequestParam("page") int page , @RequestParam("size") int size){
List<Grade> grades = gradeService.getsByPage(page,size);
return grades;
}
}