[AXBoot] 메뉴 생성부터 Swagger, H2-Console, ModelExtractor, 컨트롤러 추가, 검색기능까지

yesjm·2021년 4월 26일
1
post-thumbnail

1. 프로그램 생성

거래처 관리 프로그램을 생성한다.

2. 메뉴 관리

기준정보-거래처 관리 경로를 만들고 프로그램 명을 선택하고 권한을 설정한다.

3. H2-Console

EDUCATION_YESJM DB를 생성한다.

CREATE TABLE education_yesjm
  ID BIGINT( 20 ) NOT NULL auto_increment,
  COMPANY_NM VARCHAR(30),
  CEO VARCHAR(30),
  BIZNO VARCHAR(10),
  TEL VARCHAR(18),
  ZIP VARCHAR(7),
  ADDRESS VARCHAR(200),
  ADDRESS_DETAIL VARCHAR(200),
  EMAIL VARCHAR(50),
  REMARK VARCHAR(500),
  USE_YN VARCHAR(1),
  CREATED_AT TIMESTAMP,
  CREATED_BY VARCHAR(100),
  UPDATED_AT TIMESTAMP,
  UPDATED_BY VARCHAR(100)
)

4. ModelExtractor

역공학을 실행한다. 클래스명은 YesjmGrid로 지정했다.

5. YesjmGrid.java

필요 없는 어노테이션을 제거하고, @ID를 추가한다.

package edu.axboot.domain._education;

import edu.axboot.domain.SimpleJpaModel;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;

@Setter
@Getter
@DynamicInsert
@DynamicUpdate
@Entity
@Table(name = "EDUCATION_YESJM")
public class YesjmGrid extends SimpleJpaModel<Long> {

	@Id
	@Column(name = "ID", precision = 19, nullable = false)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Column(name = "COMPANY_NM", length = 30)
	private String companyNm;

	@Column(name = "CEO", length = 30)
	private String ceo;

	@Column(name = "BIZNO", length = 10)
	private String bizno;

	@Column(name = "TEL", length = 18)
	private String tel;

	@Column(name = "ZIP", length = 7)
	private String zip;

	@Column(name = "ADDRESS", length = 200)
	private String address;

	@Column(name = "ADDRESS_DETAIL", length = 200)
	private String addressDetail;

	@Column(name = "EMAIL", length = 50)
	private String email;

	@Column(name = "REMARK", length = 500)
	private String remark;

	@Column(name = "USE_YN", length = 1)
	private String useYn;

	@Override
	public Long getId() {
		return id;
	}
}

6. YesjmGridController.java

package edu.axboot.controllers;

import com.chequer.axboot.core.api.response.Responses;
import com.chequer.axboot.core.controllers.BaseController;
import com.chequer.axboot.core.parameter.RequestParams;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
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 edu.axboot.domain._education.YesjmGrid;
import edu.axboot.domain._education.YesjmGridService;

import javax.inject.Inject;
import java.util.List;

@Controller
@RequestMapping(value = "/api/v1/_education/yesjmGrid")
public class YesjmGridController extends BaseController {

    @Inject
    private YesjmGridService yesjmGridService;

    @RequestMapping(method = RequestMethod.GET, produces = APPLICATION_JSON)
    public Responses.ListResponse list(RequestParams<YesjmGrid> requestParams) {
        List<YesjmGrid> list = yesjmGridService.gets(requestParams);
        return Responses.ListResponse.of(list);
    }

    @RequestMapping(method = {RequestMethod.PUT}, produces = APPLICATION_JSON)
    public ApiResponse save(@RequestBody List<YesjmGrid> request) {
        yesjmGridService.save(request);
        return ok();
    }


    @RequestMapping(value = "/queryDsl", method = RequestMethod.GET, produces = APPLICATION_JSON)
    @ApiImplicitParams({
            @ApiImplicitParam(name="company", value="회사명", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name="ceo", value="대표자", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name="bizno", value="사업자번호", dataType = "string", paramType = "query"),
    })
    public Responses.ListResponse select(RequestParams<YesjmGrid> requestParams) {
        List<YesjmGrid> yesjmGrid = this.yesjmGridService.getByQueryDsl(requestParams);
        return Responses.ListResponse.of(yesjmGrid);
    }

    @RequestMapping(value = "/queryDsl/selectOne", method = RequestMethod.GET, produces = APPLICATION_JSON)
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value="ID", required = true, dataType = "Long", paramType = "query"),
    })
    public YesjmGrid selectOne(RequestParams<YesjmGrid> requestParams) {
        YesjmGrid yesjmGrid = this.yesjmGridService.getOneByQueryDsl(requestParams);
        return yesjmGrid;
    }

    @RequestMapping(value = "/queryDsl", method = {RequestMethod.PUT}, produces = APPLICATION_JSON)
    public ApiResponse save2(@RequestBody List<YesjmGrid> request) {
        yesjmGridService.saveByQueryDsl(request);
        return ok();
    }
}

7. YesjmGridService.java

package edu.axboot.domain._education;

import com.querydsl.core.BooleanBuilder;
import org.springframework.stereotype.Service;
import edu.axboot.domain.BaseService;
import javax.inject.Inject;
import com.chequer.axboot.core.parameter.RequestParams;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class YesjmGridService extends BaseService<YesjmGrid, Long> {
    private YesjmGridRepository yesjmGridRepository;

    @Inject
    public YesjmGridService(YesjmGridRepository yesjmGridRepository) {
        super(yesjmGridRepository);
        this.yesjmGridRepository = yesjmGridRepository;
    }

    public List<YesjmGrid> gets(RequestParams<YesjmGrid> requestParams) {
        return findAll();
    }

    public List<YesjmGrid> getByQueryDsl(RequestParams<YesjmGrid> requestParams) {
        String company = requestParams.getString("company","");
        String ceo = requestParams.getString("ceo","");
        String bizno = requestParams.getString("bizno","");

        BooleanBuilder builder = new BooleanBuilder();

        if (isNotEmpty(company)) {
            builder.and(qYesjmGrid.companyNm.eq(company));
        }
        if (isNotEmpty(ceo)) {
            builder.and(qYesjmGrid.ceo.eq(ceo));
        }
        if (isNotEmpty(bizno)){
            builder.and(qYesjmGrid.bizno.eq(bizno));
        }

        List<YesjmGrid> yesjmGridList = select()
                .from(qYesjmGrid)
                .where(builder)
                .orderBy(qYesjmGrid.companyNm.asc())
                .fetch();

        return yesjmGridList;
    }

    public YesjmGrid getOneByQueryDsl(RequestParams<YesjmGrid> requestParams) {
        Long id = requestParams.getLong("id");
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qYesjmGrid.id.eq(id));
        YesjmGrid yesjmGrid = select().from(qYesjmGrid).where(builder).fetchOne();
        return yesjmGrid;
    }

    @Transactional
    public void saveByQueryDsl(List<YesjmGrid> request) {
        for (YesjmGrid yesjmGrid:request) {
            if (yesjmGrid.isCreated()) {
                save(yesjmGrid);
            } else if (yesjmGrid.isModified()) {
                update(qYesjmGrid)
                        .set(qYesjmGrid.companyNm, yesjmGrid.getCompanyNm())
                        .set(qYesjmGrid.ceo, yesjmGrid.getCeo())
                        .set(qYesjmGrid.bizno, yesjmGrid.getBizno())
                        .where(qYesjmGrid.id.eq(yesjmGrid.getId()))
                        .execute();
            } else if (yesjmGrid.isDeleted()){
                delete(qYesjmGrid)
                        .where(qYesjmGrid.id.eq(yesjmGrid.getId()))
                        .execute();
            }
        }

    }
}

8. Swagger 확인

swagger에서 정상적으로 동작하는것을 확인한다.

9. education-yesjm-grid.jsp

        <div role="page-header">
            <ax:form name="searchView0">
                <ax:tbl clazz="ax-search-tbl" minWidth="500px">
                    <ax:tr>
                        <ax:td label='ax.base.company.name' width="300px">
                            <input type="text" name="company" id="company" class="form-control" />
                        </ax:td>
                        <ax:td label='ax.base.company.ceo' width="300px">
                            <input type="text" name="ceo" id="ceo" class="form-control" />
                        </ax:td>
                        <ax:td label='ax.base.company.bizno' width="300px">
                            <input type="text" name="bizno" id="bizno" class="form-control" />
                        </ax:td>
                    </ax:tr>
                </ax:tbl>
            </ax:form>
            <div class="H10"></div>
        </div>

10.education-teach-grid.js

searchView와 gridView를 수정하고 PAGE_SEARCH와 PAGE_SAVE의 url을 변경한다.

//== view 시작
/**
 * searchView
 */
fnObj.searchView = axboot.viewExtend(axboot.searchView, {
    initView: function () {
        this.target = $(document['searchView0']);
        this.target.attr('onsubmit', 'return ACTIONS.dispatch(ACTIONS.PAGE_SEARCH);');
        this.filter = $('#filter');
        this.company = $('#company');
        this.ceo = $('#ceo');
        this.bizno = $('#bizno');
    },
    getData: function () {
        return {
            pageNumber: this.pageNumber,
            pageSize: this.pageSize,
            filter: this.filter.val(),
            company: this.company.val(),
            ceo: this.ceo.val(),
            bizno: this.bizno.val(),
        };
    },
});

/**
 * gridView
 */
fnObj.gridView01 = axboot.viewExtend(axboot.gridView, {
    initView: function () {
        var _this = this;

        this.target = axboot.gridBuilder({
            showRowSelector: true,
            frozenColumnIndex: 0,
            multipleSelect: true,
            target: $('[data-ax5grid="grid-view-01"]'),
            columns: [
                { key: 'companyNm', label: COL('company.name'), width: 300, align: 'left', editor: 'text' },
                { key: 'ceo', label: COL('company.ceo'), width: 100, align: 'center', editor: 'text' },
                { key: 'bizno', label: COL('company.bizno'), width: 100, align: 'center', editor: 'text' },
                { key: 'tel', label: COL('company.tel'), width: 100, align: 'center', editor: 'text' },
                { key: 'email', label: COL('company.email'), width: 100, align: 'center', editor: 'text' },
                { key: 'useYn', label: COL('use.or.not'), width: 100, align: 'center', editor: 'text' },
            ],
            body: {
                onClick: function () {
                    this.self.select(this.dindex, { selectedClear: true });
                },
            },
        });

        axboot.buttonClick(this, 'data-grid-view-01-btn', {
            add: function () {
                ACTIONS.dispatch(ACTIONS.ITEM_ADD);
            },
            delete: function () {
                ACTIONS.dispatch(ACTIONS.ITEM_DEL);
            },
        });
    },
    getData: function (_type) {
        var list = [];
        var _list = this.target.getList(_type);

        if (_type == 'modified' || _type == 'deleted') {
            list = ax5.util.filter(_list, function () {
                return this.id;
            });
        } else {
            list = _list;
        }
        return list;
    },
    addRow: function () {
        this.target.addRow({ __created__: true }, 'last');
    },
});

searchView에 검색할 항목에 대한 값을 추가해줘야 검색이 된다. gridView의 getData도 return this.id;로 수정을 해줘야 삭제가 되니 주의하자.

profile
yesjm's second brain

0개의 댓글