Controller
package com.gameplay.org.controller;
import com.gameplay.basic.util.AjaxResult;
import com.gameplay.basic.util.PageList;
import com.gameplay.org.aop.LogAop;
import com.gameplay.org.domain.Department;
import com.gameplay.org.service.IDepartmentService;
import com.gameplay.query.DepartmentQuery;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/dept")
public class DepartmentController {
@Autowired
private IDepartmentService iDepartmentServicee;
/*
//之前定义方法的方式
@RequestMapping(value = "/findById",method = RequestMethod.GET)
public Department findById(Long id) {
return iDepartmentServicee.findById(id);
}
*/
//Restful风格定义
//路径:heep://localhost:80/dept/1
@GetMapping("/{id}")
@ApiOperation(value = "通过ID查询详情" )
public Department findById(@ApiParam(value="部门id",required = true) @PathVariable("id") Long id){
return iDepartmentServicee.findById(id);
}
//查询全部用户
@LogAop(name = "查询全部用户",sex = false)
@GetMapping()
public List<Department> findAll() {
return iDepartmentServicee.findAll();
}
//添加或修改
@LogAop(name = "添加或者修改",sex = false)
@PutMapping()
public AjaxResult addOrUpdate(@RequestBody Department department) {
try {
if (null != department.getId()) {
//修改
iDepartmentServicee.update(department);
}else {
//添加
iDepartmentServicee.add(department);
}
}catch (Exception e){
e.printStackTrace();
return AjaxResult.me().fail();
}
return AjaxResult.me().success();
}
//删除
@LogAop(name = "删除",sex = false)
@DeleteMapping("/{id}")
public AjaxResult delete(@PathVariable("id") Long id) {
iDepartmentServicee.delete(id);
return AjaxResult.me().success();
}
@PatchMapping()
public AjaxResult batchDelete(@RequestBody List<Long> ids){
iDepartmentServicee.batchDelete(ids);
return AjaxResult.me().success();
}
//分页查询
@PostMapping()
public PageList<Department> queryByPage(@RequestBody DepartmentQuery departmentQuery) {
return iDepartmentServicee.queryByPage(departmentQuery);
}
//无限极数
@GetMapping("/tree")
private List<Department> tree(){
return iDepartmentServicee.tree();
}
}
basic/util的Ajax返回和分页
Ajax返回
package com.gameplay.basic.util;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AjaxResult {
private Boolean success;
private String msg;
public AjaxResult success(){
return AjaxResult.me().setSuccess(true).setMsg("操作成功");
}
public AjaxResult fail(){
return AjaxResult.me().setSuccess(false).setMsg("操作失败");
}
public AjaxResult fail(String msg){
return AjaxResult.me().setSuccess(false).setMsg(msg);
}
public static AjaxResult me(){
return new AjaxResult();
}
public AjaxResult setSuccess(Boolean success) {
this.success = success;
return this;
}
public AjaxResult setMsg(String msg) {
this.msg = msg;
return this;
}
}
分页
package com.gameplay.basic.util;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageList<T> {
private Integer totals;
// vue取值 result.data.rows
// null []
private List<T> rows;
}
query文件夹的分页
package com.gameplay.query;
import com.gameplay.basic.query.BaseQuery;
import lombok.Data;
@Data
public class DepartmentQuery extends BaseQuery {
//关键字
private String keyword;
private Integer currentPage = 1;
private Integer pageSize = 5;
public Integer getStart(){
return (currentPage - 1) * pageSize;
}
}
sql
Department
<?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">
<!-- namespace取接口的全限定名 -->
<mapper namespace="com.gameplay.org.mapper.DepartmentMapper">
<resultMap id="queryByPageMap" type="com.gameplay.org.domain.Department">
<id column="id" property="id"></id>
<result column="sn" property="sn"></result>
<result column="name" property="name"></result>
<result column="dirPath" property="dirPath"></result>
<result column="state" property="state"></result>
<result column="manager_id" property="managerId"></result>
<result column="parent_id" property="parentId"></result>
<association property="manager" javaType="com.gameplay.org.domain.Employee">
<id column="teId" property="id"></id>
<result column="teUsername" property="username"></result>
</association>
<association property="parent" javaType="com.gameplay.org.domain.Department">
<id column="parentId" property="id"></id>
<result column="parentName" property="name"></result>
</association>
</resultMap>
<select id="findAll" resultType="com.gameplay.org.domain.Department">
select * from t_department
</select>
<select id="findById" resultType="com.gameplay.org.domain.Department">
select * from t_department where id = #{id}
</select>
<insert id="add">
insert into t_department(sn,`name`,dirPath,`state`,manager_id,parent_id)
values(#{sn},#{name},#{dirPath},#{state},#{managerId},#{parentId})
</insert>
<update id="update">
update t_department set sn=#{sn}, `name`=#{name}, dirPath=#{dirPath},state=#{state}, manager_id=#{managerId}, parent_id=#{parentId}
where id = #{id}
</update>
<delete id="delete">
delete from t_department where id=#{id}
</delete>
<delete id="batchDelete">
delete from t_department where id in
<foreach collection="list" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
<sql id="queryByPageSql">
<where>
<if test="null != keyword and !''.equals(keyword.trim())">
td.name like concat ('%',#{keyword},'%') or td.sn like concat ('%',#{keyword},'%')
</if>
</where>
</sql>
<select id="queryByPage" resultMap="queryByPageMap">
SELECT
td.*,
te.id teId,
te.username teUsername,
tde.id parentId,
tde.name parentName
FROM
t_department td
LEFT JOIN t_employee te ON td.manager_id = te.id
left JOIN t_department tde on td.parent_id = tde.id
<include refid="queryByPageSql"></include>
order by id
limit #{start},#{pageSize}
</select>
<select id="queryByCount" resultType="java.lang.Integer">
select count(*) from t_department td
<include refid="queryByPageSql"></include>
</select>
</mapper>
Empoyee
<?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">
<!-- namespace取接口的全限定名 -->
<mapper namespace="com.gameplay.org.mapper.EmployeeMapper">
<resultMap id="queryByPageMap" type="com.gameplay.org.domain.Employee">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="phone" property="phone"></result>
<result column="email" property="email"></result>
<result column="salt" property="salt"></result>
<result column="password" property="password"></result>
<result column="age" property="age"></result>
<result column="state" property="state"></result>
<result column="department_id" property="departmentId"></result>
<result column="logininfo_id" property="logininfoId"></result>
<result column="shop_id" property="shopId"></result>
<association property="department" javaType="com.gameplay.org.domain.Department">
<id column="teid" property="id"/>
<result column="name" property="name"/>
</association>
<association property="shop" javaType="com.gameplay.org.domain.Shop">
<id column="tsid" property="id"/>
<result column="tsname" property="name"/>
</association>
</resultMap>
<select id="findAll" resultType="com.gameplay.org.domain.Employee">
select * from t_employee
</select>
<select id="findById" resultType="com.gameplay.org.domain.Employee">
select * from t_employee where id = #{id}
</select>
<insert id="add">
insert into t_employee(username, phone, email, salt, password, age, state, department_id, logininfo_id, shop_id)
VALUES (#{username}, #{phone}, #{email}, #{salt}, #{password}, #{age}, #{state}, #{departmentId}, #{logininfoId}, #{shopId})
</insert>
<update id="update">
UPDATE t_employee
set username =#{username},
phone=#{phone},
email=#{email},
salt=#{salt},
password=#{password},
age=#{ age},
state =#{ state},
department_id=#{departmentId},
logininfo_id=#{logininfoId},
shop_id=#{shopId}
where id = #{id}
</update>
<delete id="delete">
delete from t_employee where id=#{id}
</delete>
<delete id="batchDelete">
delete from t_employee where id in
<foreach collection="list" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
<sql id="queryByPageSql">
<where>
<if test="null != keyword and !''.equals(keyword.trim())">
te.username like concat ('%',#{keyword},'%') or te.phone like concat ('%',#{keyword},'%')
</if>
</where>
</sql>
<select id="queryByPage" resultMap="queryByPageMap">
select
te.*, td.id tdId, td.`name`,ts.id tsid,ts.`name` tsname
from t_employee te
LEFT JOIN t_department td on te.department_id = td.id
LEFT JOIN t_shop ts on ts.id=te.shop_id
<include refid="queryByPageSql"></include>
limit #{start} , #{pageSize}
</select>
<select id="queryByCount" resultType="java.lang.Integer">
select count(*) from t_employee te
<include refid="queryByPageSql"></include>
</select>
</mapper>
本文介绍了一个基于RESTful风格的部门管理API设计实现,包括增删改查等核心功能,并使用MyBatis进行数据库操作。
933

被折叠的 条评论
为什么被折叠?



