QueryDSL?
์คํ์์ค ํ๋ก์ ํธ๋ก ๋ณต์กํ Creteria๋ฅผ ๋์ฒดํ๋ JPQL๋น๋์ด๋ค.
๋ณต์กํ ์ฟผ๋ฆฌ์ ๋์ ์ฟผ๋ฆฌ๋ฅผ ๊น๋ํ๊ฒ ํด๊ฒฐํด์ฃผ๊ณ ์ฟผ๋ฆฌ๋ฅผ ์๋ฐ ์ฝ๋๋ก ์์ฑํ ์ ์๋ค. ๋ฐ๋ผ์ ๋ฌธ๋ฒ์ค๋ฅ๋ฅผ ์ปดํ์ผ ๋จ๊ณ์์ ์ก์์ค ์ ์๋ค.
-Creteria๋ Java์ฝ๋๋ฅผ ์ด์ฉํด JPQL์ ์์ฑํ ์ ์๊ฒ ๋์์ฃผ๋ ํ์ ์ธ์ดํ(Type-Safe: ํ์ ์ ์์ ์ ์ฆ ์ด๋ ํ ์คํผ๋ ์ด์ (๋๋ ์ฐ์ฐ)๋ ์ ์๋์ง ์์ ๊ฒฐ๊ณผ๋ฅผ ๋ด๋์ง ์์ ๊ฒ์ด๋ค. ์์ธก๋ถ๊ฐ๋ฅํ ๊ฒฐ๊ณผ๋ฅผ ๋ด์ง ์๋ ๊ฒ์ ๋ปํจ)๋ฅผ ์ ๊ณตํ๋ ์ฟผ๋ฆฌ์ด๋ค. SQL ์ฟผ๋ฆฌ๋ฅผ ๋์ฒดํด HQL, JPQL์ ์๋ฐ ์ฝ๋๋ก ์ด์ฉํด ์์ฑํ ์ ์๊ฒ ํด ์ค๋ค.
-JPQL๋ JPA์ ์ผ๋ถ๋ก ์ ์๋ ํ๋ซํผ ๋ ๋ฆฝ์ ์ธ ๊ฐ์ฒด์งํฅ ์ฟผ๋ฆฌ์ด๋ค.
SQL์ ํฌ๊ฒ ์ํฅ์ ๋ฐ์ SQL๋ฌธ๊ณผ ๋น์ทํ์ง๋ง ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํ ์ด๋ธ์ ์ง์ ์ฐ๊ฒฐ๋๋ ๊ฒ์ด ์๋๋ผ
JPA ์ํฐํฐ์ ๋ํด ๋์ํ๋ค.
โDB 'Company'์ ๋ํ ๊ฐ์ฒด ๋ฑ๋ก ๋ฐ ์ญ์ /์์ /์กฐํ ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ
1. ์งํํ๊ณ ์๋ ํ๋ก์ ํธ์์ Companyservice๋จ์์ ์์๋
BaseService์ QueryDSL์ ๊ฐ์ฒด๋ฅผ ๋ฑ๋กํ ํ,
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 QCompany qCompany = QCompany.company;
protected AXBootJPAQueryDSLRepository<T, ID> repository;
public BaseService() {
super();
}
public BaseService(AXBootJPAQueryDSLRepository<T, ID> repository) {
super(repository);
this.repository = repository;
}
}
2. CompanyService.java์ ์ฌ์ฉํ QueryDSL ๊ธฐ๋ฅ์ ์ถ๊ฐํจ.
//QueryDSL
public List<Company> getByQueryDsl(RequestParams<Company> 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(qCompany.companyNm.eq(company));
}
if (isNotEmpty(ceo)) {
builder.and(qCompany.ceo.eq(ceo));
}
if (isNotEmpty(bizno)) {
builder.and(qCompany.bizno.eq(bizno));
}
List<Company> companyList = select()
.from(qCompany)
.where(builder)
.orderBy(qCompany.companyNm.asc())
.fetch();
return companyList;
}
@Transactional
public void saveByQueryDsl(List<Company> request) {
for (Company company: request) {
if (company.isCreated()) {
save(company);
} else if (company.isModified()) {
update(qCompany)
.set(qCompany.companyNm, company.getCompanyNm())
.set(qCompany.ceo, company.getCeo())
.where(qCompany.id.eq(company.getId()))
.execute();
} else if (company.isDeleted()) {
delete(qCompany)
.where(qCompany.id.eq(company.getId()))
.execute();
}
}
}
3. CompanyController.java์ @ApiImplicitParams(์ ์ฅ๋ ํ ์ด๋ธ ์ปฌ๋ผ์ ๋ฐ์ดํฐ ์กฐํ๋ก ํ ์คํธํ ์ ์๋ ๊ธฐ๋ฅ?)
@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 list2(RequestParams<Company> requestParams) {
List<Company> list = companyService.getByQueryDsl(requestParams);
return Responses.ListResponse.of(list);
}
@RequestMapping(value = "/QueryDsl", method = {RequestMethod.PUT}, produces = APPLICATION_JSON)
public ApiResponse save2(@RequestBody List<Company> request) {
companyService.saveByQueryDsl(request);
return ok();
}
4. company.jsp์์ ํ๋ฉด๋จ์ ํด๋นํ๋ ๊ฐ์ ์ถ์ถํ๊ธฐ ์ํด์ ๊ฐ inputํ๊ทธ์ NAME๊ณผ ID๋ฅผ ๊ฐ์ ์ด๋ฆ์ผ๋ก
์์๋ก ์ถ๊ฐํ๋ค.
<div role="page-header">
<ax:form name="searchView0">
<ax:tbl clazz="ax-search-tbl" minWidth="500px">
<ax:tr>
<ax:td label='ํ์ฌ๋ช
' width="300px">
<input type="text" name="company" id="company" class="form-control" />
</ax:td>
<ax:td label='๋ํ์' width="300px">
<input type="text" name="ceo" id="ceo" class="form-control" />
</ax:td>
<ax:td label='์ฌ์
์๋ฒํธ' 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>
5. company.js์์ jsp์์ ์ถ๊ฐํ๋ name/id ์์๋ก ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์๊ฒ ์์ค์ฝ๋๋ฅผ ์ถ๊ฐ.
fnObj.searchView = axboot.viewExtend(axboot.searchView, {
initView: function () {
this.target = $(document["searchView0"]);
this.target.attr("onsubmit", "return ACTIONS.dispatch(ACTIONS.PAGE_SEARCH);");
this.company = $("#company");
this.ceo = $("#ceo");
this.bizno = $("#bizno");
},
getData: function () {
return {
pageNumber: this.pageNumber,
pageSize: this.pageSize,
company: this.company.val(),
ceo: this.ceo.val(),
bizno: this.bizno.val()
}
}
});
โ๋ฐ์ดํฐ ์์ ๊ธฐ๋ฅ
1. company.js์์ PAGE_SAVE์ type : put์์ญ์ querydsl ๊ฒฝ๋ก๋ฅผ ์ถ๊ฐํ์ฌ ์์ ๊ธฐ๋ฅ์ ์์ฑํ๋ค.
PAGE_SAVE: function (caller, act, data) {
var saveList = [].concat(caller.gridView01.getData()); //modified์ญ์ ํจ
saveList = saveList.concat(caller.gridView01.getData("deleted"));
axboot.ajax({
type: "PUT",
// url: "/api/v1/company",
url: "/api/v1/company/QueryDsl",
data: JSON.stringify(saveList),
callback: function (res) {
ACTIONS.dispatch(ACTIONS.PAGE_SEARCH);
axToast.push("์ ์ฅ ๋์์ต๋๋ค");
}
});
},
โ๋ฐ์ดํฐ ์ญ์ ๊ธฐ๋ฅ
getData: function (_type) {
var list = [];
var _list = this.target.getList(_type);
if (_type == "modified" || _type == "deleted") {
list = ax5.util.filter(_list, function () {
// delete this.deleted;
// return this.key;
return this.id; // ๊ฐ์ฒด์์๋ key๊ฐ์ด ์์ผ๋ฏ๋ก id๊ฐ์ ๋ฐ์์จ๋ค.
});
} else {
list = _list;
}
return list;
},
์ธํ ๋ฆฌ์ ์ด ์ปค๋ฎค๋ํฐ ๋ฒ์ ์ผ๋ก ํ๋ฉด๋จ์ ์ค์๊ฐ ์ ๋ฐ์ดํธํ ์ ์๋ dependency
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.12.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
'Programming๐ฉ๐ปโ๐ป > AxBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Banner / Logging / Excel download (0) | 2021.04.30 |
---|---|
jsfiddle (0) | 2021.04.23 |
*์ค์ ๋๋ฒ๊น , ๋จ์ํ ์คํธ (0) | 2021.04.22 |
Mybatis์ฐ๋ (0) | 2021.04.19 |
Mac ๋งฅ) Maven ์ค์น ๋ฐ ํ๊ฒฝ ๋ณ์ ์ค์ (0) | 2021.04.19 |