diff --git a/src/main/java/com/kwan/springbootkwan/controller/DepartmentController.java b/src/main/java/com/kwan/springbootkwan/controller/DepartmentController.java new file mode 100644 index 0000000000000000000000000000000000000000..e7184925c2900d934b686aab8de59789a2e37a88 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/controller/DepartmentController.java @@ -0,0 +1,102 @@ +package com.kwan.springbootkwan.controller; + +import com.kwan.springbootkwan.entity.Department; +import com.kwan.springbootkwan.service.DepartmentService; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 部门(Department)表控制层 + * + * @author makejava + * @since 2023-08-28 22:17:33 + */ +@RestController +@RequestMapping("department") +public class DepartmentController { + /** + * 服务对象 + */ + @Resource + private DepartmentService departmentService; + + /** + * 分页查询 + * + * @param department 筛选条件 + * @param pageRequest 分页对象 + * @return 查询结果 + */ + @GetMapping + public ResponseEntity> queryByPage(Department department, PageRequest pageRequest) { + return ResponseEntity.ok(this.departmentService.queryByPage(department, pageRequest)); + } + + /** + * 通过主键查询单条数据 + * + * @param id 主键 + * @return 单条数据 + */ + @GetMapping("{id}") + public ResponseEntity queryById(@PathVariable("id") Integer id) { + //已知id=16,查询树结构 + return ResponseEntity.ok(this.departmentService.queryById(id)); + } + + /** + * 已知id查询树结构 + * + * @param id + * @return + */ + @GetMapping("/tree/{id}") + public ResponseEntity queryTreeById(@PathVariable("id") Integer id) { + //已知id=16,查询树结构 + return ResponseEntity.ok(this.departmentService.queryTreeById(id)); + } + + + @GetMapping("/all") + public ResponseEntity> queryTreeAll() { + return ResponseEntity.ok(this.departmentService.queryTreeAll()); + } + + /** + * 新增数据 + * + * @param department 实体 + * @return 新增结果 + */ + @PostMapping + public ResponseEntity add(Department department) { + return ResponseEntity.ok(this.departmentService.insert(department)); + } + + /** + * 编辑数据 + * + * @param department 实体 + * @return 编辑结果 + */ + @PutMapping + public ResponseEntity edit(Department department) { + return ResponseEntity.ok(this.departmentService.update(department)); + } + + /** + * 删除数据 + * + * @param id 主键 + * @return 删除是否成功 + */ + @DeleteMapping + public ResponseEntity deleteById(Integer id) { + return ResponseEntity.ok(this.departmentService.deleteById(id)); + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/entity/Department.java b/src/main/java/com/kwan/springbootkwan/entity/Department.java new file mode 100644 index 0000000000000000000000000000000000000000..e3e951e6865c7b9918fab7a5d91de07919dd918e --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/entity/Department.java @@ -0,0 +1,32 @@ +package com.kwan.springbootkwan.entity; + +import lombok.Data; + +import java.util.Date; +import java.io.Serializable; +import java.util.List; + +/** + * 部门(Department)实体类 + * + * @author makejava + * @since 2023-08-28 22:17:34 + */ +@Data +public class Department implements Serializable { + private static final long serialVersionUID = -95673125287393878L; + + private Integer id; + + private String deptName; + + private Integer parentId; + + private String remark; + + private Date createdAt; + + private Integer deleted; + + private List children; +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/mapper/DepartmentMapper.java b/src/main/java/com/kwan/springbootkwan/mapper/DepartmentMapper.java new file mode 100644 index 0000000000000000000000000000000000000000..788db1fee6dfec768c71cfc48aa701753725d623 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/mapper/DepartmentMapper.java @@ -0,0 +1,93 @@ +package com.kwan.springbootkwan.mapper; + +import com.kwan.springbootkwan.entity.Department; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.springframework.data.domain.Pageable; + +import java.util.List; + +/** + * 部门(Department)表数据库访问层 + * + * @author makejava + * @since 2023-08-28 22:17:33 + */ +@Mapper +public interface DepartmentMapper { + + /** + * 通过ID查询单条数据 + * + * @param id 主键 + * @return 实例对象 + */ + Department queryById(Integer id); + + /** + * 查询指定行数据 + * + * @param department 查询条件 + * @param pageable 分页对象 + * @return 对象列表 + */ + List queryAllByLimit(Department department, @Param("pageable") Pageable pageable); + + /** + * 统计总行数 + * + * @param department 查询条件 + * @return 总行数 + */ + long count(Department department); + + /** + * 新增数据 + * + * @param department 实例对象 + * @return 影响行数 + */ + int insert(Department department); + + /** + * 批量新增数据(MyBatis原生foreach方法) + * + * @param entities List 实例对象列表 + * @return 影响行数 + */ + int insertBatch(@Param("entities") List entities); + + /** + * 批量新增或按主键更新数据(MyBatis原生foreach方法) + * + * @param entities List 实例对象列表 + * @return 影响行数 + * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参 + */ + int insertOrUpdateBatch(@Param("entities") List entities); + + /** + * 修改数据 + * + * @param department 实例对象 + * @return 影响行数 + */ + int update(Department department); + + /** + * 通过主键删除数据 + * + * @param id 主键 + * @return 影响行数 + */ + int deleteById(Integer id); + + /** + * 根据父id查询数据 + * + * @param parentId + * @return + */ + List queryByParentId(@Param("parentId") Integer parentId); +} + diff --git a/src/main/java/com/kwan/springbootkwan/service/DepartmentService.java b/src/main/java/com/kwan/springbootkwan/service/DepartmentService.java new file mode 100644 index 0000000000000000000000000000000000000000..04f428111ba628b804fd060f6d01dd4e724bfbfc --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/service/DepartmentService.java @@ -0,0 +1,72 @@ +package com.kwan.springbootkwan.service; + +import com.kwan.springbootkwan.entity.Department; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; + +import java.util.List; + +/** + * 部门(Department)表服务接口 + * + * @author makejava + * @since 2023-08-28 22:17:34 + */ +public interface DepartmentService { + + /** + * 通过ID查询单条数据 + * + * @param id 主键 + * @return 实例对象 + */ + Department queryById(Integer id); + + /** + * 分页查询 + * + * @param department 筛选条件 + * @param pageRequest 分页对象 + * @return 查询结果 + */ + Page queryByPage(Department department, PageRequest pageRequest); + + /** + * 新增数据 + * + * @param department 实例对象 + * @return 实例对象 + */ + Department insert(Department department); + + /** + * 修改数据 + * + * @param department 实例对象 + * @return 实例对象 + */ + Department update(Department department); + + /** + * 通过主键删除数据 + * + * @param id 主键 + * @return 是否成功 + */ + boolean deleteById(Integer id); + + /** + * 根据id查询部门 + * + * @param id + * @return + */ + Department queryTreeById(Integer id); + + /** + * 查询所有部门 + * + * @return + */ + List queryTreeAll(); +} diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/DepartmentServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/DepartmentServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..3d1d675a262e34c8616a5c19a1ee739a989bfd13 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/service/impl/DepartmentServiceImpl.java @@ -0,0 +1,118 @@ +package com.kwan.springbootkwan.service.impl; + +import com.kwan.springbootkwan.entity.Department; +import com.kwan.springbootkwan.mapper.DepartmentMapper; +import com.kwan.springbootkwan.service.DepartmentService; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 部门(Department)表服务实现类 + * + * @author makejava + * @since 2023-08-28 22:17:34 + */ +@Service("departmentService") +public class DepartmentServiceImpl implements DepartmentService { + @Resource + private DepartmentMapper departmentDao; + + /** + * 通过ID查询单条数据 + * + * @param id 主键 + * @return 实例对象 + */ + @Override + public Department queryById(Integer id) { + return this.departmentDao.queryById(id); + } + + /** + * 分页查询 + * + * @param department 筛选条件 + * @param pageRequest 分页对象 + * @return 查询结果 + */ + @Override + public Page queryByPage(Department department, PageRequest pageRequest) { + long total = this.departmentDao.count(department); + return new PageImpl<>(this.departmentDao.queryAllByLimit(department, pageRequest), pageRequest, total); + } + + /** + * 新增数据 + * + * @param department 实例对象 + * @return 实例对象 + */ + @Override + public Department insert(Department department) { + this.departmentDao.insert(department); + return department; + } + + /** + * 修改数据 + * + * @param department 实例对象 + * @return 实例对象 + */ + @Override + public Department update(Department department) { + this.departmentDao.update(department); + return this.queryById(department.getId()); + } + + /** + * 通过主键删除数据 + * + * @param id 主键 + * @return 是否成功 + */ + @Override + public boolean deleteById(Integer id) { + return this.departmentDao.deleteById(id) > 0; + } + + @Override + public Department queryTreeById(Integer id) { + Department departmentDTO = new Department(); + departmentDTO.setId(0); + queryChildren(departmentDTO); + return departmentDTO; + } + + @Override + public List queryTreeAll() { + //先找出父id为0的部门 + List children = departmentDao.queryByParentId(0); + for (Department child : children) { + child.setChildren(queryChildren(child)); + } + return children; + } + + /** + * 递归查询 + * + * @param parent + */ + private List queryChildren(Department parent) { + List children = departmentDao.queryByParentId(parent.getId()); + if (CollectionUtils.isEmpty(children)) { + return null; + } + for (Department child : children) { + child.setChildren(queryChildren(child)); + } + return children; + } +} \ No newline at end of file diff --git a/src/main/resources/mapper/DepartmentMapper.xml b/src/main/resources/mapper/DepartmentMapper.xml new file mode 100644 index 0000000000000000000000000000000000000000..5271261d36f91775bb2ee0600959217844f555e2 --- /dev/null +++ b/src/main/resources/mapper/DepartmentMapper.xml @@ -0,0 +1,147 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + INSERT INTO department(dept_name, parent_id, remark, created_at, deleted) + VALUES (#{deptName}, #{parentId}, #{remark}, #{createdAt}, #{deleted}) + + + + insert into department(dept_name, parent_id, remark, created_at, deleted) + values + + (#{entity.deptName}, #{entity.parentId}, #{entity.remark}, #{entity.createdAt}, #{entity.deleted}) + + + + + insert into department(dept_name, parent_id, remark, created_at, deleted) + values + + (#{entity.deptName}, #{entity.parentId}, #{entity.remark}, #{entity.createdAt}, #{entity.deleted}) + + on duplicate key update + dept_name = values(dept_name), + parent_id = values(parent_id), + remark = values(remark), + created_at = values(created_at), + deleted = values(deleted) + + + + + update department + + + dept_name = #{deptName}, + + + parent_id = #{parentId}, + + + remark = #{remark}, + + + created_at = #{createdAt}, + + + deleted = #{deleted}, + + + where id = #{id} + + + + + DELETE + FROM department + WHERE id = #{id} + + + +