java jpa如何自定义sql语句

作者:java小白- 时间:2022-08-04 14:36:52 

java jpa自定义sql语句

本篇只是为了再次记录自己又学习了jpa的使用,框架原生的通过解析方法名多适用于单表操作,自定义的sql查询则可以解决所有问题,记录些自定义sql语法的记录,以便后续参照。

1.多表关联查询,含条件


@Query(value = "SELECT b FROM QyVideo a JOIN YjQyXx b ON  a.qyId = b.id AND a.cameraId = ?1 ")

2.清空表


@Transactional
@Modifying
@Query(value = "truncate table yj_qy_xx", nativeQuery = true)

注:update、truncate或delete时必须使用@Modifying和@Transactional对方法进行注解,才能使得ORM知道现在要执行的是写操作。

3.模糊查询


@Query("select p from WhpzxryzsXxPo p where p.ryxm like concat('%',?1,'%') and p.cyyxqq >= ?2")

4.查询结果为VO

含两个实体类


@Query(value = "SELECT new com.kun.aqsczt.vo.FxjzfbVo(u, seventinfo) FROM SSmsInfo u left join SEventInfo seventinfo on u.referId = seventinfo.eventId WHERE (:referType IS NULL OR :referType IS '' OR u.referType = :referType) AND (:isSend IS NULL OR :isSend IS '' OR u.isSend = :isSend) ")

5.使用@Param注解注入参数

分页查询


@Query(value = "SELECT a  FROM CEiWorkaccMaybe a " +
           "WHERE (:psnName IS NULL OR :psnName IS '' OR a.psnName LIKE %:psnName%) " +
           "AND (:commName IS NULL OR :commName IS '' OR a.commName LIKE %:commName%) " +
           "AND (:idCard IS NULL OR :idCard IS '' OR a.idCard LIKE %:idCard%) " +
           "AND (:doctorDateStart IS NULL OR :doctorDateStart IS '' OR a.doctorDate >= :doctorDateStart) " +
           "AND (:doctorDateEnd IS NULL OR :doctorDateEnd IS '' OR a.doctorDate <= :doctorDateEnd) "
   )
Page<CEiWorkaccMaybe> getSuspectedWorkAccidentVerification(
           @Param("psnName") String psnName,
           @Param("commName") String commName,
           @Param("idCard") String idCard,
           @Param("doctorDateStart") String doctorDateStart,
           @Param("doctorDateEnd") String doctorDateEnd,
           Pageable pageable
   );

无非是把日常的sql中的表名换成了对应的实体类名,接收参数适用 ?加上第几个参数的几。当然也可使用@Param注解注入参数,就变成了使用 :参数 名称接收。

jpa自定义sql查询结果

很多时候都会遇到自定义sql,自定义返回字段,而不是pojo类。这个情况要通过接口定义返回。

直接上代码


@Query(value = "select m.field AS field,COUNT(m.field) AS size from MigrationObject m where m.xmlName = ?1 and m.groupName = ?2 group by m.field")
   List<WorkCenter> getKey(String xmlName, String groupName);

对于这种情况,只返回了两个字段,就需要定义一个接口来接收(注意AS别名的配置)


public interface WorkCenter {
   String getField();
   String getSize();
}

最后跑一下demo代码


  List<WorkCenter> list = migrationObjectRepository.getKey("EN_Work centerResource.xml","Key");
       for (WorkCenter workCenter:list){
           System.out.println(workCenter.getField());
           System.out.println(workCenter.getSize());
       }

ARBPL
5
SPRAS
2
CANUM
2
ENDDA
1
WERKS
5

来源:https://blog.csdn.net/weixin_42209368/article/details/119649029

标签:java,jpa,自定义,sql
0
投稿

猜你喜欢

  • Android RecyclerView加载不同布局简单实现

    2022-07-15 16:57:09
  • 分析那些不讲武德的SDK(构造使用规范)

    2023-05-21 00:36:22
  • RocketMQ NameServer架构设计启动流程

    2023-12-13 07:40:01
  • SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)推荐

    2023-01-21 13:58:45
  • 举例解析Java的设计模式编程中里氏替换原则的意义

    2021-06-30 18:37:17
  • 两个surfaceView实现切换效果

    2021-10-04 15:18:52
  • mybatis使用foreach查询不出结果也不报错的问题

    2023-11-24 22:36:17
  • 使用自定义注解+springAop实现参数非空校验方式

    2023-09-21 00:13:22
  • Android中Listview点击item不变颜色及设置listselector 无效的解决方案

    2022-07-04 23:49:10
  • Spring Boot2如何构建可部署的war包

    2023-11-29 06:40:59
  • SpringBoot自动配置深入探究实现原理

    2023-08-06 09:59:58
  • 如何利用java控制鼠标操作一些重复的事情

    2021-11-12 12:54:02
  • 详解Java中while和do-while循环、break的使用

    2022-10-24 13:37:04
  • C# 常用日期时间函数(老用不熟)

    2021-08-21 10:12:18
  • C#在图片增加文字的实现代码

    2023-03-30 03:26:24
  • 极简的Resty服务端和客户端RESTful框架

    2022-01-19 19:51:00
  • 解决Java原生压缩组件不支持中文文件名乱码的问题

    2021-07-29 22:39:38
  • protobuf与json转换小结

    2023-01-23 15:26:34
  • Scala异常处理的方法深入分析

    2022-01-09 19:50:35
  • 你知道jdk竟有4个random吗

    2022-06-14 23:37:08
  • asp之家 软件编程 m.aspxhome.com