From 40b20e871da8b3a742bbbe67885191b40e12b3b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Mon, 28 Aug 2023 23:15:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=A0=91=E7=BB=93=E6=9E=84=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DepartmentController.java | 102 ++++++++++++ .../springbootkwan/entity/Department.java | 32 ++++ .../mapper/DepartmentMapper.java | 93 +++++++++++ .../service/DepartmentService.java | 72 +++++++++ .../service/impl/DepartmentServiceImpl.java | 118 ++++++++++++++ .../resources/mapper/DepartmentMapper.xml | 147 ++++++++++++++++++ 6 files changed, 564 insertions(+) create mode 100644 src/main/java/com/kwan/springbootkwan/controller/DepartmentController.java create mode 100644 src/main/java/com/kwan/springbootkwan/entity/Department.java create mode 100644 src/main/java/com/kwan/springbootkwan/mapper/DepartmentMapper.java create mode 100644 src/main/java/com/kwan/springbootkwan/service/DepartmentService.java create mode 100644 src/main/java/com/kwan/springbootkwan/service/impl/DepartmentServiceImpl.java create mode 100644 src/main/resources/mapper/DepartmentMapper.xml 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 0000000..e718492 --- /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 0000000..e3e951e --- /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 0000000..788db1f --- /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 0000000..04f4281 --- /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 0000000..3d1d675 --- /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 0000000..5271261 --- /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} + + + + -- GitLab