Java后端tlias员工部门管理-部门管理-2

目录

先建立数据库

查找所有部门

根据id查询部门

新增部门 

删除部门

修改部门


环境搭建准备具体请看上一个小节:http://t.csdnimg.cn/5TQxE

那么经过上一个小节的环境搭建准备工作完成之后,我们今天来完成部门管理这个模块。

首先来说明一下,Javaweb后端是三层架构的,分为controller、service、mapper层 ,

  1. controller层是直接对前端工程或者是操作浏览器的
  2. service是业务逻辑层,
  3. mapper是直接操作数据库的

整个的大致流程是mapper拿到从数据库读取的数据,交还给service,service对其数据进行业务逻辑处理之后,交给controller,controller拿到之后,把响应结果返回给前端

分开是为了以后方便管理,分层解耦 

接下来正式进入编写代码

先建立数据库

先在datagrip建立tlias数据库,如下图,右键@localhost,然后选择schema

输入tlias,就可以OK了,当然这个可以改成你们自己的名字

然后编写sql语句去创建表,这里我直接给大家提供好

-- 部门管理
create table dept(
    id int unsigned primary key auto_increment comment '主键ID',
    name varchar(10) not null unique comment '部门名称',
    create_time datetime not null comment '创建时间',
    update_time datetime not null comment '修改时间'
) comment '部门表';

insert into dept (id, name, create_time, update_time) values(1,'学工部',now(),now()),(2,'教研部',now(),now()),(3,'咨询部',now(),now()), (4,'就业部',now(),now()),(5,'人事部',now(),now());



-- 员工管理(带约束)
create table emp (
  id int unsigned primary key auto_increment comment 'ID',
  username varchar(20) not null unique comment '用户名',
  password varchar(32) default '123456' comment '密码',
  name varchar(10) not null comment '姓名',
  gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',
  image varchar(300) comment '图像',
  job tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师',
  entrydate date comment '入职时间',
  dept_id int unsigned comment '部门ID',
  create_time datetime not null comment '创建时间',
  update_time datetime not null comment '修改时间'
) comment '员工表';

INSERT INTO emp
	(id, username, password, name, gender, image, job, entrydate,dept_id, create_time, update_time) VALUES
	(1,'jinyong','123456','金庸',1,'1.jpg',4,'2000-01-01',2,now(),now()),
	(2,'zhangwuji','123456','张无忌',1,'2.jpg',2,'2015-01-01',2,now(),now()),
	(3,'yangxiao','123456','杨逍',1,'3.jpg',2,'2008-05-01',2,now(),now()),
	(4,'weiyixiao','123456','韦一笑',1,'4.jpg',2,'2007-01-01',2,now(),now()),
	(5,'changyuchun','123456','常遇春',1,'5.jpg',2,'2012-12-05',2,now(),now()),
	(6,'xiaozhao','123456','小昭',2,'6.jpg',3,'2013-09-05',1,now(),now()),
	(7,'jixiaofu','123456','纪晓芙',2,'7.jpg',1,'2005-08-01',1,now(),now()),
	(8,'zhouzhiruo','123456','周芷若',2,'8.jpg',1,'2014-11-09',1,now(),now()),
	(9,'dingminjun','123456','丁敏君',2,'9.jpg',1,'2011-03-11',1,now(),now()),
	(10,'zhaomin','123456','赵敏',2,'10.jpg',1,'2013-09-05',1,now(),now()),
	(11,'luzhangke','123456','鹿杖客',1,'11.jpg',5,'2007-02-01',3,now(),now()),
	(12,'hebiweng','123456','鹤笔翁',1,'12.jpg',5,'2008-08-18',3,now(),now()),
	(13,'fangdongbai','123456','方东白',1,'13.jpg',5,'2012-11-01',3,now(),now()),
	(14,'zhangsanfeng','123456','张三丰',1,'14.jpg',2,'2002-08-01',2,now(),now()),
	(15,'yulianzhou','123456','俞莲舟',1,'15.jpg',2,'2011-05-01',2,now(),now()),
	(16,'songyuanqiao','123456','宋远桥',1,'16.jpg',2,'2007-01-01',2,now(),now()),
	(17,'chenyouliang','123456','陈友谅',1,'17.jpg',NULL,'2015-03-21',NULL,now(),now());



查找所有部门

1.首先在mapper层进行数据库操作,拿到数据。

在mapper层编写list方法,返回值是部门列表,所以是List<Dept>,简单SQL语句通过加注解@Select直接编写SQL查询语句

DeptMapper.interface

/*
 * 查询全部部门数据
* */
@Select("SELECT * FROM dept")
public List<Dept> list();

2.然后在service层编写接口,并在对应的实现类编写业务逻辑的具体操作

DeptService.interface

/*
* 查询全部部门数据
* */
public List<Dept> list();

3.这个时候我们可以看到,service层的实现类出现了下划线,这是要你对接口的方法进行重写。

我们直接按alt+回车键,选择第一个

点击OK 

效果就出来了

那么他要拿到mapper 的数据,是不是要引入mapper的对象,所以可以通过加入@Autowired注解(表示自动注入)来引入mapper的DeptMapper 。

这时候我来讲一下Java的IOC容器,当你在该类加上@Component这个注解之后,他会把该类的对象交给IOC容器来管理,想要用到容器里的bean对象,通过@Autowired这个自动引入就可以拿到。

也许有人会问,我怎么只看到了@RestController@Service@Mapper这三个注解,这是因为这三个注解已经包括了@Component的功能。

所以,DeptServiceImpl.class的代码如下,

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.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
        List<Dept> deptList = deptMapper.list();
        return deptList;
    }
}

 4.这时候编写Controller层的代码

还是一样,要拿到Service层的对象需要通过@Autowired来引入

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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }
}

说明一下:

@Slf4j是用来调用日志函数的,加了这个注解,可以编写log.info("")这个方法,当程序运行的时候可以在控制台输出日志格式的内容。

@RequestMappingurl的公共路径

@GetMapping说明这是一个get请求

返回结果为什么是Result类型的,我们已经编写好了Result类,就是为了统一响应结果返回给前端。如果响应结果不统一,前端人员编写麻烦,日后也不方便维护。

6.接下来我们可以通过postman来测试。

  1. 点击+号来添加
  2. 选择GET请求
  3. 输入url路径,springboot默认的端口号是8080,所以是localhost:8080
  4. 点击那里,改名字,然后保存 

7.接下来我们来启动我们的启动类来测试一下

注意:一定要有@SpringBootApplication注解,他表示这个类是springboot的启动类

启动之后他报错了,后来才想起来Mapper层那边没连接数据库

所以, 点击右侧的数据库按钮,然后点击加号选择mysql,配置你的数据库连接

 Test Connection正常之后,Apply应用,这样,mapper那边就不会爆红了

再次启动整个项目,点击postman的send发送

可以看到正常响应了数据,同时Java的控制台也输出了响应回来的信息

 至此,查询全部部门的数据完成了。 

根据id查询部门

接下来的查询就很类似了,基本上大差不差,就sql语句不一样罢了。

DeptMapper

/*
* 根据id查询部门
 * */
@Select("select * from dept where id = #{id}")
Dept selectById(Integer id);

DeptService

/*
    * 根据id查询部门
    * */
Dept getById(Integer id);

DeptServiceImpl

 @Override
 public Dept getById(Integer id) {
      return deptMapper.selectById(id);
}

DeptController

@GetMapping("/{id}")
public Result GetDeptById(@PathVariable Integer id){
     log.info("根据id查询部门,id={}",id);
     Dept dept = deptService.getById(id);
     return Result.success(dept);
}

查询路径如果要传入参数用括号{}括起来,然后编写的方法想要接收到这个参数,需要加上

@PathVariable这个注解

新增部门 

DeptMapper

/*
* 新增部门
* */
@Insert("INSERT INTO dept(NAME, CREATE_TIME, UPDATE_TIME) VALUES (#{name},#{createTime},#{updateTime})")
void insert(Dept dept);

DeptService

/*
* 新增部门
* */
void insert(Dept dept);

DeptServiceImpl

@Override
public void insert(Dept dept) {
    dept.setCreateTime(LocalDateTime.now());
    dept.setUpdateTime(LocalDateTime.now());
    deptMapper.insert(dept);
}

DeptController

添加部门是post请求哦

@PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("新增部门数据:{}",dept);
        deptService.insert(dept);
        return Result.success();
    }

删除部门

删除部门就跟根据id查询差不多

DeptMapper

/*
    * 根据id删除部门
    * */
@Delete("delete from dept where id = #{id}")
void deleteById(Integer id);

DeptService 

/*
* 根据id删除部门
* */
void delete(Integer id);

DeptServiceImpl

@Override
public void delete(Integer id) {
      deptMapper.deleteById(id);//根据部门id删除部门
}

DeptController

@DeleteMapping("/{id}")
public Result DeleteDeptById(@PathVariable Integer id){
    log.info("根据部门id删除部门,id={}",id);
    deptService.delete(id);
    return Result.success();
}

修改部门

DeptMapper

/*
* 修改部门
* */
@Update("update dept set name = #{name},update_time = #{updateTime} where id = #{id}")
void update(Dept dept);

DeptService 

/*
* 修改部门
* */
@Update("update dept set name = #{name},update_time = #{updateTime} where id = #{id}")
void update(Dept dept);

DeptServiceImpl

@Override
public void update(Dept dept) {
    dept.setUpdateTime(LocalDateTime.now());
    deptMapper.update(dept);
}

DeptController

注意修改的请求是PUT噢!

@PutMapping
public Result UpdateDept(@RequestBody Dept dept){
    log.info("修改部门dept={}",dept);
    deptService.update(dept);
    return Result.success();
}

接下来用postman测试

1. 根据id查询部门

2.删除部门

注意啦:一定要看你的数据库表的id是多少!! 

 比如我要删掉胡根神部,就需要传递id=20。尽管表总共就8的数据,就是要看id是多少!

那么测试一下,我的响应是成功的

3.新增部门

对于POST的请求,你需要点击Body,然后点击raw,然后选择JSON,最后编写{"name":"xxx部"}

最终点击send测试一下,没有问题

4.修改部门

修改是PUT请求噢,然后还是和新增的一样,需要传递JSON格式的参数,也是需要在Body请求体去编写json

如图可知,返回的结果也是success,没有问题。


那么至此,部门管理这个模块基本上是完成了。但只是基本完成,后面要结合其他表再改进改进,还有AOP事务管理,全局异常处理器等等,都是要加上去的。 之后的正式编写员工管理我会过几天更新,期待大家的关注和支持!关注越多,点赞越多,评论多多,更新越快噢!

  • 47
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值