diff --git a/mybatis_generator/pom.xml b/mybatis_generator/pom.xml new file mode 100644 index 0000000..8da62a8 --- /dev/null +++ b/mybatis_generator/pom.xml @@ -0,0 +1,57 @@ + + + + demay + com.ssm + 1.0-SNAPSHOT + + 4.0.0 + + mybatis_generator + + + + + ${basedir}/src/main/java + com.ssm.manage.dao + com.ssm.manage.entity + + ${basedir}/src/main/resources + sqlmap + + + + + dao + + + org.mybatis.generator + mybatis-generator-maven-plugin + 1.3.6 + + + mysql + mysql-connector-java + 5.1.44 + + + tk.mybatis + mapper + 3.4.5 + + + + + true + + true + + + ${basedir}/src/main/resources/generator/generatorConfig.xml + + + + + \ No newline at end of file diff --git a/mybatis_generator/src/main/resources/database.properties b/mybatis_generator/src/main/resources/database.properties new file mode 100644 index 0000000..c68882a --- /dev/null +++ b/mybatis_generator/src/main/resources/database.properties @@ -0,0 +1,4 @@ +spring.datasource.url=jdbc:mysql://192.168.31.40:3306/dcmy?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8 +spring.datasource.username=root +spring.datasource.password=123456 +spring.datasource.driver-class-name=com.mysql.jdbc.Driver diff --git a/mybatis_generator/src/main/resources/generator/generatorConfig.xml b/mybatis_generator/src/main/resources/generator/generatorConfig.xml new file mode 100644 index 0000000..727af6e --- /dev/null +++ b/mybatis_generator/src/main/resources/generator/generatorConfig.xml @@ -0,0 +1,335 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
\ No newline at end of file diff --git a/redis/pom.xml b/redis/pom.xml new file mode 100644 index 0000000..39573c3 --- /dev/null +++ b/redis/pom.xml @@ -0,0 +1,49 @@ + + + + demay + com.ssm + 1.0-SNAPSHOT + + 4.0.0 + + redis + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + commons-lang + commons-lang + + + com.fasterxml.jackson.core + jackson-core + 2.8.10 + + + com.google.code.gson + gson + + + + com.fasterxml.jackson.core + jackson-databind + 2.8.10 + + + org.projectlombok + lombok + 1.18.8 + + + + + ${project.artifactId}-${project.version} + + \ No newline at end of file diff --git a/redis/src/main/java/com/ssm/config/RedisConfig.java b/redis/src/main/java/com/ssm/config/RedisConfig.java new file mode 100644 index 0000000..54aec64 --- /dev/null +++ b/redis/src/main/java/com/ssm/config/RedisConfig.java @@ -0,0 +1,86 @@ +package com.ssm.config; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.CachingConfigurerSupport; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.MapPropertySource; +import org.springframework.data.redis.connection.RedisClusterConfiguration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.RedisPassword; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.listener.RedisMessageListenerContainer; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +import java.util.HashMap; +import java.util.Map; + + +@Configuration +@EnableCaching +@Slf4j +public class RedisConfig extends CachingConfigurerSupport { + +// @Value("${redis.host}") +// private String host; // Redis服务器地址 +// @Value("${redis.port}") +// private int port; // Redis服务器连接端口 + @Value("${spring.redis.password}") + private String password; // Redis服务器连接密码(默认为空) + @Value("${spring.redis.timeout}") + private int timeout; // 连接超时时间(毫秒) + @Value("${spring.redis.database}") + private int database; // + @Value("${spring.redis.jedis.pool.max-active}") + private int maxTotal; // 连接池最大连接数(使用负值表示没有限制) + @Value("${spring.redis.jedis.pool.max-wait}") + private int maxWaitMillis; // 连接池最大阻塞等待时间(使用负值表示没有限制) + @Value("${spring.redis.jedis.pool.max-idle}") + private int maxIdle; // 连接池中的最大空闲连接 + @Value("${spring.redis.jedis.pool.min-idle}") + private int minIdle; // 连接池中的最小空闲连接 + + + + + /** + * 实例化 RedisTemplate 对象 + * @return + */ + @Bean(name = "redisTemplate") + public RedisTemplate functionDomainRedisTemplate(@Qualifier(value = "redisConnectionFactory") RedisConnectionFactory factory) { +// logger.info("初始化RedisTemplate"); + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(factory); + + //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 + Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); + + ObjectMapper mapper = new ObjectMapper(); + mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); + serializer.setObjectMapper(mapper); + + template.setValueSerializer(serializer); + //使用StringRedisSerializer来序列化和反序列化redis的key值 + template.setKeySerializer(new StringRedisSerializer()); + template.afterPropertiesSet(); + return template; + } + +// @Bean +// public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { +// RedisMessageListenerContainer container = new RedisMessageListenerContainer(); +// container.setConnectionFactory(connectionFactory); +// return container; +// } +} diff --git a/redis/src/main/java/com/ssm/util/RedisClient.java b/redis/src/main/java/com/ssm/util/RedisClient.java new file mode 100644 index 0000000..431c371 --- /dev/null +++ b/redis/src/main/java/com/ssm/util/RedisClient.java @@ -0,0 +1,264 @@ +package com.ssm.util; + +import com.google.gson.Gson; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; +import org.springframework.dao.DataAccessException; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.core.RedisOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.SessionCallback; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.data.redis.core.script.DefaultRedisScript; +import org.springframework.data.redis.core.script.RedisScript; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.time.Duration; +import java.util.*; +import java.util.concurrent.TimeUnit; + +@Component +@Slf4j +public class RedisClient { + +// private final long DEFAULT_EXPIRE_TIME = 1 * 24 * 60 * 60L; + + public final static long EXPIRE_TIME_ONE_HOUR = 60 * 60L; + + public final static long EXPIRE_TIME_ONE_MINUTE = 60L; + + public final static long EXPIRE_TIME_FIVE_MINUTES = 5 * 60L; + + public final static long EXPIRE_TIME_TEN_MINUTES = 10 * 60L; + + public final static long EXPIRE_TIME_TEN_MINUTESTOW = 3 * 60L; + + public static final long EXPIRE_TIME_HALF_HOUR = 30 * 60L; + + public static final long EXPIRE_TIME_ONE_DAY = 24 * 60 * 60L; + + public static final long EXPIRE_DAY_STATISTICS = 3 * 24 * 60 * 60L; + + public static final long EXPIRE_MONTHLY_STATISTICS = 33 * 24 *60 *60L; + public static final String LOCK_RELEASE_SCRIPT = "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end"; + + @Resource + RedisTemplate redisTemplate; + + public static final int LOCK_EXPIRE_TIME = 10; // 锁的过期时间,单位:秒 + public static final String LOCKED = "locked"; // 分布式锁时, 设置的值 + + public RedisTemplate getRedisTemplate(){ + return redisTemplate; + } + + /** + * 通过 SETNX 实现分布式锁 + * @param lockKey 当 {@code lockKey} 不存在时, 上锁并返回 {@code true} (通过设置 {@code lockKey} 的值为 "locked" 来上锁); + * 当 {@code lockKey} 存在时, 不设置锁, 并且返回值为 {@code false}. + */ + public boolean tryAcquireLock(String lockKey) { + RedisTemplate redisTemplate = (RedisTemplate) this.redisTemplate; + ValueOperations stringStringValueOperations = redisTemplate.opsForValue(); + // 尝试获取锁,使用 SETNX 语义 + return Boolean.TRUE.equals(stringStringValueOperations.setIfAbsent(lockKey, LOCKED, Duration.ofSeconds(LOCK_EXPIRE_TIME))); + } + + public void delete(String key){ + log.warn("delete cache ,keys in ({})",key); + redisTemplate.delete(key); + } + + public void delete(Collection keys){ + log.warn("delete cache ,keys in ({})", StringUtils.join(keys.toArray(),",")); + redisTemplate.delete(keys); + } + + public long incr(String k,long l){ + return redisTemplate.opsForValue().increment(k,l); + } + + public long incr(String k){ + return this.incr(k,1L); + } + + public Long decr(String k,long l){ + return this.incr(k,-l); + } + + public long decr(String k){ + return this.incr(k, -1L); + } + + public double incrD(String k,double d){ + return redisTemplate.opsForValue().increment(k,d); + } + + public double incrD(String k){ + return this.incrD(k, 1D); + } + + public double decrD(String k,double d){ + return this.incrD(k,-d); + } + + public double decrD(String k){ + return this.incrD(k,-1D); + } + + + public void set(String k,V v,Long l){ + redisTemplate.opsForValue().set(k,v,l,TimeUnit.SECONDS); + } + + public void set(String k,V v){ + redisTemplate.opsForValue().set(k,v); + } + + public void set(String k,V v,Long l,TimeUnit timeUnit){ + redisTemplate.opsForValue().set(k,v,l,timeUnit); + } + + public void waitingSet(String k, V v, Long time, TimeUnit timeUnit) { + redisTemplate.execute(new SessionCallback() { + @Override + public Boolean execute(RedisOperations operations) throws DataAccessException { + operations.multi(); + operations.opsForValue().set((K) k, (V) v); + operations.exec(); + return true; + } + }); + } + + public void waitingSetValueWithExpirationAt(String key, V value, Date expiration) { + redisTemplate.execute(new SessionCallback() { + @Override + public Boolean execute(RedisOperations operations) throws DataAccessException { + operations.multi(); + operations.opsForValue().set((K) key, (V) value); + operations.expireAt((K) key, expiration); + operations.exec(); + return true; + } + }); + } + + public V get(String k){ + return redisTemplate.opsForValue().get(k); + } + + public void expire(String k,int seconds){ + redisTemplate.expire(k, seconds, TimeUnit.SECONDS); + } + + public void expire(String k,long seconds){ + redisTemplate.expire(k, seconds, TimeUnit.SECONDS); + } + + public List pipeLine(Map map){ + List objects = redisTemplate.execute((RedisConnection redisConnection) -> { + RedisSerializer serializer = redisTemplate.getStringSerializer(); + Gson gson = new Gson(); + redisConnection.openPipeline(); + for(String key:map.keySet()){ + redisConnection.lPush(serializer.serialize(key),serializer.serialize(gson.toJson(map.get(key)))); + } + return redisConnection.closePipeline(); + }); + return (List) objects; + } + + + public boolean hasKey(String key){ + return redisTemplate.hasKey(key); + } + + public boolean setnx(String key,V v){ + return redisTemplate.opsForValue().setIfAbsent(key,v); + } + + + public Long sAdd(String key, V... v){ + return redisTemplate.opsForSet().add(key,v); + } + + public Long sRemove(String key, V... v){ + return redisTemplate.opsForSet().remove(key,v); + } + + public Long leftPush(String key,V v){ + return redisTemplate.opsForList().leftPush(key,v); + } + public Long rightPush(String key,V v){ + return redisTemplate.opsForList().rightPush(key, v); + } + + /* + * 从列表头部开始删除值等于value的元素count次 + * LREM key count value + * count>0:从表头开始向表尾搜索,移除与value相等的元素,数量为count + * count<0:从表尾开始向表头搜索,移除与value相等的元素,数量为count的决定值 + * count=0:移除表中所有value相等的值 + * */ + public Long lRem(String key, Long count, V v){return redisTemplate.opsForList().remove(key,count,v);} + + public Long lSize(String key){ + return redisTemplate.opsForList().size(key); + } + public List lRange(String key,Long start,Long end){ + return redisTemplate.opsForList().range(key, start, end); + } + public V lIndex(String key,Long index){ + return redisTemplate.opsForList().index(key,index); + } + + + public void lSet(String key,Long index, V v){ + redisTemplate.opsForList().set(key,index, v); + } + public V right(String key){ + return redisTemplate.opsForList().rightPop(key); + } + + public Set sMembers(String key){ + return redisTemplate.opsForSet().members(key); + } + + public V sPop(String key){ + return redisTemplate.opsForSet().pop(key); + } + + public Set sRandMember(String key, long count) { + return redisTemplate.opsForSet().distinctRandomMembers(key, count); + } + + public Long sSize(String key){ + return redisTemplate.opsForSet().size(key); + } + + + public boolean isMembers(String key,V v){ + return redisTemplate.opsForSet().isMember(key,v); + } + + + + + public V getset(String key,V v){ + return redisTemplate.opsForValue().getAndSet(key,v); + } + + + public Set keys(String pattern){ + return redisTemplate.keys(pattern); + } + + + public Long evalUnlock(String script, List keys, Object...args) { + RedisScript redisScript = new DefaultRedisScript<>(script, Long.class); + return redisTemplate.execute(redisScript, keys, args); + } +}