搭建父工程
父工程的作用,主要就是给子工程提供依赖,不需要有src下面的代码
子模块common
里面是新建实体类Result,封装了前端返回后端的信息
idWorker是雪花算法,用来标识id的,分布式id生成器
基础的CRUD模块base
新增异常
package com.mmz.base.controller;
import entity.Result;
import entity.StatusCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @Classname BaseExceptionHandler
* @Description TODO
* @Date 2020/3/18 22:15
* @Created by mmz
*/
@RestControllerAdvice
public class BaseExceptionHandler {
@ExceptionHandler(value = Exception.class)
public Result exception(Exception e) {
e.printStackTrace();
return new Result(false, StatusCode.ERROR,e.getMessage());
}
}
Controller层
package com.mmz.base.controller;
import com.mmz.base.pojo.Label;
import com.mmz.base.service.LabelService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.persistence.Id;
/**
* @Classname BaseController
* @Description TODO
* @Date 2020/3/18 20:04
* @Created by mmz
*/
@RestController
@CrossOrigin
@RequestMapping("/label")
public class LabelController {
@Autowired
private LabelService labelService;
@RequestMapping(method = RequestMethod.GET)
public Result findAll(){
return new Result(true, StatusCode.OK,"查询成功",labelService.findAll());
}
@RequestMapping(method = RequestMethod.GET,value = "/{labelId}")
public Result findById(@PathVariable String labelId){
return new Result(true, StatusCode.OK,"查询成功",labelService.findById(labelId));
}
@RequestMapping(method = RequestMethod.POST)
public Result save(@RequestBody Label label){
labelService.save(label);
return new Result(true, StatusCode.OK,"添加成功");
}
@RequestMapping(method = RequestMethod.PUT,value = "/{labelId}")
public Result update(@RequestBody Label label,@PathVariable String labelId){
label.setId(labelId);
labelService.update(label);
return new Result(true, StatusCode.OK,"更新成功");
}
@RequestMapping(method = RequestMethod.DELETE,value = "/{labelId}")
public Result deleteById(@PathVariable String labelId){
labelService.delete(labelId);
return new Result(true, StatusCode.OK,"删除成功");
}
}
配置文件yml
server:
port: 9001
spring:
application:
name: tensquare-base
datasource:
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.174.128:3306/tensquare_base?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
username: root
jpa:
database: mysql
show-sql: true