Source code for Intro to QueryDSL article.
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package org.baeldung.dao;
|
||||
|
||||
import org.baeldung.entity.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
public Person save(Person person);
|
||||
|
||||
public List<Person> findPersonsByFirstnameQueryDSL(String firstname);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.baeldung.dao;
|
||||
|
||||
import com.mysema.query.jpa.impl.JPAQuery;
|
||||
import org.baeldung.entity.Person;
|
||||
import org.baeldung.entity.QPerson;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public class PersonDaoImpl implements PersonDao {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
public Person save(Person person) {
|
||||
em.persist(person);
|
||||
return person;
|
||||
}
|
||||
|
||||
public List<Person> findPersonsByFirstnameQueryDSL(String firstname) {
|
||||
JPAQuery query = new JPAQuery(em);
|
||||
QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).where(person.firstname.eq(firstname)).list(person);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.baeldung.entity;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column
|
||||
private String firstname;
|
||||
|
||||
@Column
|
||||
private String surname;
|
||||
|
||||
Person() {
|
||||
}
|
||||
|
||||
public Person(String firstname, String surname) {
|
||||
this.firstname = firstname;
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstname() {
|
||||
return firstname;
|
||||
}
|
||||
|
||||
public void setFirstname(String firstname) {
|
||||
this.firstname = firstname;
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public void setSurname(String surname) {
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user