From 9492bd4ba7a6cb5a8a8021bd4e82c0a3a2982077 Mon Sep 17 00:00:00 2001 From: "haiji.yang" Date: Tue, 12 May 2020 14:47:48 +0800 Subject: [PATCH 1/2] add mybatis-redis-cache --- doc/file/db1.yaml | 4 - doc/file/mybatis.yaml | 9 ++ .../mapper/sys/SysLogisticsMapper.xml | 4 +- .../javayh-mybatis-starter/pom.xml | 6 +- .../com/javayh/mybatis/cache/RedisCache.java | 130 ++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 +- .../java/com/javayh/redis/util/RedisUtil.java | 32 ++++- 7 files changed, 180 insertions(+), 8 deletions(-) create mode 100644 doc/file/mybatis.yaml create mode 100644 javayh-dependencies/javayh-mybatis-starter/src/main/java/com/javayh/mybatis/cache/RedisCache.java diff --git a/doc/file/db1.yaml b/doc/file/db1.yaml index b91ef71..f6a8c0e 100644 --- a/doc/file/db1.yaml +++ b/doc/file/db1.yaml @@ -31,7 +31,3 @@ spring: reset-enable: false login-username: admin login-password: admin123 - -mybatis: - ### xml存放路径 - mapper-locations: classpath*:mapper/*/*Mapper.xml \ No newline at end of file diff --git a/doc/file/mybatis.yaml b/doc/file/mybatis.yaml new file mode 100644 index 0000000..349404a --- /dev/null +++ b/doc/file/mybatis.yaml @@ -0,0 +1,9 @@ +## mybatis 常用配置 +mybatis: + ### xml存放路径 + mapper-locations: classpath*:mapper/*/*Mapper.xml + configuration: + cache-enabled: true + lazy-loading-enabled: false + aggressive-lazy-loading: true + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl diff --git a/javayh-demo/javayh-demo-api/src/main/resources/mapper/sys/SysLogisticsMapper.xml b/javayh-demo/javayh-demo-api/src/main/resources/mapper/sys/SysLogisticsMapper.xml index 9c9d6bf..828cf25 100644 --- a/javayh-demo/javayh-demo-api/src/main/resources/mapper/sys/SysLogisticsMapper.xml +++ b/javayh-demo/javayh-demo-api/src/main/resources/mapper/sys/SysLogisticsMapper.xml @@ -2,7 +2,9 @@ - + + + sys_logistics.id as id, sys_logistics.addressee_name as addresseeName, diff --git a/javayh-dependencies/javayh-mybatis-starter/pom.xml b/javayh-dependencies/javayh-mybatis-starter/pom.xml index bdbc1c1..67ca0eb 100644 --- a/javayh-dependencies/javayh-mybatis-starter/pom.xml +++ b/javayh-dependencies/javayh-mybatis-starter/pom.xml @@ -51,6 +51,10 @@ javayh-common-starter true - + + com.javayh + javayh-redis-starter + true + \ No newline at end of file diff --git a/javayh-dependencies/javayh-mybatis-starter/src/main/java/com/javayh/mybatis/cache/RedisCache.java b/javayh-dependencies/javayh-mybatis-starter/src/main/java/com/javayh/mybatis/cache/RedisCache.java new file mode 100644 index 0000000..1694966 --- /dev/null +++ b/javayh-dependencies/javayh-mybatis-starter/src/main/java/com/javayh/mybatis/cache/RedisCache.java @@ -0,0 +1,130 @@ +package com.javayh.mybatis.cache; + +import com.javayh.common.selector.SpringSelector; +import com.javayh.common.util.log.Log; +import com.javayh.common.util.spring.SpringUtils; +import com.javayh.redis.util.RedisUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.cache.Cache; +import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionFactoryBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.util.CollectionUtils; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import javax.sql.DataSource; +import java.util.Set; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + *

+ * Custom cache + *

+ * + * @author Dylan-haiji + * @version 1.0.0 + * @since 2020-05-11 + */ +@Slf4j +@Configuration +public class RedisCache implements Cache { + + private String id; + + /** 读写锁*/ + private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true); + + public RedisCache() { + } + + public RedisCache(final String id) { + if (id == null) { + throw new IllegalArgumentException("Cache instances require an ID"); + } + this.id = id; + } + @Autowired + private RedisUtil redisUtil; + private static RedisCache redisCache ; + //通过@PostConstruct实现初始化bean之前进行的操作 + @PostConstruct + public void init() { + redisCache = this; + redisCache.redisUtil = this.redisUtil; + } + @Override + public String getId() { + return this.id; + } + + @Override + public void putObject(Object key, Object value) { + if (value != null) { + //向Redis中添加数据,有效时间是12小时 + redisCache.redisUtil.setObj(key.toString(),value,43200); + log.debug(value.toString()); + } + } + + @Override + public Object getObject(Object key) { + try { + if (key != null && readWriteLock.readLock().tryLock()) { + return redisCache.redisUtil.get(key.toString()); + } + } catch (Exception e) { + Log.error("Mybatis Get Cache",e.getStackTrace()); + }finally { + log.debug(key.toString()); + readWriteLock.readLock().unlock(); + } + return null; + } + + @Override + public Object removeObject(Object key) { + try { + if (key != null && readWriteLock.writeLock().tryLock()) { + redisCache.redisUtil.del(key.toString()); + } + } catch (Exception e) { + Log.error("Mybatis Del Cache",e.getStackTrace()); + }finally { + log.debug(key.toString()); + readWriteLock.writeLock().unlock(); + } + return null; + } + + @Override + public void clear() { + try { + Set keys = redisCache.redisUtil.keys(this.id); + if (!CollectionUtils.isEmpty(keys) && readWriteLock.writeLock().tryLock()) { + redisCache.redisUtil.del(keys); + } + } catch (Exception e) { + Log.error("Mybatis Clear Cache",e.getStackTrace()); + }finally { + readWriteLock.writeLock().unlock(); + } + } + + @Override + public int getSize() { + return redisCache.redisUtil.execute(); + } + + @Override + public ReadWriteLock getReadWriteLock() { + return this.readWriteLock; + } + +} diff --git a/javayh-dependencies/javayh-mybatis-starter/src/main/resources/META-INF/spring.factories b/javayh-dependencies/javayh-mybatis-starter/src/main/resources/META-INF/spring.factories index d674ed5..7278e43 100644 --- a/javayh-dependencies/javayh-mybatis-starter/src/main/resources/META-INF/spring.factories +++ b/javayh-dependencies/javayh-mybatis-starter/src/main/resources/META-INF/spring.factories @@ -1,2 +1,3 @@ -#org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +com.javayh.mybatis.cache.RedisCache #com.javayh.mybatis.filter.MybatisFilterAutoConfiguration \ No newline at end of file diff --git a/javayh-dependencies/javayh-redis-starter/src/main/java/com/javayh/redis/util/RedisUtil.java b/javayh-dependencies/javayh-redis-starter/src/main/java/com/javayh/redis/util/RedisUtil.java index e873b7e..996ebef 100644 --- a/javayh-dependencies/javayh-redis-starter/src/main/java/com/javayh/redis/util/RedisUtil.java +++ b/javayh-dependencies/javayh-redis-starter/src/main/java/com/javayh/redis/util/RedisUtil.java @@ -1,13 +1,20 @@ package com.javayh.redis.util; import lombok.extern.slf4j.Slf4j; +import org.springframework.dao.DataAccessException; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.RedisServerCommands; import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.util.CollectionUtils; +import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -61,6 +68,15 @@ public boolean expire(String key, long time) { } } + + public Integer execute() { + Long size = redisTemplate.execute(RedisServerCommands::dbSize); + return Objects.requireNonNull(size).intValue(); + } + public Set keys(String key) { + return redisTemplate.keys("*:" + key + "*"); + } + /** *

* 根据key 获取过期时间 @@ -111,11 +127,25 @@ public void del(String... key) { redisTemplate.delete(key[0]); } else { - redisTemplate.delete(CollectionUtils.arrayToList(key)); + redisTemplate.delete(Arrays.asList(key)); } } } + /** + *

+ * 删除 + *

+ * @version 1.0.0 + * @author Dylan-haiji + * @since 2020/5/11 + * @param keys + * @return void + */ + public void del(Collection keys) { + redisTemplate.delete(keys); + } + /** *

* 获取 From 266222254c979fb0dd3bba3f66ec00b7a57265c0 Mon Sep 17 00:00:00 2001 From: "haiji.yang" Date: Tue, 12 May 2020 14:50:08 +0800 Subject: [PATCH 2/2] add mybatis-redis-cache --- .../main/java/com/javayh/generator/file/GenMapperXmlFile.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javayh-plugins/javayh-generator/src/main/java/com/javayh/generator/file/GenMapperXmlFile.java b/javayh-plugins/javayh-generator/src/main/java/com/javayh/generator/file/GenMapperXmlFile.java index 71aaf23..f3d32ad 100644 --- a/javayh-plugins/javayh-generator/src/main/java/com/javayh/generator/file/GenMapperXmlFile.java +++ b/javayh-plugins/javayh-generator/src/main/java/com/javayh/generator/file/GenMapperXmlFile.java @@ -84,7 +84,7 @@ private String genContent() { .append(lineSeparator); content.append("").append(lineSeparator); - + content.append("").append(lineSeparator); // 写入共有列sql content.append(lineSeparator); content.append("\t")