SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for emp -- ---------------------------- DROP TABLE IF EXISTS `emp`; CREATE TABLE `emp` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `password` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `age` int NULL DEFAULT NULL, `sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `birthday` date NULL DEFAULT NULL, `email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `phone` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `dept_id` int NULL DEFAULT NULL, `position_id` int NULL DEFAULT NULL, `create_id` int NULL DEFAULT NULL COMMENT '创建者id', `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of emp -- ---------------------------- INSERT INTO `emp` VALUES (1, 'zhangsanfeng', '123', 25, '男', '2000-01-14', 'zhangsan@126.com', '15151770550', 1, 1, 2, '2021-10-05 16:00:00'); INSERT INTO `emp` VALUES (2, 'lisi', '123', 24, '男', '2000-03-09', 'lisi@126.com', '15151770551', 2, 1, 2, '2021-10-20 16:00:00'); INSERT INTO `emp` VALUES (3, 'wangming', '123', 22, '男', '2000-02-23', 'wangming@163.com', '15151772552', 2, 2, 3, '2021-10-06 16:00:00'); INSERT INTO `emp` VALUES (4, 'dignqian', '123', 23, '女', '2000-12-06', 'dingxi@163.com', '15151772553', 3, 3, 3, '2021-10-09 11:47:08'); INSERT INTO `emp` VALUES (5, 'zhaodong', '123', 25, '男', '2000-08-09', 'zhaodong@126.com', '15151772554', 4, 4, 2, '2021-05-06 11:47:11'); INSERT INTO `emp` VALUES (6, 'liguang', '123', 24, '男', '2000-07-04', 'liguang@126.com', '15151772555', 5, 5, 3, '2021-10-15 16:00:00'); INSERT INTO `emp` VALUES (7, 'liuyan', '123', 24, '女', '2001-11-17', 'liuyan@126.com', '15151772556', 2, 5, 2, '2021-04-07 11:47:19'); INSERT INTO `emp` VALUES (8, 'wangxia', '123', 22, '女', '2000-10-26', 'wangxia@126.com', '15151772557', 3, 3, 3, '2022-02-10 16:00:00'); SET FOREIGN_KEY_CHECKS = 1;
idea项目结构:
controller层:
package com.czxy.controller;
import com.czxy.Service.EmpService;
import com.czxy.Vo.baseResult;
import com.czxy.domain.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/emp")
public class EmpController {
@Autowired
private EmpService empService;
@GetMapping
public baseResult selectAllEmp() {
List list = empService.selectAll();
if (list != null) {
return baseResult.ok("查询成功", list);
}
return baseResult.error("查询失败");
}
@GetMapping("/{id}")
public baseResult selectBYId(@PathVariable("id") String id) {
System.out.println(id);
Emp emp = empService.selectBYId(id);
if (emp != null) {
return baseResult.ok("查询成功", emp);
}
return baseResult.error("查询失败");
}
@PutMapping
public baseResult edit(@RequestBody Emp emp) {
System.out.println(emp);
Boolean flag = empService.edit(emp);
if (flag) {
return baseResult.ok("修改成功");
}
return baseResult.error("修改失败");
}
@PostMapping
public baseResult add(@RequestBody Emp emp) {
Boolean flag = empService.add(emp);
if (flag) {
return baseResult.ok("添加成功");
}
return baseResult.error("添加失败");
}
@DeleteMapping("/{id}")
public baseResult deleteEmp(@PathVariable String id) {
System.out.println(id);
Boolean flag = empService.deleteEmp(id);
if (flag) {
return baseResult.ok("删除成功");
}
return baseResult.error("删除失败");
}
}
dao:
package com.czxy.dao; import com.czxy.domain.Emp; import tk.mybatis.mapper.common.Mapper; @org.apache.ibatis.annotations.Mapper public interface EmpMapper extends Mapper{ }
domain:
package com.czxy.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Table(name = "emp")
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Emp {
// CREATE TABLE `emp` (
// `id` int(11) NOT NULL AUTO_INCREMENT,
// `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
// `password` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
// `age` int(11) NULL DEFAULT NULL,
// `sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
// `birthday` date NULL DEFAULT NULL,
// `email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
// `phone` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
// `dept_id` int(11) NULL DEFAULT NULL,
// `position_id` int(11) NULL DEFAULT NULL,
// `create_id` int(11) NULL DEFAULT NULL COMMENT '创建者id',
// `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
// PRIMARY KEY (`id`) USING BTREE
//) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;
@Id
private Integer id;
private String name;
private String password;
private Integer age;
private String sex;
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
private Date birthday;
private String email;
private String phone;
private Integer dept_id;
private Integer position_id;
private Integer create_id;
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
private Date create_time;
}
Service层:
package com.czxy.Service;
import com.czxy.domain.Emp;
import java.util.List;
public interface EmpService {
List selectAll();
Emp selectBYId(String id);
Boolean edit(Emp emp);
Boolean add(Emp emp);
Boolean deleteEmp(String id);
}
serviceImpl层:
package com.czxy.Service.impl;
import com.czxy.Service.EmpService;
import com.czxy.dao.EmpMapper;
import com.czxy.domain.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public List selectAll() {
return empMapper.selectAll();
}
@Override
public Emp selectBYId(String id) {
return empMapper.selectByPrimaryKey(id);
}
@Override
public Boolean edit(Emp emp) {
return empMapper.updateByPrimaryKey(emp)==1;
}
@Override
public Boolean add(Emp emp) {
return empMapper.insert(emp)==1;
}
@Override
public Boolean deleteEmp(String id) {
return empMapper.deleteByPrimaryKey(id)==1;
}
}
baseResult类:
package com.czxy.Vo; import java.util.HashMap; import java.util.Map; public class baseResult{ //成功状态码 public static final int OK = 1; //失败状态码 public static final int ERROR = 0; //返回码 private Integer code; //返回消息 private String message; //存放数据 private T data; //其他数据 private Map other = new HashMap<>(); public baseResult() { } public baseResult(Integer code, String message) { this.code = code; this.message = message; } public baseResult(Integer code, String message, T data) { this.code = code; this.message = message; this.data = data; } public static baseResult ok(String message){ return new baseResult(baseResult.OK , message); } public static baseResult ok(String message, Object data){ return new baseResult(baseResult.OK , message, data ); } public static baseResult error(String message){ return new baseResult(baseResult.ERROR , message); } public baseResult append(String key , Object msg){ other.put(key , msg); return this; } public Integer getCode() { return code; } public String getMessage() { return message; } public T getData() { return data; } public Map getOther() { return other; } }
Application 类:
package com.czxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EmpApplication {
public static void main(String[] args) {
SpringApplication.run(EmpApplication.class);
}
}
前端:
emp_List.vue
id
name
password
age
sex
birthday
email
phone
dept_id
position_id
create_id
create_time
操作
{{ emp.id }}
{{ emp.name }}
{{ emp.password }}
{{ emp.age }}
{{ emp.sex }}
{{ emp.birthday }}
{{ emp.email }}
{{ emp.phone }}
{{ emp.dept_id }}
{{ emp.position_id }}
{{ emp.create_id }}
{{ emp.create_time }}
修改 |
删除
添加
emp_edit.vue
emp_add.vue
router->index.js
{
path: '/emp',
name: 'emp',
component: () => import('@/views/Emp_List.vue'),
}, {
path: '/editEmp/:id',
name: 'edit',
component: () => import('@/views/Emp_edit.vue'),
}, {
path: '/addEmp',
name: 'add',
component: () => import('@/views/Emp_add.vue'),
},
App.vue:



