◆ 정상적인 CRUD를 위한 JAVA소스 수정


1. 먼저 maven 의 generate-sources 를 해준다.    


           그러면 아래와 같이 target하위에 소스가 생긴다.


2.  JAVA 소스 수정

 - com/dasdes/shopmng/contrillers/ProductController.java : 조회조건 받을수 있도록 수정 

 - com/dasdes/shopmng/domain/prdt/Product.java           : 필수값체크

 - com/dasdes/shopmng/domain/BaseService.java             : Entity 와 연결되는 QProduct.java(위 generate에서 생성됨) 선언

 - com/dasdes/shopmng/domain/prdt/ProductService.java  : 조회조건 filter추가 및 list 리턴객체 교체


  1) ProductController.java 수정

package com.dasdes.shopmng.controllers;


import com.chequer.axboot.core.api.response.Responses;

import com.chequer.axboot.core.controllers.BaseController;

import com.chequer.axboot.core.parameter.RequestParams;

import org.springframework.stereotype.Controller;

import com.chequer.axboot.core.api.response.ApiResponse;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import com.dasdes.shopmng.domain.prdt.Product;

import com.dasdes.shopmng.domain.prdt.ProductService;

import com.wordnik.swagger.annotations.ApiImplicitParam;  //추가

import com.wordnik.swagger.annotations.ApiImplicitParams; //추가


import javax.inject.Inject;

import java.util.List;


@Controller

@RequestMapping(value = "/api/v1/product")

public class ProductController extends BaseController {


    @Inject

    private ProductService productService;


    @RequestMapping(method = RequestMethod.GET, produces = APPLICATION_JSON)

    @ApiImplicitParams({

            @ApiImplicitParam(name = "prdtCd", value = "제품코드", dataType = "String", paramType = "query"),

            @ApiImplicitParam(name = "prdtNm", value = "제품명", dataType = "String", paramType = "query"),

            @ApiImplicitParam(name = "filter", value = "검색어", dataType = "String", paramType = "query")

    })    

    public Responses.ListResponse list(RequestParams<Product> requestParams) {

        List<Product> list = productService.gets(requestParams);

        return Responses.ListResponse.of(list);

    }


    @RequestMapping(method = {RequestMethod.PUT}, produces = APPLICATION_JSON)

    public ApiResponse save(@RequestBody List<Product> request) {

        productService.savePrdt(request);

        return ok();

    }

}


  2) Product.java 수정

package com.dasdes.shopmng.domain.prdt;


import com.chequer.axboot.core.annotations.ColumnPosition;

import com.dasdes.shopmng.domain.SimpleJpaModel;

import lombok.*;

import org.apache.ibatis.type.Alias;

import org.hibernate.annotations.DynamicInsert;

import org.hibernate.annotations.DynamicUpdate;

import com.chequer.axboot.core.annotations.Comment;

import javax.persistence.*;


import javax.validation.constraints.NotNull;



@Setter

@Getter

@DynamicInsert

@DynamicUpdate

@Entity

@EqualsAndHashCode(callSuper = true)

@Table(name = "prdt_base")

@Comment(value = "")

@Alias("product")

public class Product extends SimpleJpaModel<String> {


@Id

@Column(name = "prdt_cd", length = 50, nullable = false)

@NotNull(message = "제품코드를 입력하세요")   //pk이므로 not null 체크 추가

@Comment(value = "제품코드")

private String prdtCd;


@Column(name = "prdt_nm", length = 50, nullable = false)

@Comment(value = "제품명")

private String prdtNm;


@Column(name = "origin", length = 50, nullable = false)

@Comment(value = "원산지")

private String origin;


@Column(name = "purchase_price", precision = 10, nullable = false)

@Comment(value = "매입가격")

private Integer purchasePrice;


@Column(name = "sales_price", precision = 10, nullable = false)

@Comment(value = "판매가격")

private Integer salesPrice;



    @Override

    public String getId() {

        return prdtCd;

    }


3) BaseService.java 수정

package com.dasdes.shopmng.domain;


import com.dasdes.shopmng.domain.code.QCommonCode;

import com.dasdes.shopmng.domain.file.QCommonFile;

import com.dasdes.shopmng.domain.program.QProgram;

import com.dasdes.shopmng.domain.program.menu.QMenu;

import com.dasdes.shopmng.domain.user.QUser;

import com.dasdes.shopmng.domain.user.auth.QUserAuth;

import com.dasdes.shopmng.domain.user.auth.menu.QAuthGroupMenu;

import com.dasdes.shopmng.domain.user.role.QUserRole;

import com.chequer.axboot.core.domain.base.AXBootBaseService;

import com.chequer.axboot.core.domain.base.AXBootJPAQueryDSLRepository;

import com.dasdes.shopmng.domain.prdt.QProduct;  //추가


import java.io.Serializable;



public class BaseService<T, ID extends Serializable> extends AXBootBaseService<T, ID> {


    protected QUserRole qUserRole = QUserRole.userRole;

    protected QAuthGroupMenu qAuthGroupMenu = QAuthGroupMenu.authGroupMenu;

    protected QCommonCode qCommonCode = QCommonCode.commonCode;

    protected QUser qUser = QUser.user;

    protected QProgram qProgram = QProgram.program;

    protected QUserAuth qUserAuth = QUserAuth.userAuth;

    protected QMenu qMenu = QMenu.menu;

    protected QCommonFile qCommonFile = QCommonFile.commonFile;

    protected QProduct qProduct = QProduct.product; //추가


    protected AXBootJPAQueryDSLRepository<T, ID> repository;


    public BaseService() {

        super();

    }


    public BaseService(AXBootJPAQueryDSLRepository<T, ID> repository) {

        super(repository);

        this.repository = repository;

    }

}


4) ProductService.java 수정

package com.dasdes.shopmng.domain.prdt;


import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional; //추가


import com.dasdes.shopmng.domain.BaseService;

import com.querydsl.core.BooleanBuilder;  //추가


import javax.inject.Inject;

import com.chequer.axboot.core.parameter.RequestParams;

import java.util.List;


@Service

public class ProductService extends BaseService<Product, String> {

    private ProductRepository productRepository;


    @Inject

    public ProductService(ProductRepository productRepository) {

        super(productRepository);

        this.productRepository = productRepository;

    }


    public List<Product> gets(RequestParams<Product> requestParams) {

    String prdtCd=requestParams.getString("prdtCd", "");

    String prdtNm=requestParams.getString("prdtNm", "");

    String filter = requestParams.getString("filter");

   

    BooleanBuilder builder = new BooleanBuilder();



        if (isNotEmpty(prdtCd)) {

            builder.and(qProduct.prdtCd.eq(prdtCd));

        }


        if (isNotEmpty(prdtNm)) {

            builder.and(qProduct.prdtNm.eq(prdtNm));

        }


        List<Product> prdtList = select().from(qProduct).where(builder).orderBy(qProduct.prdtCd.asc(), qProduct.prdtNm.asc()).fetch();


        if (isNotEmpty(filter)) {

        prdtList = filter(prdtList, filter);

        }

   

        return prdtList;

    }

    

    //저장

    @Transactional

    public void savePrdt(List<Product> product) {

        save(product);

    }

}


3. 서버구동 및 CRUD 테스트

1) 저장/수정테스트 : 정상작동 OK


2) 검색필터 테스트 : 필터링 잘 됨. OK



Axboot 신규화면 만들기 4 - Detail 내역 컬럼 콤보박스 만들기(DB연동) 에서 계속........


만족하셨나요? ~~~~~~~

+ Recent posts