日常开发CRUD☺️示例代码


一、Controller

import com.ne.boot.common.entity.Page;
import com.ne.boot.common.entity.Response;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(description = "车型零件服务")
@RequestMapping("modelOtaunit")
@ResponseBody
public class DemoController {
 
    // 参数校验
    @ApiOperation("新增车型零件")
    @PostMapping("add")
    public Response addModelOtaunit(@RequestBody  @Valid @NotNull VehicleModelOtaunit otaunit) {
        otaunitHandler.addModelOtaunit(otaunit);
        return Response.success();
    }

    // 查询
    @ApiOperation("查询车型零件下拉列表")
    @GetMapping("getPartDeviceList")
    public Response getPartDeviceList( @ApiParam("零件号") @RequestParam(name = "partNumber",required = false) String  partNumber,
            @ApiParam("物料号") @RequestParam(name = "materialNo",required = false) String  materialNo) {
        return Response.success(otaunitHandler.getPartDeviceList(partNumber,materialNo));
    }


    @ApiOperation("分页查询车型零件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "查询页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "查询返回数据条数", paramType = "query")
    })
    @GetMapping("getOtaManufacturer")
    public Response getModelOtaunitList(@ApiParam("车型ID") @RequestParam(name = "modelId") Long modelId,
            @ApiParam("ecuID") @RequestParam(name = "ecuId", required = false) Long ecuId,
            @ApiParam("零件号") @RequestParam(name = "partNumber", required = false) String partNumber,
            @ApiIgnore Page page) {
        List list = otaunitHandler.getModelOtaunitList(modelId, ecuId, partNumber, page);
        return Response.success(list, page);
    }

    @ApiOperation("新增供应商")
    @PostMapping("add")
    public Response add(@RequestBody Manufacturer manufacturer) {
        manufacturerHandler.add(manufacturer);
        return Response.success();
    }


  //  参数是List
    @GetMapping("/getListByName")
    List getListByName(@RequestParam("nameList") List nameList);
}

// 实体类
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class VehicleModelOtaunit {
    @NotNull(message = "零件号不能为空!")
    @Size(max = 40, min = 1, message = "零件号长度不符合要求!")
    private String partNumber;
}

二、Dao

基本的增删改查一定要用easycode生成,这样高效而且不会错

1、参数是List List非空判断


 
            AND arrival_city_open_id in 

2、参数是string + List

List get(@Param("repairOpenId")String repairOpenId,@Param("propertyList") List propertyList);

  

3、参数是string,结果是int


   
  List getSupplierIdListBySupplierName(String supplierName);     

4、模糊查询

前后模糊
  SELECT city_id FROM car_dim_city WHERE city_name
   LIKE CONCAT('%', #{name}, '%')
  
后模糊
WHERE city_name LIKE CONCAT('芜湖', '%') LIMIT 1;

5、根据时间判断

        
            AND warning_date =]]> #{beginDate}
        
        
            AND warning_date #{endDate}
        

三、日志记录,异常打印

log.error("[滴滴取消订单]--订单编号:{},异常信息:{}", orderNo, Throwables.getStackTraceAsString(e));

     
            com.google.guava
            guava
            18.0
        

四、枚举、常量

contants 采用接口(Interface)中变量默认为static final的特性

public interface Constants {
    String LOGIN_KEY = "OTA:VEHICLE:LOGIN";
}

package com.mycar.mycar.car.server.enums;

import lombok.Getter;
@Getter
public enum ViolationResultEnum {

    /** 查询成功 */
    SUCCESS(80000,"查询成功"),
    /** 没有传入城市 */
    NO_CITYNAME(70005,"没有传入城市"),
    ;

    private final Integer key;
    private final String message;
    ViolationResultEnum(Integer key,String message) {
        this.key = key;
        this.message = message;
    }
}

====== 单value 枚举
@Getter
public enum CarTypeEnum {
    /** 查询成功 */
    SMALL_CAR("02"),
    /** 查询成功 */
    BIG_NEWENERGY_CAR("51"),
    ;

    private final String key;

    CarTypeEnum(String key) {
        this.key = key;
    }
}

public enum EnumYesNo {
    YES(1, "是"),
    NO(0, "否");

    private final Integer key;
    private final String value;

    private EnumYesNo(Integer key, String value) {
        this.key = key;
        this.value = value;
    }

    public static EnumYesNo getEnumByKey(Integer key) {
        if (null == key) {
            return null;
        } else {
            EnumYesNo[] var1 = values();
            int var2 = var1.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                EnumYesNo temp = var1[var3];
                if (temp.getKey().equals(key)) {
                    return temp;
                }
            }

            return null;
        }
    }
}

相关