Spring Boot整合QueryDSL的实现示例

之前研究Jooq,今天来研究一下搭配JPA的QueryDSL吧。

简介

Querydsl 是一个Java开源框架用于构建类型安全的SQL查询语句。它采用API代替拼凑字符串来构造查询语句。可跟 Hibernate 和 JPA 等框架结合使用。

新建Spring Boot项目

。。。还说啥?

1. pom.xml


    
      org.springframework.boot
      spring-boot-starter-data-jpa
    
    
    
      org.springframework.boot
      spring-boot-starter-web
    
 
    
      mysql
      mysql-connector-java
      runtime
    
 
    
    
      com.querydsl
      querydsl-apt
      provided
    
    
    
    
      com.querydsl
      querydsl-jpa
    
 
    
      org.springframework.boot
      spring-boot-starter-test
      test
      
        
          org.junit.vintage
          junit-vintage-engine
        
      
    
  
 
  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
	  
      
        com.mysema.maven
        apt-maven-plugin
        1.1.3
        
          
            
              process
            
            
              target/generated-sources/java
              com.querydsl.apt.jpa.JPAAnnotationProcessor
            
          
        
      
    
  

2. application.yml

server:
 port: 8888
spring:
 datasource:
  url: jdbc:mysql://IP地址:3306/querydsl?characterEncoding=UTF-8
  username: 用户名
  password: 密码
 jpa:
  show-sql: true # 控制台打印SQL
  hibernate:
   ddl-auto: update

3. JPAQueryFactory Bean

/**
 * JPAQueryFactory Bean
 * @author zhouzhaodong
 */
@Configuration
public class PeopleQueryConfig {
  @Bean
  public JPAQueryFactory jpaQuery(EntityManager entityManager) {
    return new JPAQueryFactory(entityManager);
  }
}

4. 新建实体类

/**
 * 实体类
 * @author zhouzhaodong
 */
@Entity
@Table(name="people")
public class People {
 
  @Id
  @Column(name="id")
  @GeneratedValue
  private Integer id;
 
  @Column(name="name")
  private String name;
 
  @Column(name="age")
  private Integer age;
 
  @Column(name="address")
  private String address;
 
  public Integer getId() {
    return id;
  }
 
  public void setId(Integer id) {
    this.id = id;
  }
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
 
  public Integer getAge() {
    return age;
  }
 
  public void setAge(Integer age) {
    this.age = age;
  }
 
  public String getAddress() {
    return address;
  }
 
  public void setAddress(String address) {
    this.address = address;
  }
}

5. 新建service

/**
 * service
 * @author zhouzhaodong
 */
public interface PeopleService {
 
  /**
   * 根据id查询
   * @param id
   * @return
   */
  People selectOne(Integer id);
 
}

6. 新建serviceImpl

/**
 * serviceImpl
 * @author zhouzhaodong
 */
@Service
public class PeopleServiceImpl implements PeopleService {
 
  @Resource
  JPAQueryFactory queryFactory;
 
  @Override
  public People selectOne(Integer id) {
    QPeople people = QPeople.people;
    return queryFactory.selectFrom(people).where(people.id.eq(id)).fetchOne();
  }
}

7. 新建controller

/**
 * controller
 * @author zhouzhaodong
 */
@RestController
public class PeopleController {
 
  @Resource
  PeopleService peopleService;
 
  @RequestMapping("/selectById")
  public People selectById(){
    return peopleService.selectOne(0);
  }
 
}

8. 新建repository,可以使用JPA原有的功能

/**
 * 实现jpaRepository
 * @author zhouzhaodong
 */
public interface PeopleRepository extends JpaRepository {
}

9. postman测试

数据库数据如下:

Spring Boot整合QueryDSL的实现示例_第1张图片

请求结果:

Spring Boot整合QueryDSL的实现示例_第2张图片 

Github代码地址:https://github.com/zhouzhaodong/springboot/tree/master/querydsl

到此这篇关于Spring Boot整合QueryDSL的实现示例的文章就介绍到这了,更多相关SpringBoot整合QueryDSL内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Spring Boot整合QueryDSL的实现示例)