-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fc3742c
Showing
24 changed files
with
1,452 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
HELP.md | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
|
||
logs | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.zrclass</groupId> | ||
<artifactId>mybatis-redis-cache</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.2.4.RELEASE</version> | ||
<relativePath/> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>2.0.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-redis</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.46</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid</artifactId> | ||
<version>1.1.10</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
<version>1.2.47</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
<repositories> | ||
<repository> | ||
<id>maven-ali</id> | ||
<url>http://maven.aliyun.com/nexus/content/groups/public//</url> | ||
<releases> | ||
<enabled>true</enabled> | ||
</releases> | ||
<snapshots> | ||
<enabled>true</enabled> | ||
<updatePolicy>always</updatePolicy> | ||
<checksumPolicy>fail</checksumPolicy> | ||
</snapshots> | ||
</repository> | ||
</repositories> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.zrclass.cache; | ||
|
||
import org.mybatis.spring.annotation.MapperScan; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* @author 20114535 zhourui | ||
* @version 1.0 | ||
* @date 2021/3/16 11:16 | ||
*/ | ||
@SpringBootApplication | ||
@MapperScan("org.zrclass.cache.dao") | ||
public class Application { | ||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/org/zrclass/cache/common/config/BaseRedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.zrclass.cache.common.config; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||
import org.springframework.data.redis.cache.RedisCacheManager; | ||
import org.springframework.data.redis.cache.RedisCacheWriter; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
import org.springframework.data.redis.serializer.RedisSerializer; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
import org.zrclass.cache.common.redis.RedisService; | ||
import org.zrclass.cache.common.redis.impl.RedisServiceImpl; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* Redis基础配置 | ||
* | ||
* @author 20114535 | ||
*/ | ||
@Configuration | ||
@EnableCaching | ||
@Slf4j | ||
public class BaseRedisConfig { | ||
|
||
|
||
@Bean | ||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { | ||
RedisSerializer<Object> serializer = redisSerializer(); | ||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(serializer); | ||
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setHashValueSerializer(serializer); | ||
redisTemplate.afterPropertiesSet(); | ||
return redisTemplate; | ||
} | ||
|
||
@Bean | ||
public RedisSerializer<Object> redisSerializer() { | ||
//创建JSON序列化器 | ||
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | ||
//必须设置,否则无法将JSON转化为对象,会转化成Map类型 | ||
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL); | ||
serializer.setObjectMapper(objectMapper); | ||
return serializer; | ||
} | ||
|
||
@Bean | ||
public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) { | ||
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory); | ||
//设置Redis缓存有效期为1天 | ||
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() | ||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer())).entryTtl(Duration.ofDays(1)); | ||
return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration); | ||
} | ||
|
||
|
||
@Bean | ||
public RedisService redisService() { | ||
return new RedisServiceImpl(); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/org/zrclass/cache/common/config/MybatisRedisCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.zrclass.cache.common.config; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.ibatis.cache.Cache; | ||
import org.zrclass.cache.common.redis.RedisService; | ||
import org.zrclass.cache.common.utils.Md5Util; | ||
import org.zrclass.cache.common.utils.SpringUtil; | ||
|
||
import java.util.concurrent.locks.ReadWriteLock; | ||
|
||
/** | ||
* @author 20114535 zhourui | ||
* @version 1.0 | ||
* @date 2021/3/16 12:40 | ||
*/ | ||
@Slf4j | ||
public class MybatisRedisCache implements Cache { | ||
public final String id; | ||
|
||
public MybatisRedisCache(String id) { | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return this.id; | ||
} | ||
|
||
@Override | ||
public void putObject(Object key, Object value) { | ||
RedisService redisService = (RedisService) SpringUtil.getBean("redisService"); | ||
redisService.hSet(id, Md5Util.getMD5Code(key.toString()), value); | ||
} | ||
|
||
@Override | ||
public Object getObject(Object key) { | ||
RedisService redisService = (RedisService) SpringUtil.getBean("redisService"); | ||
return redisService.hGet(id, Md5Util.getMD5Code(key.toString())); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
RedisService redisService = (RedisService) SpringUtil.getBean("redisService"); | ||
redisService.del(id); | ||
} | ||
|
||
@Override | ||
public Object removeObject(Object key) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
RedisService redisService = (RedisService) SpringUtil.getBean("redisService"); | ||
return redisService.hSize(id).intValue(); | ||
} | ||
|
||
@Override | ||
public ReadWriteLock getReadWriteLock() { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.zrclass.cache.common.entity; | ||
|
||
import lombok.Data; | ||
|
||
import java.io.Serializable; | ||
import java.util.Date; | ||
|
||
/** | ||
* @author 20114535 zhourui | ||
* @version 1.0 | ||
* @date 2021/3/16 11:47 | ||
*/ | ||
@Data | ||
public class Emp implements Serializable { | ||
private Long id; | ||
private Long userId; | ||
private String empName; | ||
private Date birthday; | ||
private String password; | ||
private Integer gender; | ||
|
||
@Override | ||
public String toString() { | ||
return "Emp{" + | ||
"id=" + id + | ||
", userId=" + userId + | ||
", empName='" + empName + '\'' + | ||
", birthday=" + birthday + | ||
", password='" + password + '\'' + | ||
", gender=" + gender + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.zrclass.cache.common.entity; | ||
|
||
import lombok.Data; | ||
|
||
import java.io.Serializable; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* @author 20114535 zhourui | ||
* @version 1.0 | ||
* @date 2021/3/16 11:45 | ||
*/ | ||
@Data | ||
public class User implements Serializable { | ||
private Long id; | ||
private String username; | ||
private String password; | ||
private Date birthday; | ||
private Integer gender; | ||
private List<Emp> empList; | ||
|
||
@Override | ||
public String toString() { | ||
return "User{" + | ||
"id=" + id + | ||
", username='" + username + '\'' + | ||
", password='" + password + '\'' + | ||
", birthday=" + birthday + | ||
", gender=" + gender + | ||
", empList=" + empList + | ||
'}'; | ||
} | ||
} |
Oops, something went wrong.