Querydsl与SpringBoot集成
Querydsl为⼤多数数据库提供了⼀种基于Java的类型安全,类SQL的查询⽅式。相⽐JPA,Querydsl能提供更加强⼤的查询⽅式,⽐如关联查询。相⽐MyBatis,Querydsl省去了XML⽂件或者注解SQL的⿇烦,直接通过流式API的⽅式进⾏调⽤。
除了⽀持关系型数据库之外,Querydsl同样⽀持⼀下存储
MongoDB
Lucene
Collections
Spatial
JDO
jpa mybatis接下来通过H2+JPA的⽅式介绍Querydsl的基本使⽤(为了演⽰采⽤H2数据库)
⼀、创建项⽬,添加依赖
本⽂采⽤Spring Boot快速搭建项⽬,并在l中添加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>4.3.1</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId&sema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
注意
当在项⽬中使⽤的是JPA的注解( javax.persistence.Entity)的时候,需要使⽤JPAAnnotationProcessor⽣成相关的查询类,如果使⽤的是Hibernate注解的时候,需要将JPAAnnotationProcessor替换成com.querydsl.apt.hibernate.HibernateAnnotationProcessor。
⼆、创建实体类
@Entity
public class User {
@Id
private Integer id;
private String name;
// 省略getter, setter等⽅法
}
@Entity
public class UserInfo {
@Id
private Integer userId;
private Integer sex;
}
实体类创建完成之后,重新编译项⽬
mvn clean package
在target/generated-sources/java⽬录中可以看到编译后⽣成的查询类(Q开头的类,例如QUser, QUserInfo)三、测试
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class QuerydslTests {
@Autowired
private EntityManager entityManager;
private JPAQueryFactory queryFactory;
private QUser qUser;
private QUserInfo qUserInfo;
@BeforeEach
void setUp() {
queryFactory = new JPAQueryFactory(entityManager);
qUser = QUser.user;
qUserInfo = QUserInfo.userInfo;
}
@Test
void simpleQuery() {
User user = queryFactory.selectFrom(qUser)
.where(qUser.name.eq("XiaoMing"))
.fetchOne();
Assertions.assertNotNull(user);
Assertions.assertEquals("XiaoMing", Name());
}
@Test
void simpleQueryFields() {
String name = queryFactory.selectFrom(qUser)
.
select(qUser.name)
.where(qUser.name.eq("XiaoMing"))
.fetchOne();
Assertions.assertNotNull(name);
Assertions.assertEquals("XiaoMing", name);
}
@Test
void join() {
Tuple tuple = queryFactory.selectFrom(qUser)
.select(qUser.id, qUser.name, qUserInfo.sex)
.innerJoin(qUserInfo).on(qUserInfo.userId.eq(qUser.id))
.
where(qUser.name.eq("XiaoMing"))
.fetchOne();
Assertions.assertNotNull(tuple);
Assertions.assertNotEquals(0, tuple.size());
Integer userId = (0, Integer.class);
String name = (1, String.class);
Integer sex = (2, Integer.class);
Assertions.assertEquals(1, userId);
Assertions.assertEquals("XiaoMing", name);
Assertions.assertEquals(0, sex);
}
}
最后附上数据库初始化脚本
insert into `user` (id, name) values (1, 'XiaoMing');
insert into `user` (id, name) values (2, 'XiaoHong');
insert into `user_info` (user_id, sex) values (1, 0);
insert into `user_info` (user_id, sex) values (2, 1);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论