admin 管理员组

文章数量: 887021

saas

部门管理

数据库表设计

CREATE TABLE `co_department` (
`id` varchar(40) NOT NULL,
`company_id` varchar(255) NOT NULL COMMENT '企业ID',
`parent_id` varchar(255) DEFAULT NULL COMMENT '父级部门ID',
`name` varchar(255) NOT NULL COMMENT '部门名称',
`code` varchar(255) NOT NULL COMMENT '部门编码',
`category` varchar(255) DEFAULT NULL COMMENT '部门类别',
`manager_id` varchar(255) DEFAULT NULL COMMENT '负责人ID',
`city` varchar(255) DEFAULT NULL COMMENT '城市',
`introduce` text COMMENT '介绍',
`create_time` datetime NOT NULL COMMENT '创建时间',
`manager` varchar(40) DEFAULT NULL COMMENT '部门负责人',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

抽取公共代码

package com.ihrmmon.controller;import org.springframework.web.bind.annotation.ModelAttribute;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class BaseController {protected HttpServletRequest request;protected HttpServletResponse response;protected String companyId;protected String companyName;@ModelAttributepublic void setResAnReq(HttpServletRequest request,HttpServletResponse response) {this.request = request;this.response = response;/*** 目前使用 companyId = 1*         companyName = "传智播客"*/thispanyId = "1";thispanyName = "传智播客";}}
package com.ihrmmon.service;import org.springframework.data.jpa.domain.Specification;import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;public class BaseService<T> {protected Specification<T> getSpec(String companyId) {Specification<T> spect = new Specification() {@Overridepublic Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder cb) {//根据企业id查询return cb.equal(root.get("companyId").as(String.class),companyId);}};return spect;}
}

实现基本CRUD操作

package com.ihrm.domainpany;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;
import java.util.Date;
import java.util.List;/*** (Department)实体类*/
@Entity
@Table(name = "co_department")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department implements Serializable {private static final long serialVersionUID = -9084332495284489553L;//ID@Idprivate String id;/*** 父级ID*/private String pid;/*** 企业ID*/private String companyId;/*** 部门名称*/private String name;/*** 部门编码,同级部门不可重复*/private String code;/*** 负责人ID*/private String managerId;/***  负责人名称*/private String manager;/*** 介绍*/private String introduce;/*** 创建时间*/private Date createTime;
}
package com.ihrmpany.dao;import com.ihrm.domainpany.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;/*** 部门dao接口*/
public interface DepartmentDao extends JpaRepository<Department,String> ,JpaSpecificationExecutor<Department> {
}
package com.ihrmpany.service;import com.ihrmmon.service.BaseService;
import com.ihrmmon.utils.IdWorker;
import com.ihrmpany.dao.DepartmentDao;
import com.ihrm.domainpany.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;@Service
public class DepartmentService extends BaseService {@Autowiredprivate DepartmentDao departmentDao;@Autowiredprivate IdWorker idWorker;/*** 1.保存部门*/public void save(Department department) {//设置主键的值String id = idWorker.nextId()+"";department.setId(id);//调用dao保存部门departmentDao.save(department);}/*** 2.更新部门*/public void update(Department department) {//1.根据id查询部门Department dept = departmentDao.findById(department.getId()).get();//2.设置部门属性dept.setCode(department.getCode());dept.setIntroduce(department.getIntroduce());dept.setName(department.getName());//3.更新部门departmentDao.save(dept);}/*** 3.根据id查询部门*/public Department findById(String id) 

本文标签: SaaS