在 Web 后端开发领域,实现复杂业务功能是开发者的核心任务之一。本文将围绕 Tlias 系统的员工管理模块,详细介绍新增员工、事务管理以及文件上传功能的开发过程,通过实际代码示例帮助读者深入理解相关技术要点。
新增员工功能涉及员工基本信息和工作经历信息的存储。在数据库设计上,使用emp
表存储员工基本信息,emp_expr
表存储员工工作经历信息。
接口用于添加员工信息,请求路径为/emps
,请求方式为POST
,参数格式为application/json
。参数包括用户名、姓名、性别等,其中用户名、姓名为必填项。
参数名称 | 类型 | 是否必须 | 备注 |
---|---|---|---|
username | string | 是 | 用户名,2 - 20 个字 |
name | string | 是 | 姓名,2 - 10 个字 |
gender | number | 非必须 | 性别,1 代表男,2 代表女 |
image | string | 非必须 | 图像 |
deptId | number | 非必须 | 部门 id |
entryDate | string | 非必须 | 入职日期 |
job | number | 非必须 | 职位,1 班主任,2 讲师,3 学工主管,4 教研主管,5 咨询师 |
salary | number | 非必须 | 薪资 |
exprList | object[] | 非必须 | 工作经历列表 |
@RestController
@RequestMapping("/emps")
public class EmpController {
@Autowired
private EmpService empService;
@PostMapping
public Result save(@RequestBody Emp emp) {
log.info("请求参数emp: {}", emp);
empService.save(emp);
return Result.success();
}
}
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Autowired
private EmpExprMapper empExprMapper;
@Override
public void save(Emp emp) {
emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
empMapper.insert(emp);
Integer empId = emp.getId();
List exprList = emp.getExprList();
if (!CollectionUtils.isEmpty(exprList)) {
exprList.forEach(empExpr -> empExpr.setEmpId(empId));
empExprMapper.insertBatch(exprList);
}
}
}
@Mapper
public interface EmpMapper {
@Insert("insert into emp(username, name, gender, phone, job, salary, image, entry_date, dept_id, create_time, update_time) values (#{username},#{name},#{gender},#{phone},#{job},#{salary},#{image},#{entryDate},#{deptId},#{createTime},#{updateTime})")
void insert(Emp emp);
}
insert into emp_expr (emp_id, begin, end, company, job) values
(#{expr.empId}, #{expr.begin}, #{expr.end}, #{expr.company}, #{expr.job})
事务是一组操作的集合,要么全部成功,要么全部失败。在新增员工功能中,若保存员工基本信息成功但保存工作经历失败,会导致数据不一致,因此需要事务管理。
事务控制主要包括开启事务、提交事务和回滚事务。在 MySQL 中,默认事务是自动提交的,可使用start transaction
或begin
开启事务,commit
提交事务,rollback
回滚事务。
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Autowired
private EmpExprMapper empExprMapper;
@Transactional
@Override
public void save(Emp emp) {
emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
empMapper.insert(emp);
int i = 1 / 0;
Integer empId = emp.getId();
List exprList = emp.getExprList();
if (!CollectionUtils.isEmpty(exprList)) {
exprList.forEach(empExpr -> empExpr.setEmpId(empId));
empExprMapper.insertBatch(exprList);
}
}
}
REQUIRED
(默认,有事务则加入,无则创建新事务)、REQUIRES_NEW
(总是创建新事务)等。文件上传是将本地文件上传到服务器的过程,在项目中应用广泛。
前端通过form
表单上传文件,设置enctype="multipart/form-data"
。后端在 Controller 层接收文件。
@Slf4j
@RestController
public class UploadController {
@PostMapping("/upload")
public Result handleFileUpload(String name, Integer age, MultipartFile file) {
log.info("文件上传:{}", file);
return Result.success();
}
}
将接收到的文件存储在本地磁盘目录,需保证文件名唯一。
private static final String UPLOAD_DIR = "D:/upload/";
@PostMapping("/upload")
public Result handleFileUpload(MultipartFile file) throws Exception {
log.info("文件上传:{}", file);
String uniqueFileName = generateUniqueFileName(file.getOriginalFilename());
file.transferTo(new File(UPLOAD_DIR + uniqueFileName));
return Result.success();
}
private String generateUniqueFileName(String originalFilename) {
String randomStr = UUID.randomUUID().toString().replaceAll("-", "");
String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
return randomStr + extension;
}
@Autowired
private AliyunOSSOperator aliyunOSSOperator;
@PostMapping("/upload")
public Result upload(MultipartFile file) throws Exception {
log.info("文件上传:{}", file);
String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
return Result.success(url);
}
配置文件注入参数,可使用@Value
或@ConfigurationProperties
注解。
aliyun:
oss:
endpoint: https://oss-cn-beijing.aliyuncs.com
bucketName: java-ai
// 使用@Value注解
@Component
public class AliyunOSSOperator {
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Value("${aliyun.oss.bucketName}")
private String bucketName;
// ... 省略
}
// 使用@ConfigurationProperties注解
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliyunOSSProperties {
private String endpoint;
private String bucketName;
}
@Component
public class AliyunOSSOperator {
@Autowired
private AliyunOSSProperties aliyunOSSProperties;
public String upload(byte[] content, String originalFilename) throws Exception {
String endpoint = aliyunOSSProperties.getEndpoint();
String bucketName = aliyunOSSProperties.getBucketName();
// ... 省略
}
}
通过以上对新增员工、事务管理和文件上传功能的开发介绍,希望读者能掌握 Web 后端开发中这些关键功能的实现方法,为实际项目开发提供有力支持。