Skip to content

Commit 09b52ca

Browse files
committed
springboot-15-redis(完)
1 parent 504243e commit 09b52ca

26 files changed

+1128
-0
lines changed

springboot-15-redis/doc/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# ***SpringBoot-RedisCache***
2+
3+
**[Redis中文网](http://www.redis.cn/)**
4+
5+
## **1.引入依赖**
6+
```pom
7+
<dependencies>
8+
<!-- Data-Redis -->
9+
<dependency>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-starter-data-redis</artifactId>
12+
</dependency>
13+
14+
<!-- mybatis依赖-->
15+
<dependency>
16+
<groupId>org.mybatis.spring.boot</groupId>
17+
<artifactId>mybatis-spring-boot-starter</artifactId>
18+
<version>1.3.1</version>
19+
</dependency>
20+
21+
<!-- mysql驱动 -->
22+
<dependency>
23+
<groupId>mysql</groupId>
24+
<artifactId>mysql-connector-java</artifactId>
25+
<scope>runtime</scope>
26+
</dependency>
27+
</dependencies>
28+
```
29+
## **2.编写配置文件*
30+
```yml
31+
spring:
32+
datasource:
33+
driver-class-name: com.mysql.cj.jdbc.Driver
34+
url: jdbc:mysql://localhost:3306/springboot-redis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
35+
username: root
36+
password: '123456'
37+
schema:
38+
- classpath:sql/springboot-redis.sql
39+
initialization-mode: always
40+
#redis配置
41+
redis:
42+
host: 192.168.25.145
43+
port: 6379
44+
#mybatis配置
45+
mybatis:
46+
configuration:
47+
map-underscore-to-camel-case: true
48+
type-aliases-package: com.clown.redis.model
49+
mapper-locations: classpath:mybatis/mapper/*.xml
50+
#logging配置
51+
logging:
52+
level:
53+
com.clown.redis.mapper: debug
54+
```
55+
56+
# **Redis数据操作**
57+
* Redis常见5大数据类型
58+
* String(字符串). list(列表). Set(集合). Hash(散列 ). ZSet(有序集合)
59+
* 5大数据操作
60+
* 操作字符串: stringRedisTemplate.opsForValue()
61+
* 操作Hash: stringRedisTemplate.opsForHash()
62+
* 操作list: stringRedisTemplate.opsForList()
63+
* 操作Set: stringRedisTemplate.opsForSet()
64+
* 操作ZSet: stringRedisTemplate.opsForZSet()

springboot-15-redis/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>SpringBootLearn</artifactId>
7+
<groupId>com.clown</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>springboot-15-redis</artifactId>
12+
13+
<dependencies>
14+
<!-- Data-Redis -->
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-data-redis</artifactId>
18+
</dependency>
19+
20+
<!-- mybatis依赖-->
21+
<dependency>
22+
<groupId>org.mybatis.spring.boot</groupId>
23+
<artifactId>mybatis-spring-boot-starter</artifactId>
24+
<version>1.3.1</version>
25+
</dependency>
26+
27+
<!-- mysql驱动 -->
28+
<dependency>
29+
<groupId>mysql</groupId>
30+
<artifactId>mysql-connector-java</artifactId>
31+
<scope>runtime</scope>
32+
</dependency>
33+
</dependencies>
34+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.clown.redis;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
/**
8+
* @author: Richard·Ackerman
9+
* @create: 2019/1/22
10+
**/
11+
@SpringBootApplication
12+
@MapperScan("com.clown.redis.mapper")
13+
public class RedisApplication {
14+
public static void main(String[] args){
15+
SpringApplication.run(RedisApplication.class);
16+
}
17+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.clown.redis.config;
2+
3+
import com.clown.redis.model.Department;
4+
import com.clown.redis.model.Employee;
5+
import com.clown.redis.model.User;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.data.redis.cache.RedisCacheConfiguration;
9+
import org.springframework.data.redis.cache.RedisCacheManager;
10+
import org.springframework.data.redis.cache.RedisCacheWriter;
11+
import org.springframework.data.redis.connection.RedisConnectionFactory;
12+
import org.springframework.data.redis.core.RedisTemplate;
13+
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
14+
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
15+
import org.springframework.data.redis.serializer.RedisSerializationContext;
16+
import org.springframework.data.redis.serializer.RedisSerializer;
17+
18+
import java.net.UnknownHostException;
19+
import java.time.Duration;
20+
21+
/**
22+
* @author: Richard·Ackerman
23+
* @create: 2019/1/22
24+
*
25+
* redis默认使用JDK的序列化器,一般都会将其配置为json序列化器
26+
*
27+
**/
28+
@Configuration
29+
public class MyRedisConfig {
30+
31+
//将数据进行序列化
32+
@Bean
33+
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
34+
RedisTemplate<Object, Object> template = new RedisTemplate<>();
35+
template.setConnectionFactory(redisConnectionFactory);
36+
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
37+
template.setDefaultSerializer(serializer);
38+
return template;
39+
}
40+
41+
//将redis序列化器进行转换,并且该模板的返回值是employee
42+
@Bean
43+
public RedisTemplate<Object, Employee> employeeRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
44+
RedisTemplate<Object, Employee> template = new RedisTemplate<>();
45+
template.setConnectionFactory(redisConnectionFactory);
46+
Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<>(Employee.class);
47+
template.setDefaultSerializer(serializer);
48+
return template;
49+
}
50+
51+
//将redis序列化器进行转换,并且该模板的返回值是department
52+
@Bean
53+
public RedisTemplate<String, Department> departmentRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
54+
RedisTemplate<String, Department> template = new RedisTemplate<>();
55+
template.setConnectionFactory(redisConnectionFactory);
56+
Jackson2JsonRedisSerializer<Department> serializer = new Jackson2JsonRedisSerializer<>(Department.class);
57+
template.setDefaultSerializer(serializer);
58+
return template;
59+
}
60+
61+
//将redis序列化器进行转换,并且该模板的返回值是user
62+
@Bean
63+
public RedisTemplate<Object, User> userRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
64+
RedisTemplate<Object, User> template = new RedisTemplate<>();
65+
template.setConnectionFactory(redisConnectionFactory);
66+
Jackson2JsonRedisSerializer<User> serializer = new Jackson2JsonRedisSerializer<>(User.class);
67+
template.setDefaultSerializer(serializer);
68+
return template;
69+
}
70+
71+
//定制缓存规则
72+
@Bean
73+
public RedisCacheManager userRedisCacheManager(RedisConnectionFactory connectionFactory){
74+
//创建一个redisCacheWriter
75+
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
76+
RedisSerializer<Object> jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
77+
RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jsonRedisSerializer);
78+
RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);
79+
//设置默认过期时间是30秒
80+
defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
81+
//初始化RedisCacheManager
82+
return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
83+
}
84+
85+
86+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.clown.redis.controller;
2+
3+
import com.clown.redis.model.Department;
4+
import com.clown.redis.service.DepartmentService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.List;
9+
10+
/**
11+
* @author: Richard·Ackerman
12+
* @create: 2019/1/20
13+
**/
14+
@RestController
15+
@RequestMapping("/department")
16+
public class DepartmentController {
17+
18+
@Autowired
19+
private DepartmentService departmentService;
20+
21+
@GetMapping
22+
public List<Department> findAll(){
23+
return departmentService.findAll();
24+
}
25+
26+
@GetMapping("/{id}")
27+
public Department findById(@PathVariable("id") Integer id){
28+
return departmentService.findById(id);
29+
}
30+
31+
@GetMapping("/findByDepartmentName/{departmentName}")
32+
public Department findByDepartmentName(@PathVariable("departmentName") String departmentName){
33+
return departmentService.findByDepartmentName(departmentName);
34+
}
35+
36+
@PostMapping
37+
public String add(@RequestBody Department department){
38+
return departmentService.add(department);
39+
}
40+
41+
@PutMapping
42+
public String update(@RequestBody Department department){
43+
return departmentService.update(department);
44+
}
45+
46+
@DeleteMapping("/{id}")
47+
public String deleteById(@PathVariable("id") Integer id){
48+
return departmentService.deleteById(id);
49+
}
50+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.clown.redis.controller;
2+
3+
import com.clown.redis.model.Employee;
4+
import com.clown.redis.service.EmployeeService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.List;
9+
10+
/**
11+
* @author: Richard·Ackerman
12+
* @create: 2019/1/20
13+
**/
14+
@RestController
15+
@RequestMapping("/employee")
16+
public class EmployeeController {
17+
18+
@Autowired
19+
private EmployeeService employeeService;
20+
21+
@GetMapping
22+
public List<Employee> findAll(){
23+
return employeeService.findAll();
24+
}
25+
26+
@GetMapping("/{id}")
27+
public Employee findById(@PathVariable("id") Integer id){
28+
return employeeService.findById(id);
29+
}
30+
31+
@GetMapping("/findByDId/{dId}")
32+
public List<Employee> findByDId(@PathVariable("dId") Integer dId){
33+
return employeeService.findByDId(dId);
34+
}
35+
36+
@GetMapping("/findByLastName")
37+
public Employee findByLastName(@RequestParam("lastName") String lastName){
38+
return employeeService.findByLastName(lastName);
39+
}
40+
41+
@PostMapping
42+
public String add(@RequestBody Employee employee){
43+
return employeeService.add(employee);
44+
}
45+
46+
@PutMapping
47+
public String update(@RequestBody Employee employee){
48+
Employee update = employeeService.update(employee);
49+
if (null==update) {
50+
return "更新失败";
51+
}
52+
return "更新成功";
53+
}
54+
55+
@DeleteMapping("/{id}")
56+
public String deleteById(@PathVariable("id") Integer id){
57+
return employeeService.deleteById(id);
58+
}
59+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.clown.redis.controller;
2+
3+
import com.clown.redis.model.User;
4+
import com.clown.redis.service.UserService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.List;
9+
10+
/**
11+
* @author: Richard·Ackerman
12+
* @create: 2019/1/22
13+
**/
14+
@RestController
15+
@RequestMapping("/user")
16+
public class UserController {
17+
18+
@Autowired
19+
private UserService userService;
20+
21+
@GetMapping
22+
public List<User> findAll(){
23+
return userService.findAll();
24+
}
25+
26+
@GetMapping("/{id}")
27+
public User findById(@PathVariable("id") Integer id){
28+
return userService.findById(id);
29+
}
30+
31+
@PostMapping
32+
public String add(@RequestBody User user){
33+
return userService.add(user);
34+
}
35+
36+
@PutMapping
37+
public String update(@RequestBody User user){
38+
return userService.update(user);
39+
}
40+
41+
@DeleteMapping("/{id}")
42+
public String deleteById(@PathVariable("id") Integer id){
43+
return userService.deleteById(id);
44+
}
45+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.clown.redis.mapper;
2+
3+
import com.clown.redis.model.Department;
4+
import org.apache.ibatis.annotations.*;
5+
6+
import java.util.List;
7+
8+
//@Mapper
9+
public interface DepartmentMapper {
10+
11+
@Select("SELECT * FROM department")
12+
List<Department> findAll();
13+
14+
@Select("SELECT * FROM department WHERE id = #{id}")
15+
Department findById(Integer id);
16+
17+
@Select("SELECT * FROM department WHERE department_name = #{departmentName}")
18+
Department findByDepartmentName(String departmentName);
19+
20+
@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
21+
@Insert("INSERT INTO department(department_name) VALUES(#{departmentName})")
22+
int add(Department department);
23+
24+
@Update("UPDATE department SET department_name = #{departmentName} WHERE id = #{id}")
25+
int update(Department department);
26+
27+
@Delete("DELETE FROM department WHERE id = #{id}")
28+
int deleteById(Integer id);
29+
30+
31+
}

0 commit comments

Comments
 (0)