SpringJPA的基本使用

csdn

QueryDSL——比SpringData JPA更面向对象 - 掘金 (juejin.cn)

Spring Boot (六): 为 JPA 插上翅膀的 QueryDSL - 极客挖掘机 - 博客园 (cnblogs.com)

简介

脑图

image-20240121190043418

注解

@Entity(name=“xxx”)

用与实体类上面,建立与表的映射

@Id

主键属性

@Column(name=“xxx”)

建立属性与字段的映射,当属性名与字段名一致时会自动映射

image-20240121190111860

@Query

image-20240121190136885

?号后面的数字对应方法的参数列表顺序

image-20240121190158191

接口

image-20240121190218962

JpaRepository

image-20240121190250498

分页

image-20240121190321802

排序

Sort

image-20240121190336551

动态查询

image-20240121190408561

springboot使用querydsl
<dependencies>

  <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId>

  </dependency>

 

  <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>

  </dependency>

 

 

  <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId>

  </dependency>

 

  <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId>

  </dependency>

 

  <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.22</version>

  </dependency>

 

  <dependency><groupId>com.querydsl</groupId><artifactId>querydsl-jpa</artifactId>

  </dependency>

  <dependency><groupId>com.querydsl</groupId><artifactId>querydsl-apt</artifactId><scope>provided</scope>

  </dependency>

 

 

 

 

</dependencies>

 

<build>

  <plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>com.mysema.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>

关联查询

一对一

image-20240121190442562

@OneToOne参数

image-20240121190455893

多对多
querydsl
/**

 \* dsl条件查询选修了数学并且年龄18岁以上的学生姓名

 */

@Test

@Transactional

void dsl1(){

  QStudent student = QStudent.student;

  QSubject subject = QSubject.subject;

  List<String> fetch = jpaQueryFactory.select(student.name).from(student).innerJoin(student.subjects,subject)

​          .where(student.age.gt(18).and(subject.subjectName.eq("化学"))).fetch();

  fetch.forEach(e-> System.out.println(e));

 

}
动态查询
public List<LessonModel> findLessonDynaList(String name, Date startDate, String address, String userId) throws ParseException {

​    QLessonModel lessonModel = QLessonModel.lessonModel;

​    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

 

​    // 动态查询示例

​    BooleanBuilder builder = new BooleanBuilder();

 

​    if (!StringUtils.isEmpty(name)){

​      builder.and(lessonModel.name.like("%" + name + "%"));

​    }

 

​    if (startDate != null) {

​      builder.and(lessonModel.startDate.between(simpleDateFormat.parse("2018-12-31 00:00:00"), new Date()));

​    }

 

​    if (!StringUtils.isEmpty(address)) {

​      builder.and(lessonModel.address.contains(address));

​    }

 

​    if (!StringUtils.isEmpty(userId)) {

​      builder.and(lessonModel.userId.eq(userId));

​    }

 

​    return queryFactory.selectFrom(lessonModel).where(builder).fetch();

  }

bug记录

image-20240121190551489

出现这个错误的原因是在接口自定义方法中,定义了不符合规范的方法

image-20240121190607278

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值