728x90
반응형

본 프로젝트는 Querydsl, SpringDataJpa 둘 다 사용 가능하게끔 설계했습니다.

 

1. 파일 만들기

2. User DAO

backend/module/user/User

package web.backend.module.user;

import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@Getter
@NoArgsConstructor
@Table(name="user_info")
public class User {

    @Id @GeneratedValue
    @Column(name="user_index")
    private Long id;

    @Column(name="user_id")
    private String userId;

    @CreatedDate
    @Column(name="user_createdat", updatable = false)
    private LocalDateTime user_creadtedat;

    @LastModifiedDate
    @Column(name="user_updatedat")
    private LocalDateTime user_updatedat;

    @PrePersist
    public void prePersist() {
        LocalDateTime now = LocalDateTime.now();
        user_creadtedat = now;
        user_updatedat = now;
    }

    @PreUpdate
    public void preUpdate() {
        user_updatedat = LocalDateTime.now();
    }

    public User(String userId) {
        this.userId = userId;
    }

    /**
     * user 수정
     */
    public void changeUser(String userId) {
        this.userId = userId;
    }

}

 

3. UserController

backend/module/user/UserController

package web.backend.module.user;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

}

 

4. UserService

backend/module/user/UserController

package web.backend.module.user;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import web.backend.module.user.repository.UserQueryRepository;
import web.backend.module.user.repository.UserSpringJpaRepository;

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class UserService {

    private final UserQueryRepository userQueryRepository;
    private final UserSpringJpaRepository userSpringJPARepository;

}

 

5. UserQueryRepository

backend/module/user/repository/UserQueryRepository

package web.backend.module.user.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;

@Repository
@Slf4j
public class UserQueryRepository {

    private final JPAQueryFactory query;

    public UserQueryRepository(EntityManager em) {
        this.query = new JPAQueryFactory(em);
    }
}

 

6. UserSpringJpaRepository

backend/module/user/repository/UserSpringJpaRepository

package web.backend.module.user.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import web.backend.module.user.User;

import java.util.Optional;

public interface UserSpringJpaRepository extends JpaRepository<User, Long> {

    Optional<User> findByUserId(String userId);
}

 

728x90
반응형

+ Recent posts