Giriş
JOOQ Access Layer bağımlılığı eklenir.
Maven Plugin
jooq-codegen plugin eklenir
Maven
Şu satırı dahil ederiz
<dependency><groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jooq</artifactId> </dependency> <dependency> <groupId>org.jooq</groupId> <artifactId>jooq</artifactId> <version>3.17.5</version> </dependency>
application.properties
Örnek
Şöyle yaparız
#PostgreSQL Database Configurationdatasource.driver= org.postgresql.Driverdatasource.jdbcUrl= jdbc:postgresql://localhost:5432/librarydbdatasource.username= devdatasource.password= qwerty# configure spring data sourcespring.application.name=jooq-apispring.datasource.driver-class-name=${datasource.driver}spring.datasource.url = ${datasource.jdbcUrl}spring.datasource.username = ${datasource.username}spring.datasource.password = ${datasource.password}spring.jooq.sql-dialect= postgres
JOOQ iki şekilde kullanılabilir
1. DAO
2. DSLContext
1. DAO
Açıklaması şöyle. Yani her tablo için DAO üretilmiyor. DAO üretilmesi için code generation plugin'e ayar belirtmek lazım
jOOQ generates one DAO per UpdatableRecord, i.e. per table with a single-column primary key. Generated DAOs implement a common jOOQ type called org.jooq.DAO. With this, we don’t need our custom repository interface JOOQRepository.java because generated DAO classes implement various useful CRUD methods.
DAO sınıfı insert(), update(), findAll(), findById(), deleteById() metodlarını sağlar.
Örnek
Şöyle yaparız
RequiredArgsConstructor @Service(value = "BookServiceDAO") public class BookServiceImpl implements BookService { private final BookDao repository; @Override public Book create(Book book) { repository.insert(book); return book; } @Override public Book update(Book book) { repository.update(book); return book; } @Override public List<Book> getAll(){ return repository.findAll(); } @Override public Book getOne(int id) { Book book = repository.findById(id); if(book == null){ throw new DataNotFoundException( MessageFormat.format("Book id {0} not found", String.valueOf(id))); } return book; } @Override public void deleteById(int id) { repository.deleteById(id); } }
2. DSLContext Sınıfı
Örnek
Elimizde şöyle bir arayüz olsun
public interface JOOQRepository<T> { T save(T tablePojo); T update(T tablePojo, int id); List<T> findAll(); Optional<T> findById(int id); boolean deleteById(int id); }
Şöyle yaparız
import org.jooq.DSLContext; import org.springframework.util.ObjectUtils; import static com.example.springbootjooq.model.Tables.BOOK; @RequiredArgsConstructor @Transactional @Repository public class BookRepository implements JOOQRepository<Book> { private final DSLContext context; @Override public Book save(Book book){ BookRecord bookRecord = context.insertInto(BOOK) set(BOOK.DESCRIPTION, book.getDescription()) ... returning(BOOK.ID).fetchOne(); if (bookRecord != null) { book.setId(bookRecord.getId()); return book; } return null; } @Override public Book update(Book book, int id) { BookRecord bookRecord = context.update(BOOK) .set(BOOK.DESCRIPTION, book.getDescription()) ... .where(BOOK.ID.eq(5)) .returning(BOOK.ID).fetchOne(); return (bookRecord != null) ? book : null; } @Override public List<Book> findAll(){ return context .selectFrom(BOOK) .fetchInto(Book.class); } @Override public Optional<Book> findById(int id){ Book b = context.selectFrom(BOOK).where(BOOK.ID.eq(id)).fetchOneInto(Book.class); return (ObjectUtils.isEmpty(b)) ? Optional.empty() : Optional.of(b); } @Override public boolean deleteById(int id) { return context.delete(BOOK) .where(BOOK.ID.eq(id)) .execute() == 1; } }
Hiç yorum yok:
Yorum Gönder