话不多说,直接整代码
实体类:
package com.example.system03.entity; import lombok.Data; import java.io.Serializable; import java.sql.Timestamp; @Data public class Department implements Serializable { private String department_name; private String manager_name; private int manager_id; private String department_describe; private String status; private String is_deleted; private Timestamp created_time; private String created_by; private Timestamp modified_time; private String modified_by; }
mapper类:
package com.example.system03.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.system03.entity.Department; import com.example.system03.entity.Role; import com.example.system03.entity.Vo.DepartmentSelectVo; import com.example.system03.entity.Vo.DepartmentUpdateVo; import com.example.system03.entity.Vo.RoleSelectVo; import com.example.system03.service.DepartmentService; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface DepartmentMapper extends BaseMapper<Department> { List<Department> queryDepartmentList(@Param("DepartmentSelectVo") DepartmentSelectVo departmentSelectVo, @Param("numStart") int numStart, @Param("numEnd") int numEnd); int getLastCount(); void departmentDelete(@Param("department") Department department); Department departmentFind(String department_name); void departmentUpdate(@Param("n") DepartmentUpdateVo departmentUpdateVo); void departmentInsert(@Param("n") DepartmentUpdateVo departmentUpdateVo); }
mapper.xml配置文件:
<?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.example.system03.mapper.DepartmentMapper"> <select id="queryDepartmentList" resultType="com.example.system03.entity.Department"> select SQL_CALC_FOUND_ROWS * from department where (department_name like '%${DepartmentSelectVo.filter_department_name}%' or #{DepartmentSelectVo.filter_department_name} = '') and (is_deleted = '0') order by ${DepartmentSelectVo.sort} ${DepartmentSelectVo.sortMethod} limit #{numStart}, #{numEnd} </select> <select id="getLastCount" resultType="integer"> select FOUND_ROWS() </select> <update id="departmentDelete"> update department <set> is_deleted = "1", modified_time = #{department.modified_time}, modified_by = #{department.modified_by}, status = #{department.status} </set> where department_name = #{department.department_name} </update> <select id="departmentFind" resultType="com.example.system03.entity.Department" parameterType="string"> select * from department where department_name = #{department_name}; </select> <update id="departmentUpdate"> update department <set> <if test="n.manager_name != null">manager_name = #{n.manager_name},</if> <if test="n.manager_id != null">manager_id = #{n.manager_id},</if> <if test="n.department_describe != null">department_describe = #{n.department_describe},</if> <if test="n.status != null">status = #{n.status},</if> modified_by = #{n.modified_by}, modified_time = #{n.modified_time} </set> where department_name = #{n.department_name} </update> <insert id="departmentInsert"> insert into department (department_name, manager_name, manager_id, department_describe, status, is_deleted, created_time, created_by, modified_time, modified_by) values (#{n.department_name}, #{n.manager_name}, #{n.manager_id}, #{n.department_describe},#{n.status},#{n.is_deleted},#{n.created_time},#{n.created_by},#{n.modified_time},#{n.modified_by}) </insert> </mapper>
service层:
package com.example.system03.service; import com.baomidou.mybatisplus.extension.service.IService; import com.example.system03.entity.Department; import com.example.system03.entity.Vo.DepartmentSelectVo; import com.example.system03.entity.Vo.DepartmentUpdateVo; import org.springframework.stereotype.Service; import java.util.List; @Service public interface DepartmentService extends IService<Department> { List<Department> queryDepartmentList(DepartmentSelectVo departmentSelectVo); int getLastCount(); void departmentDelete(Department department); Department departmentFind(DepartmentUpdateVo departmentUpdateVo); void departmentUpdate(DepartmentUpdateVo departmentUpdateVo); void departmentAdd(DepartmentUpdateVo departmentUpdateVo); }
最后便是controller类将后端数据传入前端
package com.example.system03.controller; import com.example.system03.entity.Department; import com.example.system03.entity.Vo.DepartmentSelectVo; import com.example.system03.entity.Vo.DepartmentUpdateVo; import com.example.system03.service.DepartmentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.List; @RestController @RequestMapping("/data/departmentManagement/") public class DepartmentController { @Autowired DepartmentService departmentService; @PostMapping("/departmentList") public Object queryRoleList( @RequestBody DepartmentSelectVo departmentSelectVo ) { HashMap<String, Object> res = new HashMap<>(); List<Department> departmentList = departmentService.queryDepartmentList(departmentSelectVo); int departmentListCount = departmentService.getLastCount(); res.put("departmentList", departmentList); res.put("total", departmentListCount); MyUtils.success(res); return res; } @PostMapping("/departmentDelete") public Object departmentDelete( @RequestBody List<Department> departmentList ) { HashMap<String, Object> res = new HashMap<>(); for (Department department : departmentList) { departmentService.departmentDelete(department); } MyUtils.success(res); return res; } @PostMapping("/departmentUpdate") public Object departmentUpdate( @RequestBody DepartmentUpdateVo departmentUpdateVo ) { HashMap<String, Object> res = new HashMap<>(); Department department = departmentService.departmentFind(departmentUpdateVo); if (departmentUpdateVo.getRequest().equals("update")) { if (department == null) { MyUtils.fail(res, "该部门不存在,无法更新"); } else { departmentService.departmentUpdate(departmentUpdateVo); MyUtils.success(res); } } else if (departmentUpdateVo.getRequest().equals("insert")) { if (department != null) { MyUtils.fail(res, "该部门已经存在,请勿重复创建"); } else { departmentService.departmentAdd(departmentUpdateVo); MyUtils.success(res); } } return res; } }