三层架构
controller:接受请求处理响应
package com.ittest.controller;
import com.ittest.pojo.Dept;
import com.ittest.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 部门管理Controller
*/
@Slf4j//注解,加上后可以直接调用log.info
@RequestMapping("/depts")
@RestController
@ResponseBody
public class DeptController {
//private static Logger log = LoggerFactory.getLogger(DeptController.class);
@Autowired
private DeptService deptService;
/**
* 查询部门数据
* @return
*/
//@RequestMapping(value = "/depts",method = RequestMethod.GET) //指定请求方式为GET
@GetMapping
public Result list(){
log.info("查询全部部门数据");
//调用service查询部门数据
List<Dept> deptList = deptService.list();
return Result.success(deptList);
}
/**
* 删除部门
* @return
*/
@DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id){
log.info("根据id删除部门:{}",id);
//调用service删除部门
deptService.delete(id);
return Result.success();
}
/**
* 新增部门
* @return
*/
@PostMapping
public Result add(@RequestBody Dept dept){
log.info("新增部门: {}" , dept);
//调用service新增部门
deptService.add(dept);
return Result.success();
}
}
package com.ittest.controller;
import com.ittest.pojo.PageBean;
import com.ittest.pojo.Result;
import com.ittest.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
*/
@Slf4j
@RestController
@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("分页查询, 参数: {},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
//调用service分页查询
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("批量删除操作, ids:{}",ids);
empService.delete(ids);
return Result.success();
}
}
service:逻辑处理,接口
package com.ittest.service.impl;
import com.ittest.mapper.DeptMapper;
import com.ittest.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public List<Dept> list() {
return deptMapper.list();
}
@Override
public void delete(Integer id) {
deptMapper.deleteById(id);
}
@Override
public void add(Dept dept) {
dept.setCreateTime(LocalDateTime.now());
dept.setUpdateTime(LocalDateTime.now());
deptMapper.insert(dept);
}
}
package com.ittest.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ittest.mapper.EmpMapper;
import com.ittest.pojo.Emp;
import com.ittest.pojo.PageBean;
import com.ittest.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
/*@Override
public PageBean page(Integer page, Integer pageSize) {
//1. 获取总记录数
Long count = empMapper.count();
//2. 获取分页查询结果列表
Integer start = (page - 1) * pageSize;
List<Emp> empList = empMapper.page(start, pageSize);
//3. 封装PageBean对象
PageBean pageBean = new PageBean(count, empList);
return pageBean;
}*/
@Override
public PageBean page(Integer page, Integer pageSize,String name, Short gender,LocalDate begin,LocalDate end) {
//1. 设置分页参数
PageHelper.startPage(page,pageSize);
//2. 执行查询
List<Emp> empList = empMapper.list(name, gender, begin, end);
Page<Emp> p = (Page<Emp>) empList;
//3. 封装PageBean对象
PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
return pageBean;
}
@Override
public void delete(List<Integer> ids) {
empMapper.delete(ids);
}
}
接口:
package com.ittest.service;
import com.ittest.pojo.Dept;
import java.util.List;
/**
* 部门管理
*/
public interface DeptService {
/**
* 查询全部部门数据
* @return
*/
List<Dept> list();
/**
* 删除部门
* @param id
*/
void delete(Integer id);
/**
* 新增部门
* @param dept
*/
void add(Dept dept);
}
package com.ittest.service;
import com.ittest.pojo.PageBean;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDate;
import java.util.List;
/**
* 员工管理
*/
public interface EmpService {
/**
* 分页查询
* @param page
* @param pageSize
* @return
*/
PageBean page(Integer page, Integer pageSize,String name, Short gender,LocalDate begin,LocalDate end);
/**
* 批量删除
* @param ids
*/
void delete(List<Integer> ids);
}
mapper:数据访问
package com.ittest.mapper;
import com.ittest.pojo.Dept;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 部门管理
*/
@Mapper
public interface DeptMapper {
/**
* 查询全部部门
* @return
*/
@Select("select * from dept")
List<Dept> list();
/**
* 根据ID删除部门
* @param id
*/
@Delete("delete from dept where id = #{id}")
void deleteById(Integer id);
/**
* 新增部门
* @param dept
*/
@Insert("insert into dept(name, create_time, update_time) values(#{name},#{createTime},#{updateTime})")
void insert(Dept dept);
}
package com.ittest.mapper;
import com.ittest.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List;
/**
* 员工管理
*/
@Mapper
public interface EmpMapper {
/**
* 查询总记录数
* @return
*/
//@Select("select count(*) from emp")
//public Long count();
/**
* 分页查询,获取列表数据
* @param start
* @param pageSize
* @return
*/
//@Select("select * from emp limit #{start},#{pageSize}")
//public List<Emp> page(Integer start, Integer pageSize);
/**
* 员工信息查询
* @return
*/
//@Select("select * from emp")
public List<Emp> list(String name, Short gender,LocalDate begin,LocalDate end);
/**
* 批量删除
* @param ids
*/
void delete(List<Integer> ids);
}