一.新增员工
- 编写新增员工接口设计的DTO
package com.sky.dto;
import lombok.Data;
import java.io.Serializable;
@Data
public class EmployeeDTO implements Serializable {
private Long id;
private String username;
private String name;
private String phone;
private String sex;
private String idNumber;
}
- controller
/**
* 新增员工
*
* @return
*/
@PostMapping
@ApiOperation("新增员工")
public Result save(@RequestBody EmployeeDTO employeeDTO) {
log.info("新增员工:{}", employeeDTO);
employeeService.save(employeeDTO);
return Result.success();
}
- service
/**
* 添加员工
* @param employeeDTO
* @return
*/
void save(@RequestBody EmployeeDTO employeeDTO);
- serviceimpl
/**
* 添加员工
*
* @param employeeDTO
* @return
*/
@Override
public void save(EmployeeDTO employeeDTO) {
Employee employee = new Employee();
BeanUtils.copyProperties(employeeDTO, employee);
employee.setStatus(StatusConstant.ENABLE);//默认启用
//生成盐
byte[] salt = new byte[SALT_LENGTH];
RANDOM.nextBytes(salt);
employee.setSalt(Base64.getEncoder().encodeToString(salt));
//对密码进行加密,默认密码为123456
String password = hashPassword(PasswordConstant.DEFAULT_PASSWORD, employee.getSalt());
employee.setPassword(password);
//设置创建时间和更新时间
employee.setCreateTime(LocalDateTime.now());
employee.setUpdateTime(LocalDateTime.now());
//设置创建人和更新人
//XJJ TODD 2024/9/7:后期需要修改
employee.setCreateUser(10L);
employee.setUpdateUser(10L);
employeeMapper.insert(employee);
}
由于我这里将Mybatis换成了Mybatis-plus,所以Mybatis相关的代码不再描写
- 引入依赖
<!-- mybatisPlus 核心库 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
- 修改application.yml配置文件
# mybatis-plus配置
mybatis-plus:
configuration:
# 驼峰命名
map-underscore-to-camel-case: true
# 日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- 修改employee实体类,添加ID自动增长策略
package com.sky.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("employee")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private String username;
private String name;
private String password;
private String phone;
private String sex;
private String idNumber;
private Integer status;
//@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
//@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
private Long createUser;
private Long updateUser;
private String salt;
}
- 删除resource下的mapper包,将server中的mapper包改为继承自BaseMapper
package com.sky.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.dto.EmployeeDTO;
import com.sky.entity.Employee;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
- 修改启动类,增加mapper扫描区间
package com.sky;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理
@MapperScan("com.sky.mapper") //扫描mapper接口
@Slf4j
public class SkyApplication {
public static void main(String[] args) {
SpringApplication.run(SkyApplication.class, args);
log.info("server started");
}
}
二.员工分页查询
- 编写分页查询接口的DTO
package com.sky.dto;
import lombok.Data;
import java.io.Serializable;
@Data
public class EmployeePageQueryDTO implements Serializable {
//员工姓名
private String name;
//页码
private int page;
//每页显示记录数
private int pageSize;
}
- controller
@GetMapping("/page")
@ApiOperation("分页查询员工")
public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) {
log.info("分页查询员工:{}", employeePageQueryDTO);
employeeService.page(employeePageQueryDTO);
return Result.success();
}
- service
/**
* 分页查询员工
* @param employeePageQueryDTO
* @return
*/
PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO);
- serviceimpl
@Transactional
@Override
public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
// 创建分页对象
Page<Employee> page = new Page<>(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
// 创建查询条件
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
// 排除姓名为null的记录
queryWrapper.isNotNull(Employee::getName);
// 使用like方法进行模糊查询,参数化防止SQL注入
if (StringUtils.isNotBlank(employeePageQueryDTO.getName())) {
queryWrapper.like(Employee::getName, employeePageQueryDTO.getName());
}
// 根据创建时间降序排序
queryWrapper.orderByDesc(Employee::getCreateTime);
// 执行分页查询
Page<Employee> employeePage = employeeMapper.selectPage(page, queryWrapper);
// 提取结果
long total = employeePage.getTotal();
List<Employee> records = employeePage.getRecords();
// 返回结果
return new PageResult(total, records);
}
注意:使用MP分页时不要配置MP分页插件,否则会找不到对象(Mapper)