SpringBoot整合SpringDataJpa QueryDSL和原生态SQL

1.导入依赖



    4.0.0

    com.jmj
    springDataJPA-Springboot
    1.0-SNAPSHOT
    
        8
        8
        UTF-8
        4.4.0
        1.1.3
        
        2.5.5
    
    
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
        
            com.alibaba
            druid-spring-boot-starter
            1.2.6
        
        
            org.projectlombok
            lombok
        
        
            com.querydsl
            querydsl-jpa
            ${querydsl.version}
        
    

    
    
        
            
                com.mysema.maven
                apt-maven-plugin
                ${apt.version}
                
                    
                        com.querydsl
                        querydsl-apt
                        ${querydsl.version}
                    
                
                
                    
                        generate-sources
                        
                            process
                        
                        
                            target/generated-sources/queries
                            com.querydsl.apt.jpa.JPAAnnotationProcessor
                            true
                        
                    
                
            
        
    



2.mapper

package com.jmj.springDataApp.mapper;

import com.jmj.springDataApp.pojo.Student;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface StudentDSLMapper extends PagingAndSortingRepository , QuerydslPredicateExecutor {
}

3.test

package com.jmj.springDataApp.mapper;

import com.jmj.springDataApp.pojo.QStudent;
import com.jmj.springDataApp.pojo.Student;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Visitor;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Nullable;

import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class StudentDSLMapperTest {

    @Autowired
    private StudentDSLMapper mapper;

    @Test
    void test1() {
        QStudent student = QStudent.student;
        BooleanExpression eq = student.id.eq(7L);


        Iterable all = mapper.findAll(eq);
        System.out.println(all);

    }

    /**
     * 查询名称范围(in)
     * id>大于
     * 地址精确
     *
     */
    @Test
    void name() {
        QStudent student = QStudent.student;
        BooleanExpression a = student.id.in(3, 7, 9).and(student.name.startsWith("a")).and(student.grade.gt(0));
        Iterable all = mapper.findAll(a);
        System.out.println(all);

    }
}

原生态

package com.jmj.springDataApp.mapper;

import com.jmj.springDataApp.pojo.QStudent;
import com.jmj.springDataApp.pojo.Student;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import java.util.List;

@SpringBootTest
public class EntityManagerTest {

    @PersistenceContext
    private EntityManager em;

    @Test
    void test1() {
        JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(em);
        QStudent student = QStudent.student;
        JPAQuery b = jpaQueryFactory.select(student.id, student.name, student.grade)
                .from(student)
                .where(student.id.gt(3), student.name.contains("b"))
                .orderBy(student.id.desc())
                .limit(3);

        List fetch = b.fetch();
        for (Tuple tuple : fetch) {
            Long aLong = tuple.get(student.id);
            String s = tuple.get(student.name);
            Integer integer = tuple.get(student.grade);
            System.out.println(aLong+s+integer);
        }
    }


    @Test
    void test2() {
        JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(em);
        QStudent student = QStudent.student;
        JPAQuery b = jpaQueryFactory.select(student.id.sum())
                .from(student)
                .where(student.id.gt(3), student.name.contains("b"))
                .orderBy(student.id.desc())
                .limit(3);

        List fetch = b.fetch();
        for (Long aLong : fetch) {
            System.out.println(aLong);
        }

        }

}

你可能感兴趣的:(SpringBoot,spring,boot,后端,java)