QueryDSL的常用方法

零、遗漏

1、

@ContextConfiguration(classes = SpringDataJPAConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)

 

一、原生态查询

1、注入EntityManager使用@PersistenceContext自动装配(这个注解可以保证线程安全)

@PersistenceContext
EntityManager em;

2、引入JPAQueryFactory对象

public class testQueryDSL {
    @PersistenceContext
    EntityManager em;

    @Test
    public void testQueryDsl(){
        JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(em);
        
        jpaQueryFactory.select()
                .from()
                .where()
                .orderBy();
    }
}

 

3、传入Q类对象,and单列查询

public class testQueryDSL {
    @PersistenceContext
    EntityManager em;

    @Test
    public void testQueryDsl(){
        JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(em);

        QAuditLog auditLog=QAuditLog.auditLog;
//EventAction是实体类属性,既是表的字段 JPAQuery
<EventAction> eventActionJPAQuery = jpaQueryFactory.select(auditLog.action) .from(auditLog) .where(auditLog.id.eq("0a8f775824e843729b646b54ec212f35")) .orderBy(auditLog.id.desc()); } }

4、查询所有列:直接在.select()方法中传入对应的Q类

public class testQueryDSL {
    @PersistenceContext
    EntityManager em;

    @Test
    public void testQueryDsl(){
        JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(em);

        QAuditLog auditLog=QAuditLog.auditLog;

        JPAQuery<AuditLog> logJPAQuery = jpaQueryFactory.select(auditLog)
                .from(auditLog)
                .where(auditLog.id.eq("0a8f775824e843729b646b54ec212f35"))
                .orderBy(auditLog.id.desc());
    }
}

5、查询两列及以上但不是全部列,此时系统会自动生成一个自定义的Tuple对象,去包含查询的列

public class testQueryDSL {
    @PersistenceContext
    EntityManager em;

    @Test
    public void testQueryDsl(){
        JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(em);

        QAuditLog auditLog=QAuditLog.auditLog;

        JPAQuery<Tuple> tupleJPAQuery = jpaQueryFactory.select(auditLog.id, auditLog.message)
                .from(auditLog)
                .where(auditLog.id.eq("0a8f775824e843729b646b54ec212f35"))
                .orderBy(auditLog.id.desc());
    }
}

6、获取查询结果

@ContextConfiguration(classes = SpringDataJPAConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class testQueryDSL {

    @PersistenceContext
    private EntityManager em;

    @Test
    public void testQueryDsl(){
        JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(em);

        QAuditLog auditLog=QAuditLog.auditLog;

        JPAQuery<Tuple> tupleJPAQuery = jpaQueryFactory.select(auditLog.id, auditLog.message)
                .from(auditLog)
                .where(auditLog.id.eq("297e7f73c5e04613ac5fa86cc793f539"))
                .orderBy(auditLog.id.desc());

        List<Tuple> fetch = tupleJPAQuery.fetch();

        System.out.println(fetch);
    }
}

 

7、获取Tuple对象里的查询出来的属性

@ContextConfiguration(classes = SpringDataJPAConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class testQueryDSL {

    @PersistenceContext
    private EntityManager em;

    @Test
    public void testQueryDsl(){
        JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(em);

        QAuditLog auditLog=QAuditLog.auditLog;

        JPAQuery<Tuple> tupleJPAQuery = jpaQueryFactory.select(auditLog.id, auditLog.message)
                .from(auditLog)
                .where(auditLog.id.eq("297e7f73c5e04613ac5fa86cc793f539"))
                .orderBy(auditLog.id.desc());

        List<Tuple> fetch = tupleJPAQuery.fetch();

        for (Tuple tuple : fetch) {
            System.out.println(tuple.get(auditLog.id));
            System.out.println(tuple.get(auditLog.message));
        }
    }
}

8、智能判断查询出的数据的类型

@ContextConfiguration(classes = SpringDataJPAConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class testQueryDSL {

    @PersistenceContext
    private EntityManager em;

    @Test
    public void testQueryDsl(){
        JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(em);

        QAuditLog auditLog=QAuditLog.auditLog;
JPAQuery<Long> longJPAQuery = jpaQueryFactory.select(auditLog.message.count())

 


                .from(auditLog)
                .where(auditLog.id.eq("297e7f73c5e04613ac5fa86cc793f539"))
                .orderBy(auditLog.id.desc());

        System.out.println(longJPAQuery.fetch());
    }
}

字段类型是数字可以使用.sun()

JPAQuery<Long> longJPAQuery = jpaQueryFactory.select(auditLog.id.sum())

 

posted on 2022-10-31 09:56  影子BLH  阅读(822)  评论(0)    收藏  举报