Commit 7abfefba authored by shengnan hu's avatar shengnan hu
Browse files

init

parents
Pipeline #281 passed with stage
in 1 minute and 55 seconds
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mall4j.cloud.biz.mapper.AttachFileGroupMapper">
<resultMap id="attachFileGroupMap" type="com.mall4j.cloud.biz.model.AttachFileGroup">
<id column="attach_file_group_id" property="attachFileGroupId" />
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<result column="shop_id" property="shopId"/>
<result column="name" property="name"/>
</resultMap>
<sql id="Vo_Column_List">
`attach_file_group_id`,`create_time`,`update_time`,`shop_id`,`name`
</sql>
<select id="list" resultType="com.mall4j.cloud.biz.vo.AttachFileGroupVO">
select <include refid="Vo_Column_List"/> from attach_file_group
where shop_id = #{shopId}
order by attach_file_group_id desc
</select>
<select id="getByAttachFileGroupId" resultType="com.mall4j.cloud.biz.vo.AttachFileGroupVO">
select <include refid="Vo_Column_List"/> from attach_file_group where attach_file_group_id = #{attachFileGroupId}
</select>
<insert id="save">
insert into attach_file_group (`shop_id`,`name`)
values (#{attachFileGroup.shopId},#{attachFileGroup.name});
</insert>
<update id="update">
update attach_file_group
<set>
<if test="attachFileGroup.shopId != null">
`shop_id` = #{attachFileGroup.shopId},
</if>
<if test="attachFileGroup.name != null">
`name` = #{attachFileGroup.name},
</if>
</set>
where attach_file_group_id = #{attachFileGroup.attachFileGroupId}
</update>
<delete id="deleteById">
delete from attach_file_group where attach_file_group_id = #{attachFileGroupId}
</delete>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mall4j.cloud.biz.mapper.AttachFileMapper">
<resultMap id="attachFileMap" type="com.mall4j.cloud.biz.model.AttachFile">
<id column="file_id" property="fileId" />
<result column="file_path" property="filePath"/>
<result column="file_type" property="fileType"/>
<result column="file_name" property="fileName"/>
<result column="file_size" property="fileSize"/>
<result column="shop_id" property="shopId"/>
<result column="type" property="type"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
</resultMap>
<sql id="Vo_Column_List">
`file_id`,`file_path`,`file_type`,`file_name`,`file_size`,`shop_id`,`type`,`create_time`,`update_time`,`attach_file_group_id`
</sql>
<select id="list" resultType="com.mall4j.cloud.biz.vo.AttachFileVO">
select <include refid="Vo_Column_List"/> from attach_file
WHERE `type` = 1 AND shop_id = #{shopId}
<if test="fileName != null and fileName != ''">
AND file_name LIKE CONCAT('%',#{fileName},'%')
</if>
<if test="fileGroupId != null and fileGroupId != ''">
AND attach_file_group_id = #{fileGroupId}
</if>
ORDER BY update_time DESC, file_id DESC
</select>
<insert id="save">
insert into attach_file (`file_path`,`file_type`,`file_name`,`file_size`,`shop_id`,`type`,`attach_file_group_id`)
values
<foreach collection="attachFiles" item="attachFile" separator=",">
(#{attachFile.filePath},#{attachFile.fileType},#{attachFile.fileName},#{attachFile.fileSize},#{shopId},#{attachFile.type},#{attachFile.attachFileGroupId})
</foreach>
</insert>
<update id="update">
update attach_file
<set>
<if test="attachFile.fileName != null">
`file_name` = #{attachFile.fileName},
</if>
<if test="attachFile.attachFileGroupId != null">
`attach_file_group_id` = #{attachFile.attachFileGroupId},
</if>
</set>
where file_id = #{attachFile.fileId}
</update>
<delete id="deleteById">
delete from attach_file where file_id = #{fileId}
</delete>
<select id="getById" resultMap="attachFileMap">
select <include refid="Vo_Column_List"/> from attach_file where file_id = #{fileId}
</select>
<update id="updateBatchByAttachFileGroupId">
update attach_file
set attach_file_group_id = 0
where attach_file_group_id = #{attachFileGroupId}
</update>
</mapper>
### 一些通用的缓存配置
缓存key命名规范:
服务名:业务名:方法名:key, 大小写以下划线分割 如
mall4cloud_oauth:token:access:
mall4cloud_oauth:token:refresh_to_access:
mall4cloud_oauth:token:uid_to_access:
<?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">
<parent>
<artifactId>mall4cloud-common</artifactId>
<groupId>com.mall4j.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mall4cloud-common-cache</artifactId>
<packaging>jar</packaging>
<description>mall4cloud 缓存相关代码</description>
<dependencies>
<dependency>
<groupId>com.mall4j.cloud</groupId>
<artifactId>mall4cloud-common-core</artifactId>
<version>${project.version}</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-integration</artifactId>
</dependency>
<!--分布式锁-->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-redis</artifactId>
</dependency>
</dependencies>
</project>
package com.mall4j.cloud.common.cache.adapter;
import com.mall4j.cloud.common.cache.bo.CacheNameWithTtlBO;
import java.util.List;
/**
* 实现该接口之后,根据缓存的cacheName和ttl将缓存进行过期
*
* @author FrozenWatermelon
* @date 2020/7/4
*/
public interface CacheTtlAdapter {
/**
* 根据缓存的cacheName和ttl将缓存进行过期
* @return 需要独立设置过期时间的缓存列表
*/
List<CacheNameWithTtlBO> listCacheNameWithTtl();
}
package com.mall4j.cloud.common.cache.bo;
/**
* 通过 cacheName 配置 和 时间告诉缓存多久清楚一遍
*
* @author FrozenWatermelon
* @date 2020/7/4
*/
public class CacheNameWithTtlBO {
private String cacheName;
private Integer ttl;
public CacheNameWithTtlBO(String cacheName, Integer ttl) {
this.cacheName = cacheName;
this.ttl = ttl;
}
public String getCacheName() {
return cacheName;
}
public void setCacheName(String cacheName) {
this.cacheName = cacheName;
}
public Integer getTtl() {
return ttl;
}
public void setTtl(Integer ttl) {
this.ttl = ttl;
}
@Override
public String toString() {
return "CacheNameWithTtlBO{" + "cacheName='" + cacheName + '\'' + ", ttl=" + ttl + '}';
}
}
package com.mall4j.cloud.common.cache.config;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.mall4j.cloud.common.cache.adapter.CacheTtlAdapter;
import com.mall4j.cloud.common.cache.bo.CacheNameWithTtlBO;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
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.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* @author FrozenWatermelon
* @date 2020/7/4
*/
@EnableCaching
@Configuration
public class RedisCacheConfig {
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, CacheTtlAdapter cacheTtlAdapter) {
RedisCacheManager redisCacheManager = new RedisCacheManager(
RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
// 默认策略,未配置的 key 会使用这个
this.getRedisCacheConfigurationWithTtl(3600),
// 指定 key 策略
this.getRedisCacheConfigurationMap(cacheTtlAdapter));
redisCacheManager.setTransactionAware(true);
return redisCacheManager;
}
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap(CacheTtlAdapter cacheTtlAdapter) {
if (cacheTtlAdapter == null) {
return Collections.emptyMap();
}
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(16);
for (CacheNameWithTtlBO cacheNameWithTtlBO : cacheTtlAdapter.listCacheNameWithTtl()) {
redisCacheConfigurationMap.put(cacheNameWithTtlBO.getCacheName(),
getRedisCacheConfigurationWithTtl(cacheNameWithTtlBO.getTtl()));
}
return redisCacheConfigurationMap;
}
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer()))
.entryTtl(Duration.ofSeconds(seconds));
return redisCacheConfiguration;
}
/**
* 自定义redis序列化的机制,重新定义一个ObjectMapper.防止和MVC的冲突
* https://juejin.im/post/5e869d426fb9a03c6148c97e
*/
@Bean
public RedisSerializer<Object> redisSerializer() {
ObjectMapper objectMapper = new ObjectMapper();
// 反序列化时候遇到不匹配的属性并不抛出异常
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 序列化时候遇到空对象不抛出异常
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// 反序列化的时候如果是无效子类型,不抛出异常
objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
// 不使用默认的dateTime进行序列化,
objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
// 使用JSR310提供的序列化类,里面包含了大量的JDK8时间序列化类
objectMapper.registerModule(new JavaTimeModule());
// 启用反序列化所需的类型信息,在属性中添加@class
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.PROPERTY);
// 配置null值的序列化器
GenericJackson2JsonRedisSerializer.registerNullValueSerializer(objectMapper, null);
return new GenericJackson2JsonRedisSerializer(objectMapper);
}
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory,
RedisSerializer<Object> redisSerializer) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setDefaultSerializer(redisSerializer);
template.setValueSerializer(redisSerializer);
template.setHashValueSerializer(redisSerializer);
template.setKeySerializer(StringRedisSerializer.UTF_8);
template.setHashKeySerializer(StringRedisSerializer.UTF_8);
template.afterPropertiesSet();
return template;
}
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
return new StringRedisTemplate(redisConnectionFactory);
}
@Bean
@ConditionalOnMissingBean
public CacheTtlAdapter cacheTtl() {
return Collections::emptyList;
}
}
package com.mall4j.cloud.common.cache.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.integration.redis.util.RedisLockRegistry;
/**
* @author FrozenWatermelon
* @date 2020/12/29
*/
@Configuration
public class RedisLockConfiguration {
@Bean
public RedisLockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory){
return new RedisLockRegistry(redisConnectionFactory,"spring-cloud");
}
}
package com.mall4j.cloud.common.cache.constant;
/**
* @author FrozenWatermelon
* @date 2021/01/25
*/
public interface BizCacheNames {
/**
* 前缀
*/
String COUPON_PREFIX = "mall4cloud_biz:";
}
package com.mall4j.cloud.common.cache.constant;
/**
* 缓存名字
*
* @author FrozenWatermelon
* @date 2020/7/9
*/
public interface CacheNames extends RbacCacheNames,OauthCacheNames,ProductCacheNames,MultishopCacheNames,PlatformCacheNames,BizCacheNames, UserCacheNames {
/**
*
* 参考CacheKeyPrefix
* cacheNames 与 key 之间的默认连接字符
*/
String UNION = "::";
/**
* key内部的连接字符(自定义)
*/
String UNION_KEY = ":";
}
package com.mall4j.cloud.common.cache.constant;
/**
* @author lhd
* @date 2020/12/28
*/
public interface ConfigCacheNames {
/**
* 缓存配置名称前缀
*/
String SYS_CONFIG = "sys_config:";
/**
* 缓存配置对象前缀
*/
String SYS_CONFIG_OBJECT = SYS_CONFIG + "object:";
}
package com.mall4j.cloud.common.cache.constant;
/**
* @author FrozenWatermelon
* @date 2020/11/23
*/
public interface MultishopCacheNames {
/**
* 前缀
*/
String MULTISHOP_PREFIX = "mall4cloud_multishop:";
/**
* 店铺分类列表缓存key
*/
String SHOP_DETAIL_ID_KEY = MULTISHOP_PREFIX + "shop_detail:getById:";
/**
* 店铺分类列表缓存key
*/
String INDEX_IMG_KEY = MULTISHOP_PREFIX + "index_img";
/**
* 店铺分类列表缓存key
*/
String NOTICES_KEY = MULTISHOP_PREFIX + "notices";
String MULTISHOP_SIMPLE_INFO_KEY = MULTISHOP_PREFIX + "simple_info";
String HOT_SEARCH_LIST_KEY = MULTISHOP_PREFIX + "hot_search_list";
}
package com.mall4j.cloud.common.cache.constant;
/**
* @author FrozenWatermelon
* @date 2020/11/23
*/
public interface OauthCacheNames {
/**
* oauth 授权相关key
*/
String OAUTH_PREFIX = "mall4cloud_oauth:";
/**
* token 授权相关key
*/
String OAUTH_TOKEN_PREFIX = OAUTH_PREFIX + "token:";
/**
* 保存token 缓存使用key
*/
String ACCESS = OAUTH_TOKEN_PREFIX + "access:";
/**
* 刷新token 缓存使用key
*/
String REFRESH_TO_ACCESS = OAUTH_TOKEN_PREFIX + "refresh_to_access:";
/**
* 根据uid获取保存的token key缓存使用的key
*/
String UID_TO_ACCESS = OAUTH_TOKEN_PREFIX + "uid_to_access:";
}
package com.mall4j.cloud.common.cache.constant;
/**
* @author FrozenWatermelon
* @date 2020/11/23
*/
public interface OrderCacheNames {
/**
* 前缀
*/
String ORDER_PREFIX = "mall4cloud_order:";
/**
* 确认订单信息缓存
*/
String ORDER_CONFIRM_KEY = ORDER_PREFIX + "order:confirm";
/**
* 订单uuid
*/
String ORDER_CONFIRM_UUID_KEY = ORDER_PREFIX + "order:uuid_confirm";
}
package com.mall4j.cloud.common.cache.constant;
/**
* @author FrozenWatermelon
* @date 2020/11/23
*/
public interface PlatformCacheNames {
/**
* 前缀
*/
String PLATFORM_PREFIX = "mall4cloud_platform:";
String PLATFORM_SIMPLE_INFO_KEY = PLATFORM_PREFIX + "simple_info";
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment