// 启动类,位置必须高于其他类
@SpringBootApplication // 启动类注解
@MapperScan("com.itheima.mapper") // 指定mapper接口所在的包
@EnableAspectJAutoProxy // 开启aop
@ServletComponentScan
public class SpringBootDemoApplication {
// springboot内置了tomcat,端口号默认8080 (约定大于配置)
public static void main(String[] args) {
// 固定写法
// 参数一:启动类.class
// 参数二:args
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
# 大小写敏感
# 缩进表示层级关系
# 参数值和冒号一定要有空格
# #代表注释
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot
username: root
password: 1234
mybatis:
configuration:
# 开启驼峰映射
map-underscore-to-camel-case: true
# 日志级别( debug info warn error )
logging:
level:
com.itheima: info
file:
name: D:\\spring.log
# 只允许本地IP访问
server:
address: 127.0.0.1
# 大小写敏感
# 缩进表示层级关系
# 参数值和冒号一定要有空格
# #代表注释
server:
port: 8081
servlet:
context-path: /itheima
# 对象:键值对的集合
user:
username: '张三'
password: '123456'
addressList: # 数组:固定写法
- '杭州'
- '北京'
- '上海'
@RestController
public class IndexController {
// @Value("${配置路径}"):读取配置
@Value("${user.username}")
private String username;
@Value("${user.password}")
private String password;
@GetMapping("/index")
public String index() {
return "hello spring boot";
}
}
@Data
@Configuration // 配置类
@ConfigurationProperties(prefix = "user") // 读取配置文件中指定配置
public class UserConfig {
private String username;
private String password;
private List<String> addressList;
}
@RestController
public class FileController {
@Autowired
private OssTemplate ossTemplate;
// 文件上传
@PostMapping("/upload")
public Result uploadFile(MultipartFile image) throws IOException {
// 调用阿里云工具类上传文件
String filePath = ossTemplate.upload(image.getOriginalFilename(), image.getInputStream());
// 返回文件路径
return Result.success(filePath);
}
}