<!--引入spring cloud alibaba-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--解决Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-oss-spring-boot-starter</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘com.cevent.yameng.web.mall.product.YamengMallProductApplicationTests’: Unsatisfied dependency expressed through field ‘ossClient’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.aliyun.oss.OSSClient’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aliCloudEdasSdk' defined in class path resource [com/alibaba/cloud/spring/boot/context/autoconfigure/EdasContextAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.cloud.context.edas.AliCloudEdasSdk]: Factory method 'aliCloudEdasSdk' threw exception; nested exception is java.lang.NoSuchMethodError: com.aliyuncs.profile.DefaultProfile.getHttpClientConfig()Lcom/aliyuncs/http/HttpClientConfig;
<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>yameng-mallartifactId>
<groupId>com.cevent.yameng.web.mallgroupId>
<version>0.0.1-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>yameng-mall-commonartifactId>
<description>各个微服务系统依赖的公共服务包,包括bean、utils等工具类description>
<dependencies>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.3.1version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.8version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpcoreartifactId>
<version>4.4.13version>
dependency>
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
<version>2.6version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.17version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alicloud-ossartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.1.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
project>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.3.RELEASEversion>
<relativePath/>
parent>
<groupId>com.cevent.yameng.web.mallgroupId>
<artifactId>yameng-mall-productartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>yameng-mall-productname>
<description>亚盟商城-商品服务模组description>
<properties>
<java.version>1.8java.version>
<spring-cloud.version>Hoxton.SR8spring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>com.cevent.yameng.web.mallgroupId>
<artifactId>yameng-mall-commonartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
<version>3.10.2version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
spring:
datasource:
username: root
password: cevent
url: jdbc:mysql://***:3306/***?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.jdbc.Driver
#4.配置nacos注册中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
#5.配置阿里巴巴-cloud-oss,这里对应common.pom的spring-cloud-alibaba-dependencies(2.1.0.RELEASE),不兼容
alicloud:
access-key: ***
secret-key: ***
oss:
endpoint: ***
##1.mapper-locations默认值: private String[] mapperLocations = new String[]{"classpath*:/mapper/**/*.xml"};
#classpath*:扫描包括本product系统及各个引用包下的类路径
#classpath:只扫描本product系统
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml
#2.调整entity实体类的tableId为自增主键,数据量大后,进行分库分表的主键生成规则/自定义生成规则
global-config:
db-config:
id-type: auto
# mybatis全局逻辑删除:1代表删除 0代表没有删除
logic-delete-value: 1
logic-not-delete-value: 0
#3.配置启动端口
server:
port: 6622
#4.设置打印日志
logging:
level:
com.cevent.yameng.web.mall: debug
#5.配置阿里巴巴-cloud-oss,这里是新版本aliyun的配置,不兼容
#alibaba:
# cloud:
# access-key: ***
# secret-key: ***
# oss:
# endpoint: ***
package com.cevent.yameng.web.mall.product;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cevent.yameng.web.mall.product.entity.BrandEntity;
import com.cevent.yameng.web.mall.product.service.BrandService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
/**
* 测试brand-service
*/
@SpringBootTest
class YamengMallProductApplicationTests {
@Autowired
BrandService brandService;
//1.测试新增品牌
@Test
void contextLoads() {
BrandEntity brandEntity = new BrandEntity();
brandEntity.setName("亚盟");
brandService.save(brandEntity);
System.out.println("保存的brand-entity:" + brandEntity.getName());
}
//2.修改品牌
@Test
void updateBrand() {
BrandEntity brandEntity = new BrandEntity();
//id为long
brandEntity.setBrandId(1L);
brandEntity.setDescript("亚盟电子商务有限公司");
brandService.updateById(brandEntity);
System.out.println("更新的brand-entity:" + brandEntity.getDescript());
}
//3.查询品牌
@Test
void selectBrand() {
List<BrandEntity> brandEntityList;
//包装查询wrapper,根据指定id查询集合
brandEntityList = brandService.list(new QueryWrapper<BrandEntity>().eq("brand_id", 1L));
//遍历集合
brandEntityList.forEach((item) -> {
System.out.println("查询的brand-entity:" + item);
});
}
//4.oss文件上传测试,这里与OSSClient冲突
@Test
void testUpload() throws FileNotFoundException {
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "endpoint";
// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,
// 创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建。
String accessKeyId = "ak";
String accessKeySecret = "sk";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上传文件流
//1.指定文件
InputStream inputStream = new FileInputStream("D:\\CEVENT_INFO\\1_marknelson_anotherplace_t.jpg");
//2.上传到bucket
ossClient.putObject("cevent-yameng-shop", "cevent_icon.jpg", inputStream);
// 关闭OSSClient。
ossClient.shutdown();
System.out.println("OSS上传完成!");
}
@Autowired
OSSClient ossClient;
//5.alibaba-cloud-oss文件上传测试
@Test
void testCloudOSSUpload() throws FileNotFoundException {
// 上传文件流
//1.指定文件
InputStream inputStream = new FileInputStream("D:\\CEVENT_INFO\\icon1.jpg");
//2.上传到bucket
ossClient.putObject("cevent-yameng-shop", "cevent_red2.jpg", inputStream);
// 关闭OSSClient。
ossClient.shutdown();
System.out.println("aliyun-OSS上传完成!");
}
}