Querydsl 简单语句格式




    com.querydsl
    querydsl-jpa


    com.querydsl
    querydsl-apt




    
        
        
            com.mysema.maven
            apt-maven-plugin
            1.1.3
            
                
                    
                        process
                    
                    
                        target/generated-sources/java
                        com.querydsl.apt.jpa.JPAAnnotationProcessor
                    
                
            
        
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    

 

 进行注入 EntityManager   和 事物管理:

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }


    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
        return new JPAQueryFactory(entityManager);
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

}
# 子查询

QDepartment department = QDepartment.department;
QDepartment d = new QDepartment("d");
queryFactory.selectFrom(department)
    .where(department.size.eq(
        JPAExpressions.select(d.size.max()).from(d)))
     .fetch();


QEmployee employee = QEmployee.employee;
QEmployee e = new QEmployee("e");
queryFactory.selectFrom(employee)
    .where(employee.weeklyhours.gt(
        JPAExpressions.select(e.weeklyhours.avg())
            .from(employee.department.employees, e)
            .where(e.manager.eq(employee.manager))))
    .fetch();



# 更新修改

QCustomer customer = QCustomer.customer;
queryFactory.update(customer)
    .set(customer.name, "Bobby")
    .where(customer.name.eq("Bob"))
    .execute();

queryFactory.update(customer)
    .setNull(customer.name)
    .where(customer.name.eq("Bob"))
    .execute();



# 删除

QCustomer customer = QCustomer.customer;
queryFactory.delete(customer).execute();
queryFactory.delete(customer).where(customer.level.lt(3)).execute();



# 返回为 Tuple 类型的对象取值

# 1.
public List getOrdersListOnlinePaymentUnmatch() {
        QOrders qOrders = QOrders.orders;
        return hibernateQueryFactory
                .select(qOrders.ordersSn, qOrders.ordersAmount, qOrders.storeDiscountAmount, qOrders.couponAmount, qOrders.paymentTime)
                .from(qOrders)
                .fetch();
    }

# 2.
List orders = ordersDao.getOrdersListOnlinePaymentUnmatch();

# 3.获取第1个参数
orders.get(i).get(0, Long.class)

DTO

query = new JPASQLQuery(entityManager, templates);
List catDTOs = query.select(Projections.constructor(CatDTO.class, cat.id, cat.name))
    .from(cat)
    .orderBy(cat.name.asc())
    .fetch();


public ServiceResponse selectCollection() {
        User user = ShiroKit.getUser();
        QProduct qProduct = QProduct.product;
        QCollection qCollection = QCollection.collection;
        List list = jpaQueryFactory
                .select(Projections.bean(ProductCollectionVo.class,
                        qProduct.id,
                        qProduct.productName,
                        qProduct.productType,
                        qProduct.productDetails,
                        qProduct.produceBrand,
                        qProduct.produceMoney,
                        qProduct.produceBigimg,
                        qProduct.produceSmallimg,
                        qProduct.produceDetailsimg,
                        qCollection.id.as("collectionId")))
                .from(qProduct)
                .rightJoin(qCollection).on(qProduct.id.eq(qCollection.productId))
                .where(qCollection.userId.eq(user.getId())).fetch();
        return ServiceResponse.createBySuccess(list);


# 动态条件拼接查询
public void test() throws IOException {
        QCeshi qCeshi = QCeshi.ceshi;
        Predicate predicate = null;
        predicate = ExpressionUtils.and(predicate, qCeshi.name.eq("张三"));
        predicate = ExpressionUtils.and(predicate, qCeshi.id.eq(3));
        JPAQuery where = jpaQueryFactory.selectFrom(qCeshi).where(predicate);

}
# 执行语句为
select ceshi from Ceshi ceshi where ceshi.name = ?1 and ceshi.id = ?2



NE : not equal 不等于 .ne
GE : greater than or equal 大于等于 .goe
LE : less than or equal 小于等于 .loe
GT : greater than 大于 .gt
LT : less than  小于 .lt
EQ : equal 等于 .eq
 
  

 

你可能感兴趣的:(Querydsl)