准备工作
上图mapper类型错了,不是class,是interface,修正过后的图片,如下所示
修正如下
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/webm
spring.datasource.username=www123
spring.datasource.password=www123
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis.configuration.map-underscore-to-camel-case=true
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
private Integer code;//响应码,1 代表成功; 0 代表失败
private String msg; //响应信息 描述字符串
private Object data; //返回的数据
//增删改 成功响应
public static Result success(){
return new Result(1,"success",null);
}
//查询 成功响应
public static Result success(Object data){
return new Result(1,"success",data);
}
//失败响应
public static Result error(String msg){
return new Result(0,msg,null);
}
}
数据库准备
-- 部门管理
create table dept(
id int unsigned primary key auto_increment comment '主键ID',
name varchar(10) not null unique comment '部门名称',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间'
) comment '部门表';
insert into dept (id, name, create_time, update_time) values(1,'学工部',now(),now()),(2,'教研部',now(),now()),(3,'咨询部',now(),now()), (4,'就业部',now(),now()),(5,'人事部',now(),now());
-- 员工管理(带约束)
create table emp (
id int unsigned primary key auto_increment comment 'ID',
username varchar(20) not null unique comment '用户名',
password varchar(32) default '123456' comment '密码',
name varchar(10) not null comment '姓名',
gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',
image varchar(300) comment '图像',
job tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师',
entrydate date comment '入职时间',
dept_id int unsigned comment '部门ID',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间'
) comment '员工表';
INSERT INTO emp
(id, username, password, name, gender, image, job, entrydate,dept_id, create_time, update_time) VALUES
(1,'jinyong','123456','金庸',1,'1.jpg',4,'2000-01-01',2,now(),now()),
(2,'zhangwuji','123456','张无忌',1,'2.jpg',2,'2015-01-01',2,now(),now()),
(3,'yangxiao','123456','杨逍',1,'3.jpg',2,'2008-05-01',2,now(),now()),
(4,'weiyixiao','123456','韦一笑',1,'4.jpg',2,'2007-01-01',2,now(),now()),
(5,'changyuchun','123456','常遇春',1,'5.jpg',2,'2012-12-05',2,now(),now()),
(6,'xiaozhao','123456','小昭',2,'6.jpg',3,'2013-09-05',1,now(),now()),
(7,'jixiaofu','123456','纪晓芙',2,'7.jpg',1,'2005-08-01',1,now(),now()),
(8,'zhouzhiruo','123456','周芷若',2,'8.jpg',1,'2014-11-09',1,now(),now()),
(9,'dingminjun','123456','丁敏君',2,'9.jpg',1,'2011-03-11',1,now(),now()),
(10,'zhaomin','123456','赵敏',2,'10.jpg',1,'2013-09-05',1,now(),now()),
(11,'luzhangke','123456','鹿杖客',1,'11.jpg',5,'2007-02-01',3,now(),now()),
(12,'hebiweng','123456','鹤笔翁',1,'12.jpg',5,'2008-08-18',3,now(),now()),
(13,'fangdongbai','123456','方东白',1,'13.jpg',5,'2012-11-01',3,now(),now()),
(14,'zhangsanfeng','123456','张三丰',1,'14.jpg',2,'2002-08-01',2,now(),now()),
(15,'yulianzhou','123456','俞莲舟',1,'15.jpg',2,'2011-05-01',2,now(),now()),
(16,'songyuanqiao','123456','宋远桥',1,'16.jpg',2,'2007-01-01',2,now(),now()),
(17,'chenyouliang','123456','陈友谅',1,'17.jpg',NULL,'2015-03-21',NULL,now(),now());
部门管理
查询
测试
package com.web.management.controller;
import com.web.management.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class DeptController {
@GetMapping("/depts")
public Result list(){
log.info("查询全部部门数据");
return Result.success();
}
}
localhost:8080/depts
实现
pojo-controller-service-serviceimpl-mapper
pojo
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {
private Integer id; //ID
private String name; //部门名称
private LocalDateTime createTime; //创建时间
private LocalDateTime updateTime; //修改时间
}
controller
package com.web.management.controller;
import com.web.management.pojo.Dept;
import com.web.management.pojo.Result;
import com.web.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@GetMapping("/depts")
public Result list(){
log.info("查询全部部门数据");
//调用service查询部门数据
List<Dept> deptList = deptService.list();
return Result.success(deptList);
}
}
service
package com.web.management.service;
import com.web.management.pojo.Dept;
import org.springframework.stereotype.Service;
import java.util.List;
public interface DeptService {
// 查询全部部门数据
List<Dept> list();
}
serviceimpl
package com.web.management.service.impl;
import com.web.management.mapper.DeptMapper;
import com.web.management.pojo.Dept;
import com.web.management.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public List<Dept> list() {
return deptMapper.list();
}
}
mapper
package com.web.management.mapper;
import com.web.management.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface DeptMapper {
@Select("select * from dept")
List<Dept> list();
}
可以利用标红报错这个功能快速创建。
删除
实现
pojo-controller-service-serviceimpl-mapper
pojo
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {
private Integer id; //ID
private String name; //部门名称
private LocalDateTime createTime; //创建时间
private LocalDateTime updateTime; //修改时间
}
controller
@DeleteMapping("/depts/{id}")
public Result delete(@PathVariable Integer id){
deptService.delete(id);
log.info("成功删除{}",id);
return Result.success();
}
service
// 删除部门
void delete(Integer id);
serviceimpl
@Override
public void delete(Integer id) {
deptMapper.delete(id);
}
mapper
@Delete("delete from dept where id = #{id}")
void delete(Integer id);
新增
实现
pojo-controller-service-serviceimpl-mapper
pojo
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {
private Integer id; //ID
private String name; //部门名称
private LocalDateTime createTime; //创建时间
private LocalDateTime updateTime; //修改时间
}
controller
@PostMapping("/depts")
public Result add(@RequestBody Dept dept){
log.info("新增成功");
deptService.add(dept);
return Result.success();
}
service
void add(Dept dept);
serviceimpl-补全属性
@Override
public void add(Dept dept) {
dept.setCreateTime(LocalDateTime.now());
dept.setUpdateTime(LocalDateTime.now());
deptMapper.insert(dept);
}
mapper
@Insert("insert into dept(name, create_time, update_time) values(#{name},#{createTime},#{updateTime})")
void insert(Dept dept);
根据ID查询部门
pojo
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {
private Integer id; //ID
private String name; //部门名称
private LocalDateTime createTime; //创建时间
private LocalDateTime updateTime; //修改时间
}
controller
// 根据id查询部门
@GetMapping("/depts/{id}")
public Result sebyid(@PathVariable Integer id){
log.info("查询部门为{}",id);
Dept dept = new Dept();
dept= deptService.sebyid(id);
return Result.success(dept);
}
service
Dept sebyid(Integer id);
serviceimpl-补全属性
// 根据id查询部门
@Override
public Dept sebyid(Integer id) {
return deptMapper.sebyid(id);
}
mapper
// 根据ID查询部门
@Select("select * from dept where id = #{id}")
Dept sebyid(Integer id);
修改部门
pojo
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {
private Integer id; //ID
private String name; //部门名称
private LocalDateTime createTime; //创建时间
private LocalDateTime updateTime; //修改时间
}
controller
// 修改部门
@PutMapping("/depts")
public Result update(@RequestBody Dept dept){
log.info("修改:{}",dept);
deptService.update(dept);
return Result.success();
}
service
void update(Dept dept);
serviceimpl-补全属性
// 修改部门
@Override
public void update(Dept dept) {
dept.setCreateTime(LocalDateTime.now());
dept.setUpdateTime(LocalDateTime.now());
deptMapper.update(dept);
}
mapper
// 修改部门
@Update("update dept set name = #{name} where id = #{id}")
void update(Dept dept);
注:名字不能是表中已出现的名字
员工管理
分页查询
pojo
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean {
private long total;//总记录数
private List rows;//数据列表
}
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
private Integer id; //ID
private String username; //用户名
private String password; //密码
private String name; //姓名
private Short gender; //性别 , 1 男, 2 女
private String image; //图像url
private Short job; //职位 , 1 班主任 , 2 讲师 , 3 学工主管 , 4 教研主管 , 5 咨询师
private LocalDate entrydate; //入职日期
private Integer deptId; //部门ID
private LocalDateTime createTime; //创建时间
private LocalDateTime updateTime; //修改时间
}
controller
package com.web.management.controller;
import com.web.management.pojo.PageBean;
import com.web.management.pojo.Result;
import com.web.management.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class EmpController {
@Autowired
private EmpService empService;
@GetMapping("/emps")
public Result pagebean(@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "10") int pageSize){
log.info("查询的页数是{},一共{}页",page,pageSize);
PageBean pageBean = empService.page(page,pageSize);
return Result.success(pageBean);
}
}
service
package com.web.management.service;
import com.web.management.pojo.PageBean;
import org.springframework.stereotype.Service;
public interface EmpService {
PageBean page(Integer page, Integer pageSize);
}
serviceimpl-补全属性,记得一定要自动注入mapper
package com.web.management.service.impl;
import com.web.management.mapper.EmpMapper;
import com.web.management.pojo.Emp;
import com.web.management.pojo.PageBean;
import com.web.management.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public PageBean page(Integer page, Integer pageSize) {
//查询所有记录数
Long total = empMapper.total();
//分页查询
Integer start = (page - 1) * pageSize;
List<Emp> empList = empMapper.page(start, pageSize);
PageBean pageBean = new PageBean(total,empList);
return pageBean;
}
}
mapper
package com.web.management.mapper;
import com.web.management.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface EmpMapper {
@Select("select count(*) from emp")
Long total();
@Select("select * from emp limit #{start},#{pageSize}")
List<Emp> page(Integer start, Integer pageSize);
}
根据条件查询
pojo
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean {
private long total;//总记录数
private List rows;//数据列表
}
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
private Integer id; //ID
private String username; //用户名
private String password; //密码
private String name; //姓名
private Short gender; //性别 , 1 男, 2 女
private String image; //图像url
private Short job; //职位 , 1 班主任 , 2 讲师 , 3 学工主管 , 4 教研主管 , 5 咨询师
private LocalDate entrydate; //入职日期
private Integer deptId; //部门ID
private LocalDateTime createTime; //创建时间
private LocalDateTime updateTime; //修改时间
}
controller
package com.web.management.controller;
import com.web.management.pojo.PageBean;
import com.web.management.pojo.Result;
import com.web.management.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Slf4j
@RestController
public class EmpController {
@Autowired
private EmpService empService;
@GetMapping("/emps")
public Result pagebean(@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "10") int pageSize,
String name, Short gender,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
log.info("查询的页数是{},一共{}页",page,pageSize,name,gender,begin,end);
PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);
return Result.success(pageBean);
}
}
service
package com.web.management.service;
import com.web.management.pojo.PageBean;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
public interface EmpService {
PageBean page(Integer page, Integer pageSize,
String name, Short gender,LocalDate begin, LocalDate end);
}
serviceimpl-补全属性,记得一定要自动注入mapper
package com.web.management.service.impl;
import com.web.management.mapper.EmpMapper;
import com.web.management.pojo.Emp;
import com.web.management.pojo.PageBean;
import com.web.management.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
//查询所有记录数
Long total = empMapper.total();
//分页查询
Integer start = (page - 1) * pageSize;
List<Emp> empList = empMapper.li(start, pageSize,name,gender,begin,end);
PageBean pageBean = new PageBean(total,empList);
return pageBean;
}
}
mapper
package com.web.management.mapper;
import com.web.management.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List;
@Mapper
public interface EmpMapper {
@Select("select count(*) from emp")
Long total();
public List<Emp> li(Integer start, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end);
}
xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.web.management.mapper.EmpMapper">
<select id="li" resultType="com.web.management.pojo.Emp">
select *
from emp
<where>
<if test="name != null and name != ''">
name like concat('%',#{name},'%')
</if>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="begin != null and end != null">
and entrydate between #{begin} and #{end}
</if>
</where>
order by update_time desc
</select>
</mapper>
这里有个疑问,下列sql语句在navicat中可以实现,但是在idea中显示语法错误,有大佬知道为什么吗
SELECT * FROM emp
WHERE
name LIKE CONCAT('%','张','%')
AND gender = 1
and entrydate BETWEEN '2000-01-01' and '2015-01-01'
order BY entrydate desc LIMIT 1,1
删除员工
pojo
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
private Integer id; //ID
private String username; //用户名
private String password; //密码
private String name; //姓名
private Short gender; //性别 , 1 男, 2 女
private String image; //图像url
private Short job; //职位 , 1 班主任 , 2 讲师 , 3 学工主管 , 4 教研主管 , 5 咨询师
private LocalDate entrydate; //入职日期
private Integer deptId; //部门ID
private LocalDateTime createTime; //创建时间
private LocalDateTime updateTime; //修改时间
}
controller
@DeleteMapping("/emps/{ids}")
public Result delete(@PathVariable List<Integer> ids){
log.info("删除{}",ids);
empService.delete(ids);
return Result.success();
}
service
void delete(List<Integer> ids);
serviceimpl-补全属性,记得一定要自动注入mapper
@Override
public void delete(List<Integer> ids) {
empMapper.delete(ids);
}
mapper
void delete(List<Integer> ids);
xml文件
<delete id="delete">
delete from emp
where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
新增员工
pojo
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
private Integer id; //ID
private String username; //用户名
private String password; //密码
private String name; //姓名
private Short gender; //性别 , 1 男, 2 女
private String image; //图像url
private Short job; //职位 , 1 班主任 , 2 讲师 , 3 学工主管 , 4 教研主管 , 5 咨询师
private LocalDate entrydate; //入职日期
private Integer deptId; //部门ID
private LocalDateTime createTime; //创建时间
private LocalDateTime updateTime; //修改时间
}
controller
// 新增员工
@PostMapping("/emps")
public Result save(@RequestBody Emp emp){
log.info("增加:{}",emp);
empService.save(emp);
return Result.success();
}
service
void save(Emp emp);
serviceimpl-补全属性,记得一定要自动注入mapper
// 新增员工
@Override
public void save(Emp emp) {
emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
empMapper.insert(emp);
}
mapper
@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);
根据id查询员工
pojo
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
private Integer id; //ID
private String username; //用户名
private String password; //密码
private String name; //姓名
private Short gender; //性别 , 1 男, 2 女
private String image; //图像url
private Short job; //职位 , 1 班主任 , 2 讲师 , 3 学工主管 , 4 教研主管 , 5 咨询师
private LocalDate entrydate; //入职日期
private Integer deptId; //部门ID
private LocalDateTime createTime; //创建时间
private LocalDateTime updateTime; //修改时间
}
controller
// 根据id查询员工
@GetMapping("/emps/{id}")
public Result sebyid(@PathVariable Integer id){
log.info("查询id为{}",id);
Emp emp = empService.sebyid(id);
return Result.success(emp);
}
service
Emp sebyid(Integer id);
serviceimpl-补全属性,记得一定要自动注入mapper
@Override
public Emp sebyid(Integer id) {
return empMapper.sebyid(id);
}
mapper
// 根据id查询员工
@Select("select * from emp where id = #{id}")
Emp sebyid(Integer id);
修改员工--或者使用动态sql
pojo
package com.web.management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
private Integer id; //ID
private String username; //用户名
private String password; //密码
private String name; //姓名
private Short gender; //性别 , 1 男, 2 女
private String image; //图像url
private Short job; //职位 , 1 班主任 , 2 讲师 , 3 学工主管 , 4 教研主管 , 5 咨询师
private LocalDate entrydate; //入职日期
private Integer deptId; //部门ID
private LocalDateTime createTime; //创建时间
private LocalDateTime updateTime; //修改时间
}
controller
// 修改员工
@PutMapping("/emps")
public Result update(@RequestBody Emp emp){
log.info("修改为:{}",emp);
empService.update(emp);
return Result.success();
}
service
Emp update(Emp emp);
serviceimpl-补全属性,记得一定要自动注入mapper
@Override
public Emp update(Emp emp) {
emp.setUpdateTime(LocalDateTime.now());
emp.setCreateTime(LocalDateTime.now());
return empMapper.update(emp);
}
mapper
// 修改员工信息
@Update("update emp set username=#{username},name=#{name},gender=#{gender},image=#{image},job=#{job},entrydate=#{entrydate},"+
"dept_id=#{deptId},create_time=#{createTime},update_time=#{updateTime} where id = #{id}")
Emp update(Emp emp);
数据库修改成功,但是不知道为什么idea显示事务失败。
文件上传
本地存储
package com.web.management.controller;
import com.web.management.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.UUID;
@Slf4j
@RestController
public class UploadController {
@PostMapping("/upload")
public Result upload(String name, Integer age, MultipartFile image) throws Exception{
log.info("文件上传:{},{},{}",name,age,image);
// 获取原始文件名
String originalFilename = image.getOriginalFilename();
// 构造唯一的文件名,使用uuid
int lastIndexOf = originalFilename.lastIndexOf(".");
String suffix = originalFilename.substring(lastIndexOf);
String newFileName = UUID.randomUUID().toString() + suffix;
log.info("新的文件名是:{}",newFileName);
// 将文件存储到本地磁盘目录中
image.transferTo(new File(new File("G:\\JavaWebStudy\\images").getAbsoluteFile() + "/" + newFileName));
return Result.success();
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传文件</title>
</head>
<body>
<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>
</body>
</html>
设置上传文件大小
#配置单个文件上传大小限制
spring.servlet.multipart.max-file-size=10MB
#配置单个请求最大大小的限制(一次请求中是可以上传多个文件)
spring.servlet.multipart.max-request-size=100MB
使用阿里云oss上传
https://www.bilibili.com/video/BV1m84y1w7Tb?p=150&spm_id_from=pageDriver&vd_source=67e1472a35e11fe6fa2aec58d24f330c
配置文件
参数配置化
yml或者yaml配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/webm
username: www123
password: www123
servlet:
multipart:
max-file-size: 10MB
max-request-size: 100MB
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
注解使用
小细节
1、路径参数传递时,在controller层用注解@PathVariable绑定值,例:
@DeleteMapping("/emps/{ids}")
public Result delete(@PathVariable List<Integer> ids){
}
2、动态sql就用mybatis的xml文件写sql语句。
3、封装实体类要在controller层使用注解@RequestBody,例
public Result save(@RequestBody Emp emp){
log.info("增加:{}",emp);
}
4、log.info中的{}替代法,替代引号后的变量,例:
log.info("增加:{}",emp);
如果你觉得本文对你有用的话,请随意打赏~