前言
我是歌谣 今天继续带来前后端项目的开发 上次已经开发了部门管理,今天继续开发 员工管理
后端第一步empcontroller代码
package com.itheima.controller;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.List;
/**
* 员工管理Controller
*/
@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpController {
@Autowired
private EmpService empService;
@GetMapping
public Result page(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer pageSize,
String name, Short gender, @DateTimeFormat(pattern = "yyyy-mm-dd")LocalDate begin,
@DateTimeFormat(pattern = "yyyy-mm-dd")LocalDate end){
log.info("查询分页");
PageBean pageBean=empService.page(page,pageSize,name,gender,begin,end);
return Result.success(pageBean);
}
@DeleteMapping("/{ids}")
public Result delete(@PathVariable List<Integer> ids){
log.info("删除");
empService.delete(ids);
return Result.success();
}
@PostMapping
public Result save(@RequestBody Emp emp){
log.info("新增员工");
empService.save(emp);
return Result.success();
}
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id){
log.info("查询员工");
Emp emp=empService.getById(id);
return Result.success(emp);
}
@PutMapping
public Result update(@RequestBody Emp emp){
log.info("修改员工");
empService.update(emp);
return Result.success();
}
}
后端第二步 empservice层
package com.itheima.service;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.List;
public interface EmpService {
PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin,LocalDate end);
void delete(List<Integer> ids);
void save(Emp emp);
Emp getById(Integer id);
void update(Emp emp);
Emp login(Emp emp);
}
后端第三步empserviceImpl类
package com.itheima.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
//@Service
//public class EmpServiceImpl implements EmpService {
// @Autowired
// private EmpMapper empMapper;
// @Override
// public PageBean page(Integer page, Integer pageSize) {
// Long count= empMapper.count();
// Integer start=(page-1)*pageSize;
// empMapper.page(start,pageSize);
// List<Emp> empList=empMapper.page(start,pageSize);
// PageBean pageBean=new PageBean(count,empList);
// return pageBean;
// }
//}
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
PageHelper.startPage(page,pageSize);
List<Emp> empList = empMapper.list(name,gender,begin,end);
Page<Emp> p= (Page<Emp>) empList;
PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
return pageBean;
}
@Override
public void delete(List<Integer> ids){
empMapper.delete(ids);
}
@Override
public void save(Emp emp) {
emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
empMapper.insert(emp);
}
@Override
public Emp getById(Integer id) {
return empMapper.getById(id);
}
@Override
public void update(Emp emp) {
emp.setUpdateTime(LocalDateTime.now());
empMapper.update(emp);
}
@Override
public Emp login(Emp emp) {
return empMapper.getByUsernameAndPassword(emp);
}
}
后端第四步empmapper
package com.itheima.mapper;
import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List;
/**
* 员工管理
*/
//@Mapper
//public interface EmpMapper {
// @Select("select count(*) from emp")
// public Long count();
//@Select("select * from emp limit #{start},#{pageSize}")
// public List<Emp> page(Integer start, Integer pageSize);
//}
@Mapper
public interface EmpMapper {
// @Select("select * from emp")
public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
void delete(List<Integer> ids);
@Insert("insert into emp(username,name,gender,image,job,entrydate,dept_id,create_time,update_time)"+
"values(#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")
void insert(Emp emp);
@Select("select * from emp where id=#{id}")
Emp getById(Integer id);
void update(Emp emp);
@Select("select * from emp where username=#{username} and password=#{password}"
)
Emp getByUsernameAndPassword(Emp emp);
}
实现结果
查询
新增
删除
修改
总结
标签:yyds,vue,springboot,page,emp,import,com,public,Emp From: https://blog.51cto.com/u_14476028/6183789我是歌谣 放弃很容易 但是坚持一定很酷 一名研究java和前端的程序员 想加入前端巅峰人才交流群联系我