协享云图分析--4图片模块
图片表sql
-- 图片表
create table if not exists picture
( id bigint auto_increment comment 'id' primary key, url varchar(512) not null comment '图片 url', name varchar(128) not null comment '图片名称', introduction varchar(512) null comment '简介', category varchar(64) null comment '分类', tags varchar(512) null comment '标签(JSON 数组)', picSize bigint null comment '图片体积', picWidth int null comment '图片宽度', picHeight int null comment '图片高度', picScale double null comment '图片宽高比例', picFormat varchar(32) null comment '图片格式', userId bigint not null comment '创建用户 id', createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间', editTime datetime default CURRENT_TIMESTAMP not null comment '编辑时间', updateTime datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间', isDelete tinyint default 0 not null comment '是否删除', INDEX idx_name (name), -- 提升基于图片名称的查询性能 INDEX idx_introduction (introduction), -- 用于模糊搜索图片简介 INDEX idx_category (category), -- 提升基于分类的查询性能 INDEX idx_tags (tags), -- 提升基于标签的查询性能 INDEX idx_userId (userId) -- 提升基于用户 ID 的查询性能
) comment '图片' collate = utf8mb4_unicode_ci;
该SQL代码定义了一个名为createTime的字段,数据类型为datetime,默认值为当前时间戳CURRENT_TIMESTAMP,且不能为空。
定义了一个名为idx_category的索引,作用于字段category,用于提升基于分类查询的性能。
通过创建索引,数据库可以更快地定位与指定分类相关的记录。
config
CorsConfig
@Configuration
public class CorsConfig implements WebMvcConfigurer {/*** 添加跨域请求的映射* * @param registry CorsRegistry对象,用于注册跨域请求的映射*/@Overridepublic void addCorsMappings(CorsRegistry registry) {// 覆盖所有请求registry.addMapping("/**")// 允许发送 Cookie.allowCredentials(true)// 放行哪些域名(必须用 patterns,否则 * 会和 allowCredentials 冲突).allowedOriginPatterns("*")// 允许的请求方法.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")// 允许的请求头.allowedHeaders("*")// 暴露的响应头.exposedHeaders("*");}
}
配置所有请求路径(/**)支持跨域。
允许携带凭据(如Cookie)。
放行所有来源域名(使用allowedOriginPatterns("*")避免与allowCredentials(true)冲突)。
允许的HTTP方法包括GET、POST、PUT、DELETE、OPTIONS。
允许所有请求头和响应头。
配置所有请求路径(/**)支持跨域。
允许携带凭据(如Cookie)。
放行所有来源域名(使用allowedOriginPatterns("*")避免与allowCredentials(true)冲突)。
允许的HTTP方法包括GET、POST、PUT、DELETE、OPTIONS。
允许所有请求头和响应头。
@Configuration
@Configuration 是 Spring 框架中的一个注解,用于标记一个类为配置类。它的作用是替代传统的 XML 配置文件,通过 Java 代码的方式定义应用的配置。被 @Configuration 标记的类中可以包含多个以 @Bean 注解的方法,用于定义 Spring 容器中的 Bean。
在当前代码中,@Configuration 标记了 CorsConfig 类,表示这是一个配置类,Spring 会扫描并加载该类中的配置(如跨域设置),将其纳入容器管理。
CosClientConfig
@Configuration
@ConfigurationProperties(prefix = "cos.client")
@Data
public class CosClientConfig {/*** 域名*/private String host;/*** secretId*/private String secretId;/*** 密钥(注意不要泄露)*/private String secretKey;/*** 区域*/private String region;/*** 桶名*/private String bucket;@Beanpublic COSClient cosClient() {// 1 初始化用户身份信息(secretId, secretKey)。// SECRETID 和 SECRETKEY 请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);// 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。ClientConfig clientConfig = new ClientConfig(new Region(region));// 这里建议设置使用 https 协议// 从 5.6.54 版本开始,默认使用了 httpsclientConfig.setHttpProtocol(HttpProtocol.https);// 3 生成 cos 客户端。return new COSClient(cred, clientConfig);}
}
这段代码的功能是通过 @ConfigurationProperties 注解将配置文件中以 cos.client 为前缀的属性绑定到当前类或对象中。具体功能如下:
- 自动加载配置文件中的属性值。
- 根据指定的前缀 cos.client 过滤相关属性。
- 支持类型安全的属性绑定。