赞
踩
项目结构:
- server.port=8080
-
- # jsp view
- spring.mvc.view.prefix=/views/jsp/
- spring.mvc.view.suffix=.jsp
-
- #UTF-8encoding
- spring.http.encoding.force=true
- spring.http.encoding.charset=UTF-8
- spring.http.encoding.enabled=true
- server.tomcat.uri-encoding=UTF-8
-
- #mysql datasource
- spring.datasource.driverClassName = com.mysql.jdbc.Driver
- spring.datasource.url = jdbc:mysql://localhost:3306/hrs?useUnicode=true&characterEncoding=UTF-8&useSSL=false
- spring.datasource.username = root
- spring.datasource.password = root
-
- #spring data jpa & mysql
- spring.jpa.database = MYSQL
- spring.jpa.show-sql = true
- spring.jpa.hibernate.ddl-auto = update
- spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
- spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
pom.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.1.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.hrs</groupId>
- <artifactId>hrs</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>hrs</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- <mysql-connector-version>5.1.47</mysql-connector-version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>${mysql-connector-version}</version>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- <scope>provided</scope>
- </dependency>
-
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
dao
- package com.hrs.youzhenjie.employee.dao;
-
- import java.util.List;
-
- import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
- import org.springframework.data.repository.PagingAndSortingRepository;
- import org.springframework.stereotype.Repository;
-
- import com.hrs.youzhenjie.employee.domain.Employee;
-
- @Repository
- public interface IEmployeeDao extends PagingAndSortingRepository<Employee, Long>, JpaSpecificationExecutor<Employee> {
-
- //JPA 高级查询
- public Employee findByEmployeeId(String name);
-
- public Employee findByEmployeeName(String employeeName);
- }
domain
- package com.hrs.youzhenjie.employee.domain;
-
- import java.util.Date;
-
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.OneToOne;
- import javax.persistence.Table;
- import javax.persistence.Temporal;
- import javax.persistence.TemporalType;
- import javax.validation.constraints.NotNull;
-
- import org.springframework.format.annotation.DateTimeFormat;
- import org.springframework.lang.NonNull;
-
- import com.fasterxml.jackson.annotation.JsonFormat;
- import com.hrs.common.BaseDomain;
- import com.hrs.youzhenjie.contract.domain.Contract;
-
- import jdk.nashorn.internal.ir.annotations.Reference;
-
- @Entity
- @Table(name = "t_employee")
- public class Employee extends BaseDomain<Long> {
-
- private String employeeId;// 工号
- private String employeeName;// 姓名
- private String employeeSex;// 性别
- private String employeeDepartment;// 部门
- private String employeePosition;// 职位
- private String employeeTel;// 电话
- private String employeeState;// 在职状态
- private Date employeeEntryTime;// 入职日期
- private Date employeeLeaveTime;// 离职日期
-
- //private Contract contract;
-
- // @OneToOne(cascade=CascadeType.ALL)
- //@JoinColumn(name="referenceId")
- //
- //@OneToOne(fetch=FetchType.EAGER,mappedBy="employee")
- // public Contract getContract() {
- // return contract;
- // }
-
- @Column(unique = true, nullable = false)
- @NotNull
- public String getEmployeeId() {
- return employeeId;
- }
-
- public String getEmployeeName() {
- return employeeName;
- }
-
- public String getEmployeeSex() {
- return employeeSex;
- }
-
- public String getEmployeeDepartment() {
- return employeeDepartment;
- }
-
- public String getEmployeePosition() {
- return employeePosition;
- }
-
- public String getEmployeeTel() {
- return employeeTel;
- }
-
- @Column(length = 1)
- public String getEmployeeState() {
- return employeeState;
- }
-
- @Temporal(TemporalType.DATE)
- // @DateTimeFormat(pattern="yyyy/MM/dd")
- @JsonFormat(pattern = "yyyy/MM/dd", timezone = "GMT+8")
- public Date getEmployeeEntryTime() {
- return employeeEntryTime;
- }
-
- @Temporal(TemporalType.DATE)
- // @DateTimeFormat(pattern="yyyy/MM/dd")
- @JsonFormat(pattern = "yyyy/MM/dd", timezone = "GMT+8")
- public Date getEmployeeLeaveTime() {
- return employeeLeaveTime;
- }
-
- public void setEmployeeId(String employeeId) {
- this.employeeId = employeeId;
- }
-
- public void setEmployeeName(String employeeName) {
- this.employeeName = employeeName;
- }
-
- public void setEmployeeSex(String employeeSex) {
- this.employeeSex = employeeSex;
- }
-
- public void setEmployeeDepartment(String employeeDepartment) {
- this.employeeDepartment = employeeDepartment;
- }
-
- public void setEmployeePosition(String employeePosition) {
- this.employeePosition = employeePosition;
- }
-
- public void setEmployeeTel(String employeeTel) {
- this.employeeTel = employeeTel;
- }
-
- public void setEmployeeState(String employeeState) {
- this.employeeState = employeeState;
- }
-
- public void setEmployeeEntryTime(Date employeeEntryTime) {
- this.employeeEntryTime = employeeEntryTime;
- }
-
- public void setEmployeeLeaveTime(Date employeeLeaveTime) {
- this.employeeLeaveTime = employeeLeaveTime;
- }
-
- // public void setContract(Contract contract) {
- // this.contract = contract;
- // }
-
- }
Dto
- package com.hrs.youzhenjie.employee.domain;
-
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
-
- import javax.persistence.criteria.CriteriaBuilder;
- import javax.persistence.criteria.CriteriaQuery;
- import javax.persistence.criteria.Predicate;
- import javax.persistence.criteria.Root;
-
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.data.jpa.domain.Specification;
- import org.springframework.format.annotation.DateTimeFormat;
-
- import com.fasterxml.jackson.annotation.JsonFormat;
-
- public class EmployeeQueryDTO {
- // 查询的参数
- private String employeeId;// 工号
-
- @JsonFormat(pattern = "yyyy/MM/dd")
- @DateTimeFormat(pattern="yyyy/MM/dd")
- private Date employeeEntryTime;// 入职日期
-
- @JsonFormat(pattern = "yyyy/MM/dd")
- @DateTimeFormat(pattern="yyyy/MM/dd")
- private Date employeeLeaveTime;// 离职日期
-
- private String employeeDepartment;// 部门
- private String employeePosition;// 职位
-
- @SuppressWarnings({ "serial" })
- public static Specification<Employee> getWhereClause(final EmployeeQueryDTO employeeQueryDTO) {
- return new Specification<Employee>() {
-
- @Override
- public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
- List<Predicate> predicate = new ArrayList<>();
-
- if (StringUtils.isNotBlank(employeeQueryDTO.getEmployeeId())) {
- predicate.add(criteriaBuilder.like(root.get("employeeId").as(String.class),
- "%" + employeeQueryDTO.getEmployeeId() + "%"));
- }
- if (null != employeeQueryDTO.getEmployeeEntryTime()) {
- predicate.add(criteriaBuilder.greaterThanOrEqualTo(root.get("employeeEntryTime").as(Date.class),
- employeeQueryDTO.getEmployeeEntryTime()));
- }
- if (null != employeeQueryDTO.getEmployeeLeaveTime()) {
- predicate.add(criteriaBuilder.lessThanOrEqualTo(root.get("employeeLeaveTime").as(Date.class),
- employeeQueryDTO.getEmployeeLeaveTime()));
- }
- if (null != employeeQueryDTO.getEmployeeDepartment()) {
- predicate.add(criteriaBuilder.like(root.get("employeeDepartment").as(String.class),
- "%" + employeeQueryDTO.getEmployeeDepartment() + "%"));
- }
- if (null != employeeQueryDTO.getEmployeePosition()) {
- predicate.add(criteriaBuilder.like(root.get("employeePosition").as(String.class),
- "%" + employeeQueryDTO.getEmployeePosition() + "%"));
- }
- Predicate[] pre = new Predicate[predicate.size()];
- return query.where(predicate.toArray(pre)).getRestriction();
- }
- };
-
- }
-
- public String getEmployeeId() {
- return employeeId;
- }
-
- public void setEmployeeId(String employeeId) {
- this.employeeId = employeeId;
- }
-
- public Date getEmployeeEntryTime() {
- return employeeEntryTime;
- }
-
- public void setEmployeeEntryTime(Date employeeEntryTime) {
- this.employeeEntryTime = employeeEntryTime;
- }
-
- public Date getEmployeeLeaveTime() {
- return employeeLeaveTime;
- }
-
- public void setEmployeeLeaveTime(Date employeeLeaveTime) {
- this.employeeLeaveTime = employeeLeaveTime;
- }
-
- public String getEmployeeDepartment() {
- return employeeDepartment;
- }
-
- public void setEmployeeDepartment(String employeeDepartment) {
- this.employeeDepartment = employeeDepartment;
- }
-
- public String getEmployeePosition() {
- return employeePosition;
- }
-
- public void setEmployeePosition(String employeePosition) {
- this.employeePosition = employeePosition;
- }
-
- }
service
- package com.hrs.youzhenjie.employee.service;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Optional;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.Pageable;
- import org.springframework.data.jpa.domain.Specification;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
-
- import com.hrs.youzhenjie.employee.dao.IEmployeeDao;
- import com.hrs.youzhenjie.employee.domain.Employee;
-
- @Service
- @Transactional
- public class EmployeeService implements IEmployeeService {
-
- @Autowired
- private IEmployeeDao employeeDao;
-
- @Override
- public Employee saveOrUpdate(Employee employee) {
- System.out.println(employee.getEmployeeId());
- return employeeDao.save(employee);
- }
-
- @Override
- public Optional<Employee> searchById(Long id) {
- // TODO Auto-generated method stub
- return employeeDao.findById(id);
- }
-
- @Override
- @Transactional(readOnly = true)
- public List<Employee> searchAll() {
- // TODO Auto-generated method stub
- return (List<Employee>) employeeDao.findAll();
- }
-
- @Override
- public Page<Employee> searchAllByPage(Specification<Employee> sepc, Pageable pageable) {
- // TODO Auto-generated method stub
- return employeeDao.findAll(sepc, pageable);
- }
-
-
-
- @Override
- public Employee findByEmployeeId(String empid) {
- // TODO Auto-generated method stub
- return employeeDao.findByEmployeeId(empid);
- }
-
- @Override
-
- public void deleteById(Long id) {
- // TODO Auto-generated method stub
- if (employeeDao.findById(id) != null) {
- employeeDao.deleteById(id);
- }
- }
-
- @Override
- @Transactional(readOnly = true)
- public long count() {
- // TODO Auto-generated method stub
- return employeeDao.count();
- }
-
- @Override
- @Transactional(readOnly = true)
- public boolean existsById(Long id) {
- // TODO Auto-generated method stub
- return employeeDao.existsById(id);
- }
-
- @Override
- public void deleteAll(Long[] ids) {
- List<Long> empIdLists = new ArrayList<Long>();
- for (int i = 0; i < ids.length; i++) {
- empIdLists.add(ids[i]);
- }
-
- List<Employee> employees = (List<Employee>) employeeDao.findAllById(empIdLists);
- if (employees != null) {
- employeeDao.deleteAll(employees);
- }
- }
-
- @Override
- public Employee findByEmployeeName(String employeeName) {
- return employeeDao.findByEmployeeName(employeeName);
- }
-
-
- }
serviceimpl
- package com.hrs.youzhenjie.employee.service;
-
- import java.util.List;
- import java.util.Optional;
-
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.Pageable;
- import org.springframework.data.jpa.domain.Specification;
-
- import com.hrs.youzhenjie.employee.domain.Employee;
-
- public interface IEmployeeService {
- /*
- * 查
- */
- //通过ID查询
- public Optional<Employee> searchById(Long id);
-
- //查询所有(没有分页)
- public List<Employee> searchAll();
-
- //查询所有(分页)
- public Page<Employee> searchAllByPage(Specification<Employee> sepc,Pageable pageable);
-
- //通过名字查询
- public Employee findByEmployeeId(String name);
-
- //通过employeeName查询
- public Employee findByEmployeeName(String employeeName);
-
- /*
- * 增&&改&& 删
- * 软删除:
- * 删除员工相当于update员工的在职状态为0
- */
- public Employee saveOrUpdate(Employee employee);
-
-
- //预留删除接口:
- public void deleteById(Long id);
-
- public void deleteAll(Long[] ids);
-
-
- //统计
- public long count();
-
- //是否存在
- public boolean existsById(Long id);
-
- }
controller
- package com.hrs.youzhenjie.employee.web;
-
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.http.MediaType;
- import org.springframework.web.bind.annotation.DeleteMapping;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.PutMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
-
- import com.hrs.common.beans.BeanUtils;
- import com.hrs.common.beans.SessionUtil;
- import com.hrs.common.web.ExtAjaxResponse;
- import com.hrs.common.web.ExtjsPageRequest;
- import com.hrs.huangwenkang.entry.service.IEntryService;
- import com.hrs.youzhenjie.employee.domain.Employee;
- import com.hrs.youzhenjie.employee.domain.EmployeeQueryDTO;
- import com.hrs.youzhenjie.employee.service.IEmployeeService;
- import com.hrs.zhanshiyang.log.domain.Log;
- import com.hrs.zhanshiyang.log.domain.LogDTO;
- import com.hrs.zhanshiyang.log.service.ILogService;
- import com.hrs.zhanshiyang.record.domain.RecordDTO;
- import com.hrs.zhanshiyang.record.service.IRecordService;
-
- @RestController
- @RequestMapping(value = "employee")
- public class EmployeeController {
-
- @Autowired
- private IEmployeeService employeeService;
-
- @Autowired
- private ILogService logService;
-
- @Autowired
- private IEntryService entryService;
-
- @Autowired
- private IRecordService recordService;
- // 花名册的主页 分页+显示所有信息
- /*
- * 查询所有
- */
- @GetMapping
- public Page<Employee> findAll(EmployeeQueryDTO dto, ExtjsPageRequest pageRequest) {
- return employeeService.searchAllByPage(dto.getWhereClause(dto), pageRequest.getPageable());
-
- }
-
- /*
- * 查询一个
- */
- @GetMapping(value = "{id}")
- public Employee findOne(@PathVariable Long id) {
- System.out.println(id);
- return employeeService.searchById(id).get();
- }
-
- /*
- * 添加数据
- */
- @PostMapping("/save")
- public ExtAjaxResponse save(@RequestParam(value = "id") String id,
- @RequestParam(value = "resumeId") String resumeId,
- @RequestParam(value = "entryName") String entryName,
- @RequestParam(value = "entrySex") String entrySex,
- @RequestParam(value = "entrybranch") String entrybranch,
- @RequestParam(value = "entryposition") String entryposition,
- @RequestParam(value = "entryTel") String entryTel,
- @RequestParam(value = "entryConfirmedEntryDate") String entryConfirmedEntryDate) throws ParseException{
-
- //String类型转换为Long
- Long i=Long.valueOf(id);
-
-
- int resume=Integer.valueOf(resumeId);
- Employee domain=new Employee();
- Date date=new Date(entryConfirmedEntryDate);
- //String类型转换为Date
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
- Date dateSql = sdf.parse(entryConfirmedEntryDate);
-
- domain.setEmployeeName(entryName);//姓名
- domain.setEmployeeSex(entrySex);//性别
- domain.setEmployeeDepartment(entrybranch);//部门
- domain.setEmployeePosition(entryposition);//职位
- domain.setEmployeeTel(entryTel);//电话
- domain.setEmployeeState("1");//状态
- domain.setEmployeeEntryTime(dateSql);//入职时间
-
- //Log表
- LogDTO log=new LogDTO();
- String partId;
- if(resume<10) {
- //工号自定义
- partId=new SimpleDateFormat("yyMMdd").format(date)+"0"+resumeId;
- domain.setEmployeeId(partId);
- System.out.println("Loging start");
- log.setEmployeeId(partId);
- System.out.println("Loging end");
- }else {
- partId=new SimpleDateFormat("yyMMdd").format(date)+resumeId;
- domain.setEmployeeId(partId);
- log.setEmployeeId(partId);
- log.setEmployeeName(entryName);
- log.setLogPermission("9");
- }
- try {
- //写入employee表
- employeeService.saveOrUpdate(domain);
-
- //更新状态
- entryService.updateEntryRes(i);
-
- //写入log登录表
- logService.save(log);
-
- //日志
- writeLog("修改待入职表状态并添加");
-
- return new ExtAjaxResponse(true,"保存成功!");
- }catch(Exception e) {
- return new ExtAjaxResponse(true,"保存失败!");
- }
- }
-
- /*
- * 更新update
- */
- @PutMapping(value = "{id}")
- public ExtAjaxResponse update(@PathVariable("id") Long id, @RequestBody Employee employeedto) {
- System.out.println("update-------------------------" + id);
- try {
- Employee emp = employeeService.searchById(id).get();
- if (emp != null) {
- BeanUtils.copyProperties(employeedto, emp);// 使用自定义的BeanUtils
- employeeService.saveOrUpdate(emp);
-
- //日志
- writeLog("修改");
- }
- return new ExtAjaxResponse(true, "更新成功!");
- } catch (Exception e) {
- return new ExtAjaxResponse(false, "更新失败!");
- }
- }
-
- /*
- * 更新状态
- */
- /*
- * 删除 设置state为0
- */
- // 删除
- @DeleteMapping(value = "{id}")
- public ExtAjaxResponse delete(@PathVariable("id") Long id) {
- System.out.println("单个删除-- -------------------------------------------id:" + id);
- try {
- if (id != null) {
- employeeService.deleteById(id);
- //日志
- writeLog("删除");
- }
- return new ExtAjaxResponse(true, "删除成功!");
- } catch (Exception e) {
- return new ExtAjaxResponse(false, "删除失败!");
- }
- }
-
- @PostMapping("/deletes")
- public ExtAjaxResponse deleteRows(@RequestParam(name = "ids") Long[] ids) {
- System.out.println("批量删除-----------------");
- for (int i = 0; i < ids.length; i++) {
- System.out.println(ids[i]);
- writeLog("批量删除:删除");
- }
-
- try {
- if (ids != null) {
- employeeService.deleteAll(ids);
- }
- return new ExtAjaxResponse(true, "批量删除成功!");
- } catch (Exception e) {
- return new ExtAjaxResponse(false, "批量删除失败!");
- }
- }
-
- @RequestMapping(value = "/findByEmployeeId")
- public @ResponseBody ExtAjaxResponse findByEmployeeId(@RequestParam("employeeId") String employeeId) {
- System.out.println(employeeId);
- Employee emp=employeeService.findByEmployeeId(employeeId);
- if(emp!=null) {
- Map<String,String> map=new HashMap<String, String>();
- map.put("employeeName", emp.getEmployeeName());
- System.out.println("123"+emp.getEmployeeName());
-
- map.put("msg", "获取成功!");
- return new ExtAjaxResponse(true,map);
- } else {
- return new ExtAjaxResponse(false,"获取失败");
- }
- }
-
- //日志方法
- public void writeLog(String operation){
- HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
- HttpSession session=request.getSession();
- String employeeId=SessionUtil.getEmployeeId(session);
-
- RecordDTO recordDTO = new RecordDTO();
- recordDTO.setEmployeeId(employeeId);
- recordDTO.setRecordContent(operation+" 员工信息成功");
- recordDTO.setRecordTime(new Date());
- recordService.save(recordDTO);
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。