创建controller/service/mapper/impl过程
先创建entity包,在里面导入实体类
然后创建controller/mapper/service包
然后在service里创建impl包
然后在mapper包里写mapper接口,加上@Mapper注解,继承BaseMapper<Employee>,泛型是实体类
@Mapper public interface EmployeeMapper extends BaseMapper<Employee> { }
由于继承,把常见增删改查都有了
然后创建service,继承IService<Employee>泛型是实体类
public interface EmployeeService extends IService<Employee> { }
然后创建service的实现类,加上@Servise注解,extends ServiceImpl<>泛型是EmployeeMapper和Employee,实现implements EmployeeService
@Service public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService { }
然后创建controller,访问路径注解,注入service
@Slf4j @RestController @RequestMapping("/employee") public class EmployeeController { @Autowired private EmployeeService employeeService; }
创建common包,在里面导入R类,返回结果类
package com.itheima.reggie.common; import lombok.Data; import java.util.HashMap; import java.util.Map; /** * 通用返回结果,服务端响应的数据最终都会封装成此对象 * @param <T> */ @Data public class R<T> { private Integer code; //编码:1成功,0和其它数字为失败 private String msg; //错误信息 private T data; //数据 private Map map = new HashMap(); //动态数据 public static <T> R<T> success(T object) { R<T> r = new R<T>(); r.data = object; r.code = 1; return r; } public static <T> R<T> error(String msg) { R r = new R(); r.msg = msg; r.code = 0; return r; } public R<T> add(String key, Object value) { this.map.put(key, value); return this; } }