2016년 8월 4일 목요일
(방학특강)[스프링JPA강좌]QueryDSL4.X,JPAQueryFactory쿼리예문(조인,서브쿼리,GroupBy,OrderBy,삭제,수정,조회) ,열심히공부하세요 - 스프링실무교육학원
(방학특강)[스프링JPA강좌]QueryDSL4.X,JPAQueryFactory쿼리예문(조인,서브쿼리,GroupBy,OrderBy,삭제,수정,조회) ,열심히공부하세요 - 스프링실무교육학원
감사합니다.
아래 예문 참조하세요~
--------------------------------------------------------------------------------------------------------------------------
@Autowired
JPAQueryFactory queryFactory;
////////////////////////////////// QueryDSL용 메소드
@Override
public List<Emp> selectByJobOrderByEnameDesc(String job) {
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.job.eq(job))
.orderBy(emp.ename.desc()).fetch();
return emps;
}
@Override
@Transactional
public Long deleteByJob(String job) {
// new JPADeleteClause(em, emp)
// .where(emp.job.eq(job))
// .execute();
Long affectedRow = queryFactory.delete(emp)
.where(emp.job.eq(job))
.execute();
return affectedRow;
}
@Override
@Transactional
public Long updateByEmpno(Long empno, String newEname) {
// new JPAUpdateClause(em, emp)
// .where(emp.empno.eq(empno))
// .set(emp.ename, newEname)
// .execute();
Long affectedRow = queryFactory.update(emp)
.where(emp.empno.eq(empno))
.set(emp.ename, newEname)
.execute();
return affectedRow;
}
@Override
public List<Tuple> selectEnameJobByEmpno(Long empno) {
//Multi Column Select
List<Tuple> result = queryFactory.select(emp.ename, emp.job)
.from(emp)
.where(emp.empno.eq(empno))
.fetch();
return result;
}
@Override
public List<Tuple> selectSalAvgGroupbyJob(Long sumSal) {
List<Tuple> result = queryFactory
.select(emp.job, emp.sal.avg())
.from(emp)
.groupBy(emp.job)
.having(emp.sal.sum().gt(sumSal))
.fetch();
return result;
}
@Override
public List<Tuple> selectEmpEnameDnameJoinDept(Long deptno) {
List<Tuple> emps = queryFactory
.select(emp.ename, dept.dname)
.from(emp)
.innerJoin(emp.dept, dept)
.where(emp.dept.deptno.eq(deptno))
.fetch();
return emps;
}
@Override
public List<Emp> selectEmpMaxSal() {
QEmp e = new QEmp("e");
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.sal.eq(
JPA__EXPRESSION__s.select(e.sal.max()).from(e)))
.fetch();
return emps;
}
@Override
public List<Emp> selectEmpMaxSalOfDept() {
QEmp e = new QEmp("e");
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.sal.eq(
JPA__EXPRESSION__s
.select(e.sal.max()).from(e)
.where(emp.dept.deptno.eq(e.dept.deptno))
))
.fetch();
return emps;
}
@Override
public List<Emp> selectEmpGreaterThanAvgSal() {
QEmp e = new QEmp("e");
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.sal.gt(
JPA__EXPRESSION__s
.select(e.sal.avg()).from(e)
.where(emp.dept.deptno.eq(e.dept.deptno))
))
.fetch();
return emps;
}
@Override
public List<Emp> selectEmpEqualsEmpno(Long empno) {
QEmp e = new QEmp("e");
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.sal.eq(
JPA__EXPRESSION__s
.select(e.sal).from(e)
.where(e.empno.eq(empno))
))
.where(emp.empno.ne(empno))
.fetch();
return emps;
}
@Override
public List<Emp> selectEmpMaxSalTop3() {
List<Emp> emps = queryFactory.selectFrom(emp)
.orderBy(emp.sal.desc())
.limit(3)
.fetch();
return emps;
}
@Override
public List<String> selectDeptExistsEmp() {
List<String> depts = queryFactory.select(dept.dname).from(dept)
.where(JPA__EXPRESSION__s
.selectFrom(emp)
.where(emp.dept.deptno.eq(dept.deptno)).exists()
)
.fetch();
return depts;
}
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기