SpringBoot环境下QueryDSL-JPA的使用
2020-12-13 02:14
标签:nat rate sql isnull pac autowire frame findall man Customer.java Employees.java https://www.jianshu.com/p/69dcb1b85bbb SpringBoot环境下QueryDSL-JPA的使用 标签:nat rate sql isnull pac autowire frame findall man 原文地址:https://blog.51cto.com/12012821/24095532、application.yml配置文件
# 数据库基本配置
spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
spring.datasource.username: root
spring.datasource.password: root
spring.datasource.url: jdbc:mysql://192.168.56.1:3306/test01?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
# 数据库方言
spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.MySQL57Dialect
spring.jpa.show-sql: true
spring.jpa.hibernate.ddl-auto: update
# 数据库平台
spring.jpa.database-platform: mysql
# JPA配置
spring.jpa.database: mysql
# 禁用视图
spring.jpa.open-in-view: false
3、实体类
package com.xiaohang.springio.jpastudy.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "t_customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "customer_name")
private String name;
@Column(name = "customer_part")
private String part;
}
package com.xiaohang.springio.jpastudy.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "t_employee")
public class Employees {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "employee_name")
private String name;
@Column(name = "employee_part")
private String part;
}
4、Repository
package com.xiaohang.springio.jpastudy.repository;
import com.xiaohang.springio.jpastudy.entity.Employees;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmployeesRepository extends JpaRepository
package com.xiaohang.springio.jpastudy.repository;
import com.xiaohang.springio.jpastudy.entity.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerRepository extends JpaRepository
5、JPAQueryFactory类的使用重点
package com.xiaohang.springio.jpastudy.service;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xiaohang.springio.jpastudy.entity.Customer;
import com.xiaohang.springio.jpastudy.entity.Employees;
import com.xiaohang.springio.jpastudy.entity.QCustomer;
import com.xiaohang.springio.jpastudy.entity.QEmployees;
import com.xiaohang.springio.jpastudy.repository.CustomerRepository;
import com.xiaohang.springio.jpastudy.repository.EmployeesRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;
import javax.persistence.EntityManager;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BookServiceTest {
// 注入JPAQueryFactory
@Bean
@Autowired
public JPAQueryFactory jpaQuery(EntityManager entityManager) {
return new JPAQueryFactory(entityManager);
}
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Autowired
private EmployeesRepository employeesRepository;
@Autowired
private CustomerRepository customerRepository;
@org.junit.Test
public void jpaQuery() {
Employees employees = new Employees();
employees.setName("老李");
employees.setPart("运营部");
employeesRepository.saveAndFlush(employees);
}
@org.junit.Test
public void updateBook() {
Customer customer = new Customer();
customer.setName("老黄");
customer.setPart("生产部");
customerRepository.saveAndFlush(customer);
}
/**
* 数据库联接查询
*/
@org.junit.Test
public void findAllAuthors() {
QCustomer qc = QCustomer.customer;
QEmployees qe = QEmployees.employees;
// 内联接
List
6、推存阅读及参考
上一篇:小猿圈python之垃圾回收机制
下一篇:修改linux系统显示语言
文章标题:SpringBoot环境下QueryDSL-JPA的使用
文章链接:http://soscw.com/essay/25151.html