前言
大家好 我是歌谣 今天继续带来的是后端java的一个学习记录 首先 我们看一下整体的目录结构
后端目录结构
先写controller层
DeptControll
``` package com.itheima.controller; import com.itheima.pojo.Dept; import com.itheima.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 @RequestMapping("/depts") @RestController public class DeptController { @Autowired private DeptService deptService; //查询部门数据 @GetMapping public Result list(){ //查询全部部门数据 List deptList =deptService.list(); return Result.success(deptList); } //删除接口数据 @DeleteMapping("/{id}") public Result delete(@PathVariable Integer id){ //删除部门数据 log.info("根据id删除部门",id); deptService.delete(id); return Result.success(); } @PostMapping public Result add(@RequestBody Dept dept){ log.info("根据新增部门"); deptService.add(dept); return Result.success(); } } ```
deptSevices
``` package com.itheima.service;
import com.itheima.mapper.DeptMapper; import com.itheima.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/** * 部门管理 */ public interface DeptService {
List<Dept> list();
void delete(Integer id);
void add(Dept dept);
}
```
deptServiceImpl ``` package com.itheima.service.impl; import com.itheima.mapper.DeptMapper; import com.itheima.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 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); } }
```
DeptMapper ``` package com.itheima.mapper; import com.itheima.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 { @Select("select * from dept") List list(); @Delete("delete from dept where id=#{id}") void deleteById(Integer id); @Insert("insert into dept (name,create time,updatetime) values(#{name},#{createTime},#{updateTime}) ") void insert(Dept dept); } ```
前端启动
总结
我是歌谣 微信公众号关注前端小歌谣带你进入前端巅峰交流群