目录
1.新增员工
EmpController中:
@PostMapping
public Result save(@RequestBody Emp emp){
log.info("新增员工, emp: {}",emp);
empService.save(emp);
return Result.success(); //不需要返回值,无参
}
EmpService接口方法:
void save(Emp emp);
实现类当中:
@Override
public void save(Emp emp) {
emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
empMapper.insert(emp);
}
Empmapper方法
@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) " +
" values(#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")
void insert(Emp emp);
2.文件上传
前端页面三要素:
<form action="/upload" method="post" enctype="multipart/form-data"> //往哪提交 提交方式 编码格式
姓名: <input type="text" name="username"><br>
年龄: <input type="text" name="age"><br>
头像: <input type="file" name="image"><br>
<input type="submit" value="提交">
</form>
服务端接收文件:
@Slf4j
@RestController
public class UploadController {
@PostMapping("/upload")
public Result upload(String username , Integer age , MultipartFile image) {
log.info("文件上传: {}, {}, {}", username, age, image);return Result.success();
}}
本地存储
在服务端,接收到上传上来的文件之后,将文件存储在本地服务器磁盘中。
@Autowired
private AliOSSUtils aliOSSUtils;
//本地存储文件
@PostMapping("/upload")
public Result upload(String username , Integer age , MultipartFile image) throws Exception {
log.info("文件上传: {}, {}, {}", username, age, image);
//获取原始文件名 - 1.jpg 123.0.0.jpg
String originalFilename = image.getOriginalFilename();//构造唯一的文件名 (不能重复) - uuid(通用唯一识别码) de49685b-61c0-4b11-80fa-c71e95924018
int index = originalFilename.lastIndexOf("."); //最后一个点所处位置
String extname = originalFilename.substring(index); //截取//构建新文件名
String newFileName = UUID.randomUUID().toString() + extname;
log.info("新的文件名: {}", newFileName);
//将文件存储在服务器的磁盘目录中 E:\images
image.transferTo(new File("E:\\images\\"+newFileName));return Result.success();
}
在SpringBoot中,文件上传,默认单个文件允许最大大小为 1M。如果需要上传大文件,可以进行如下配置:
#配置单个文件最大上传大小
spring.servlet.multipart.max-file-size=10MB
#配置单个请求最大上传大小(一次请求可以上传多个文件)
spring.servlet.multipart.max-request-size=100MB
阿里云OSS
阿里云是阿里巴巴集团旗下全球领先的云计算公司,也是国内最大的云服务提供商 。
SDK:Software Development Kit 的缩写,软件开发工具包,包括辅助软件开发的依赖(jar包)、代码示例等,都可以叫做SDK。
Bucket:存储空间是用户用于存储对象(Object,就是文件)的容器,所有的对象都必须隶属于某个存储空间。
SDK:软件开发工具包,包括辅助软件开发的依赖(jar包)、代码示例等,都可以叫做SDK。
集成
1.引入OSS上传文件工具类
2.上传图片接口开发
3.修改员工
先根据ID查询出来,页面回旋展示,然后修改员工
(1)查询回显
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id){
log.info("根据ID查询员工信息, id: {}",id);
Emp emp = empService.getById(id);
return Result.success(emp);
}
Emp getById(Integer id);
@Override
public Emp getById(Integer id) {
return empMapper.getById(id);
}
@Select("select * from emp where id = #{id}")
Emp getById(Integer id);
(2)修改员工
@PutMapping
public Result update(@RequestBody Emp emp){
log.info("更新员工信息 : {}", emp);
empService.update(emp);
return Result.success();
}
void update(Emp emp);
@Override
public void update(Emp emp) {
emp.setUpdateTime(LocalDateTime.now());empMapper.update(emp);
}
void update(Emp emp);
XML中:
<!--更新员工-->
<update id="update">
update emp
<set>
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="password != null and password != ''">
password = #{password},
</if>
<if test="name != null and name != ''">
name = #{name},
</if>
<if test="gender != null">
gender = #{gender},
</if>
<if test="image != null and image != ''">
image = #{image},
</if>
<if test="job != null">
job = #{job},
</if>
<if test="entrydate != null">
entrydate = #{entrydate},
</if>
<if test="deptId != null">
dept_id = #{deptId},
</if>
<if test="updateTime != null">
update_time = #{updateTime}
</if>
</set>
where id = #{id}
</update>
4.配置文件
参数配置化
@Value 注解通常用于外部配置的属性注入,具体用法为: @Value("${配置文件中的key}")
配置格式
yml配置文件
基本语法:
- 大小写敏感 数值前边必须有空格,作为分隔符
- 使用缩进表示层级关系,缩进时,不允许使用Tab键,只能用空格(idea中会自动将Tab转换为空
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
- # 表示注释,从这个字符一直到行尾,都会被解析器忽略
yml数据格式
对象/Map集合:
user:
name: zhangsan
age: 18
password: 123456
数组/List/Set集合:
hobby:
- java
- game
- sport
application.yml 中:
spring:
#数据库连接信息
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/tlias
username: root
password: 1234
#文件上传的配置
servlet:
multipart:
max-file-size: 10MB
max-request-size: 100MB
#Mybatis配置
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true#阿里云OSS
aliyun:
oss:
endpoint: https://oss-cn-hangzhou.aliyuncs.com
accessKeyId: LTAI4GCH1vX6DKqJWxd6nEuW
accessKeySecret: yBshYweHOpqDuhCArrVHwIiBKpyqSL
bucketName: web-tlias
@ConfigurationProperties
将配置项的值自动注入到对象的属性当中
1.配置文件中Key的名字必须要和实体类当中的属性名一致,为属性名提供get,set方法(+@Data)
2.需要将类交给IOC容器管理,成为IOC容器bean对象,+@Component
3.prefix属性指定配置项的属性
创建一个新的utils下:AliOSSProperties:
配置提示信息(可有可无)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
1.
2.
3.
@ConfigurationProperties 与 @Valu
相同点:都是用来注入外部配置的属性的。
不同点:@Value注解只能一个一个的进行外部属性的注入。
@ConfigurationProperties可以批量的将外部的属性配置注入到bean对象的属性中