๐Ÿ”ฅ TIL - Day 61 QueryDSL 2

Kim Dae Hyunยท2021๋…„ 11์›” 20์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
72/93

QueryDSL์„ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ตํ•ด ๊ฐ€๋ณ๊ฒŒ ์•Œ์•„๋ดค๋‹ค.

QueryDSL์€ ๋ถ„๋ช… ์ข‹์€ ๋„๊ตฌ์ด์ง€๋งŒ ๋ชจ๋“  ์ฟผ๋ฆฌ๋ฅผ QueryDSL๋กœ ์งœ์•ผ๋˜๋Š” ๊ฑด ์•„๋‹ˆ๋‹ค.
JPA ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋„์›€์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ CRUD ์ฟผ๋ฆฌ๋Š” JPA์˜ ๋„์›€์„ ๋ฐ›์ง€ ์•Š์„ ์ด์œ ๊ฐ€ ์—†๋‹ค.

ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•˜๋ ค๋ฉด ๊ธฐ์กด JPA ์ธํ„ฐํŽ˜์ด์Šค์™€ QueryDSL์„ ์‚ฌ์šฉํ•˜๋Š” DAO ํด๋ž˜์Šค๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

User - Team ๋‘ ๊ฐœ ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์žˆ๊ณ  ๊ฐ๊ฐ Jpa Repository๋ฅผ ๋งŒ๋“  ์ƒํƒœ์—์„œ ๋™์ ์ฟผ๋ฆฌ๋ฅผ QueryDsl๋กœ ์ž‘์„ฑํ•ด๋ณด์ž.


๊ธฐ์กด UserRepository

public interface UserRepository extends JpaRepository<User, Long> {

    Optional<User> findByUsername(String username);
}

์ด Repository ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์ง์ ‘ QueryDsl์„ ๋‹ค๋ฃฐ ์ˆ˜๋Š” ์—†๋‹ค.
JPAQueryFactory๋ฅผ ์ฃผ์ž…๋ฐ›์•„์•ผ ํ•˜๋Š”๋ฐ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋”ฐ๋กœ QueryDsl ์ž‘์—…์„ ์œ„ํ•œ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์ธํ„ฐํŽ˜์ด์Šค์˜ ํŠน์ง•์ธ ๋‹ค์ค‘์ƒ์†์„ ์ด์šฉํ•ด์„œ QueryDsl์„ ์ž‘์—…์„ ์œ„ํ•œ Dao ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ตฌํ˜„์ฒด๋ฅผ ๋งŒ๋“  ํ›„ Dao ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ธฐ์กด Repository๊ฐ€ ์ƒ์†ํ•˜๋„๋ก ๊ตฌํ˜„ํ•  ๊ฒƒ์ด๋‹ค.


๐Ÿ“Œ QueryDsl์„ ์œ„ํ•œ Dao ์ธํ„ฐํŽ˜์ด์Šค, ๊ตฌํ˜„์ฒด ์ž‘์„ฑ

Team์ด๋ฆ„์œผ๋กœ Team์„ ์ฐพ๊ณ  ํ•ด๋‹น Team์˜ ์‹œ๋‹ˆ์–ด(30์‚ด ์ด์ƒ)๋ฅผ ์ฐพ๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.

์ธํ„ฐํŽ˜์ด์Šค ์ •์˜

@Repository
public interface UserDao {

    // ์–ด๋–ค Team์— ์†ํ•œ User ์ค‘ ๋‚˜์ด๊ฐ€ 30์‚ด ์ด์ƒ์ธ User๋ฅผ ์กฐํšŒํ•œ๋‹ค.
    List<UserDto> findSeniorUserByTeam(String teamName);
}

๊ตฌํ˜„์ฒด ์ •์˜

@RequiredArgsConstructor
public class UserDaoImpl implements UserDao {

    private final JPAQueryFactory query;

    @Override
    public List<UserDto> findSeniorUserByTeam(String teamName) {
        return query
                .select(new QUserDto(user.username, user.age, team.teamName))
                .from(user)
                .join(user.team, team)
                .where(teamNameEquals(teamName), userAgeGreaterOrEqualsThan(30))
                .fetch();
    }

    private BooleanExpression teamNameEquals(String teamName) {
        return Objects.isNull(teamName) ? null : team.teamName.eq(teamName);
    }

    private BooleanExpression userAgeGreaterOrEqualsThan(int age) {
        return age < 1 ? null : user.age.goe(age);
    }
}

๐Ÿ“Œ Repository์— ์ƒ์†

public interface UserRepository extends JpaRepository<User, Long>, UserDao {

    Optional<User> findByUsername(String username);
}

๐Ÿ“Œ ํ…Œ์ŠคํŠธ

ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์ปจํŠธ๋กค๋Ÿฌ

@RequiredArgsConstructor
@RestController
public class UserController {

    private final UserRepository userRepository;

    @GetMapping("/user/team/{teamName}/senior")
    public ResponseEntity<List<UserDto>> test(@PathVariable String teamName) {
        List<UserDto> userDtos = userRepository.findSeniorUserByTeam(teamName);
        return ResponseEntity.ok(userDtos);
    }
}



3์ฐจ ํ”„๋กœ์ ํŠธ Querydsl ์ค€๋น„์™„๋ฃŒ ใ…‹ใ…‹..ใ…‹ใ…‹

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€