Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringBoot-kwan
提交
40b20e87
S
SpringBoot-kwan
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
SpringBoot-kwan
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringBoot-kwan
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
40b20e87
编写于
8月 28, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:树结构查询
上级
65120f54
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
564 addition
and
0 deletion
+564
-0
src/main/java/com/kwan/springbootkwan/controller/DepartmentController.java
.../kwan/springbootkwan/controller/DepartmentController.java
+102
-0
src/main/java/com/kwan/springbootkwan/entity/Department.java
src/main/java/com/kwan/springbootkwan/entity/Department.java
+32
-0
src/main/java/com/kwan/springbootkwan/mapper/DepartmentMapper.java
...java/com/kwan/springbootkwan/mapper/DepartmentMapper.java
+93
-0
src/main/java/com/kwan/springbootkwan/service/DepartmentService.java
...va/com/kwan/springbootkwan/service/DepartmentService.java
+72
-0
src/main/java/com/kwan/springbootkwan/service/impl/DepartmentServiceImpl.java
...an/springbootkwan/service/impl/DepartmentServiceImpl.java
+118
-0
src/main/resources/mapper/DepartmentMapper.xml
src/main/resources/mapper/DepartmentMapper.xml
+147
-0
未找到文件。
src/main/java/com/kwan/springbootkwan/controller/DepartmentController.java
0 → 100644
浏览文件 @
40b20e87
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
<
Page
<
Department
>>
queryByPage
(
Department
department
,
PageRequest
pageRequest
)
{
return
ResponseEntity
.
ok
(
this
.
departmentService
.
queryByPage
(
department
,
pageRequest
));
}
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping
(
"{id}"
)
public
ResponseEntity
<
Department
>
queryById
(
@PathVariable
(
"id"
)
Integer
id
)
{
//已知id=16,查询树结构
return
ResponseEntity
.
ok
(
this
.
departmentService
.
queryById
(
id
));
}
/**
* 已知id查询树结构
*
* @param id
* @return
*/
@GetMapping
(
"/tree/{id}"
)
public
ResponseEntity
<
Department
>
queryTreeById
(
@PathVariable
(
"id"
)
Integer
id
)
{
//已知id=16,查询树结构
return
ResponseEntity
.
ok
(
this
.
departmentService
.
queryTreeById
(
id
));
}
@GetMapping
(
"/all"
)
public
ResponseEntity
<
List
<
Department
>>
queryTreeAll
()
{
return
ResponseEntity
.
ok
(
this
.
departmentService
.
queryTreeAll
());
}
/**
* 新增数据
*
* @param department 实体
* @return 新增结果
*/
@PostMapping
public
ResponseEntity
<
Department
>
add
(
Department
department
)
{
return
ResponseEntity
.
ok
(
this
.
departmentService
.
insert
(
department
));
}
/**
* 编辑数据
*
* @param department 实体
* @return 编辑结果
*/
@PutMapping
public
ResponseEntity
<
Department
>
edit
(
Department
department
)
{
return
ResponseEntity
.
ok
(
this
.
departmentService
.
update
(
department
));
}
/**
* 删除数据
*
* @param id 主键
* @return 删除是否成功
*/
@DeleteMapping
public
ResponseEntity
<
Boolean
>
deleteById
(
Integer
id
)
{
return
ResponseEntity
.
ok
(
this
.
departmentService
.
deleteById
(
id
));
}
}
\ No newline at end of file
src/main/java/com/kwan/springbootkwan/entity/Department.java
0 → 100644
浏览文件 @
40b20e87
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
<
Department
>
children
;
}
\ No newline at end of file
src/main/java/com/kwan/springbootkwan/mapper/DepartmentMapper.java
0 → 100644
浏览文件 @
40b20e87
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
<
Department
>
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<Department> 实例对象列表
* @return 影响行数
*/
int
insertBatch
(
@Param
(
"entities"
)
List
<
Department
>
entities
);
/**
* 批量新增或按主键更新数据(MyBatis原生foreach方法)
*
* @param entities List<Department> 实例对象列表
* @return 影响行数
* @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参
*/
int
insertOrUpdateBatch
(
@Param
(
"entities"
)
List
<
Department
>
entities
);
/**
* 修改数据
*
* @param department 实例对象
* @return 影响行数
*/
int
update
(
Department
department
);
/**
* 通过主键删除数据
*
* @param id 主键
* @return 影响行数
*/
int
deleteById
(
Integer
id
);
/**
* 根据父id查询数据
*
* @param parentId
* @return
*/
List
<
Department
>
queryByParentId
(
@Param
(
"parentId"
)
Integer
parentId
);
}
src/main/java/com/kwan/springbootkwan/service/DepartmentService.java
0 → 100644
浏览文件 @
40b20e87
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
<
Department
>
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
<
Department
>
queryTreeAll
();
}
src/main/java/com/kwan/springbootkwan/service/impl/DepartmentServiceImpl.java
0 → 100644
浏览文件 @
40b20e87
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
<
Department
>
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
<
Department
>
queryTreeAll
()
{
//先找出父id为0的部门
List
<
Department
>
children
=
departmentDao
.
queryByParentId
(
0
);
for
(
Department
child
:
children
)
{
child
.
setChildren
(
queryChildren
(
child
));
}
return
children
;
}
/**
* 递归查询
*
* @param parent
*/
private
List
<
Department
>
queryChildren
(
Department
parent
)
{
List
<
Department
>
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
src/main/resources/mapper/DepartmentMapper.xml
0 → 100644
浏览文件 @
40b20e87
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
namespace=
"com.kwan.springbootkwan.mapper.DepartmentMapper"
>
<resultMap
type=
"com.kwan.springbootkwan.entity.Department"
id=
"DepartmentMap"
>
<result
property=
"id"
column=
"id"
jdbcType=
"INTEGER"
/>
<result
property=
"deptName"
column=
"dept_name"
jdbcType=
"VARCHAR"
/>
<result
property=
"parentId"
column=
"parent_id"
jdbcType=
"INTEGER"
/>
<result
property=
"remark"
column=
"remark"
jdbcType=
"VARCHAR"
/>
<result
property=
"createdAt"
column=
"created_at"
jdbcType=
"TIMESTAMP"
/>
<result
property=
"deleted"
column=
"deleted"
jdbcType=
"INTEGER"
/>
</resultMap>
<!--查询单个-->
<select
id=
"queryById"
resultMap=
"DepartmentMap"
>
SELECT id
, dept_name
, parent_id
, remark
, created_at
, deleted
FROM department
WHERE id = #{id}
</select>
<!--查询指定行数据-->
<select
id=
"queryAllByLimit"
resultMap=
"DepartmentMap"
>
select
id, dept_name, parent_id, remark, created_at, deleted
from department
<where>
<if
test=
"id != null"
>
and id = #{id}
</if>
<if
test=
"deptName != null and deptName != ''"
>
and dept_name = #{deptName}
</if>
<if
test=
"parentId != null"
>
and parent_id = #{parentId}
</if>
<if
test=
"remark != null and remark != ''"
>
and remark = #{remark}
</if>
<if
test=
"createdAt != null"
>
and created_at = #{createdAt}
</if>
<if
test=
"deleted != null"
>
and deleted = #{deleted}
</if>
</where>
limit #{pageable.offset}, #{pageable.pageSize}
</select>
<!--统计总行数-->
<select
id=
"count"
resultType=
"java.lang.Long"
>
select count(1)
from department
<where>
<if
test=
"id != null"
>
and id = #{id}
</if>
<if
test=
"deptName != null and deptName != ''"
>
and dept_name = #{deptName}
</if>
<if
test=
"parentId != null"
>
and parent_id = #{parentId}
</if>
<if
test=
"remark != null and remark != ''"
>
and remark = #{remark}
</if>
<if
test=
"createdAt != null"
>
and created_at = #{createdAt}
</if>
<if
test=
"deleted != null"
>
and deleted = #{deleted}
</if>
</where>
</select>
<select
id=
"queryByParentId"
resultMap=
"DepartmentMap"
>
SELECT id,dept_name, parent_id, remark, created_at, deleted
FROM department
WHERE 1=1
<if
test=
"parentId != null"
>
AND parent_id = #{parentId}
</if>
</select>
<!--新增所有列-->
<insert
id=
"insert"
keyProperty=
"id"
useGeneratedKeys=
"true"
>
INSERT INTO department(dept_name, parent_id, remark, created_at, deleted)
VALUES (#{deptName}, #{parentId}, #{remark}, #{createdAt}, #{deleted})
</insert>
<insert
id=
"insertBatch"
keyProperty=
"id"
useGeneratedKeys=
"true"
>
insert into department(dept_name, parent_id, remark, created_at, deleted)
values
<foreach
collection=
"entities"
item=
"entity"
separator=
","
>
(#{entity.deptName}, #{entity.parentId}, #{entity.remark}, #{entity.createdAt}, #{entity.deleted})
</foreach>
</insert>
<insert
id=
"insertOrUpdateBatch"
keyProperty=
"id"
useGeneratedKeys=
"true"
>
insert into department(dept_name, parent_id, remark, created_at, deleted)
values
<foreach
collection=
"entities"
item=
"entity"
separator=
","
>
(#{entity.deptName}, #{entity.parentId}, #{entity.remark}, #{entity.createdAt}, #{entity.deleted})
</foreach>
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)
</insert>
<!--通过主键修改数据-->
<update
id=
"update"
>
update department
<set>
<if
test=
"deptName != null and deptName != ''"
>
dept_name = #{deptName},
</if>
<if
test=
"parentId != null"
>
parent_id = #{parentId},
</if>
<if
test=
"remark != null and remark != ''"
>
remark = #{remark},
</if>
<if
test=
"createdAt != null"
>
created_at = #{createdAt},
</if>
<if
test=
"deleted != null"
>
deleted = #{deleted},
</if>
</set>
where id = #{id}
</update>
<!--通过主键删除-->
<delete
id=
"deleteById"
>
DELETE
FROM department
WHERE id = #{id}
</delete>
</mapper>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录