日常开发CRUD☺️示例代码

核心代码库

cool 核心代码库
D:\learnCode\cool

过去临时使用的 D:\learnCode\smallTool

一、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 org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

@Api(description = "车型零件服务")
@RequestMapping("modelOtaunit")
@RestController
@ResponseBody
public class DemoController {
 
    @Autowired
    private UserDao userDao;

    // 参数校验
    @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<VehicleModelOtaunit> 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<String>
    @GetMapping("/getListByName")
    List<AdminBuOutput> getListByName(@RequestParam("nameList") List<String> 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非空判断

<select id="getUserListByIds" resultType="com.mycar.mycar.admin.server.entity.AdminUser">
        SELECT * FROM sso_user where status = 1 AND user_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
 <if test="arrivalCityOpenIdList != null and arrivalCityOpenIdList.size > 0">
            AND arrival_city_open_id in </if>

2、参数是string + List

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

  <select id="get" resultMap="CarRepairDeliveryMap">
        select * from user  where wms_repair_open_id = #{repairOpenId}
        AND  property_value IN
        <foreach collection="propertyList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

3、参数是string,结果是int

<select id="getCityId" resultType="Integer" parameterType="java.lang.String">
  SELECT city_id FROM car_dim_city WHERE city_name like #{cityName} ;
   </select>
   
  List<Integer> 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、根据时间判断

        <if test="beginDate !=null and beginDate != ''">
            AND warning_date <![CDATA[>=]]> #{beginDate}
        </if>
        <if test="endDate !=null and endDate != ''">
            AND warning_date <![CDATA[<=]]>#{endDate}
        </if>

6、批量插入


    void insertBatch(List<RealnameSupplementVehicle> list);

    <insert id="insertBatch">
        insert into REALNAME_SUPPLEMENT_VEHICLE(vin)
        values
        <foreach collection="list" close="" index="index" item="item" open="" separator=",">
           (#{item.vin})
        </foreach>
    </insert>

7、批量更新

和插入的差别在与 separator=";"

<update id="updateBatchByVin">
      <foreach collection="list" close="" index="index" item="item" open="" separator=";">
          update T_SERVICE_PLAN_DETAIL set start_time = #{item.startTime},end_time = #{item.endTime},update_time=now()
          where vehicle_id=(SELECT id FROM VEHICLE WHERE vin =#{item.vin}) ORDER BY id DESC LIMIT 1
      </foreach>
  </update>

8、map 查询

 /**
     * 根据车辆vin集合批量查询车辆
     */
    @MapKey("vin")
    Map<String, Vehicle> queryIds(@Param("vinList") List<String> vinList);

    <select id="queryIds" resultType="com.ne.ice.cbsp.vehicle.vo.vehicle.Vehicle">
        select vin,id from `VEHICLE` where vin in
        <foreach collection="vinList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

7、结果是List<String>

在dao.xml文件中返回值定义为 resultType=“java.lang.String” 。

posted @ 2022-04-28 13:33  爱生活(^_-)  阅读(76)  评论(0)    收藏  举报