SpringData如何通过@Query注解支持JPA语句和原生SQL语句

作者:qq_36722039 时间:2022-08-26 22:07:29 

通过@Query注解支持JPA语句和原生SQL语句

在SpringData中们可是使用继承接口直接按照规则写方法名即可完成查询的方法,不需要写具体的实现,但是这样写又是不能满足我们的需求,比如子查询,SpringData中提供了@Query注解可以让我们写JPA的语句和原生的SQL语句,那接下来看看怎么写JPA的查询语句和原生的SQL语句。


package com.springdata.study.repository;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import com.springdata.study.entitys.Person;

//1.实际上Repository是一个口接口,没有提供任何方法,是一个标记接口
//2.实现了Repository接口就会被spring IOC容器识别为Repository Bean
//  会被纳入IOC容器中
//3.Repository接口也可以同@RepositoryDefinition 注解代替,效果是一样的
//4.接口中的泛型:第一个是那个实体类的Repository,第二个是实体类的主键的类型
//@RepositoryDefinition(domainClass=Person.class,idClass=Integer.class)

/**
* 在Repository接口中申明方法 1.申明方法需要符合一定的规范 2.查询方法需要以 find | read | get开头 3.涉及查询条件时
* 需要用条件关键字连接 4.属性首字母大写 5.支持级联属性
* 6.AddressId若当前实体类中有属性,则优先使用该属性,若想要使用级联属性,需要用下划线隔开Address_Id
*/
public interface PersonRepositoiry extends Repository<Person, Integer> {
// select p from Person where p.name = ?
Person getByName(String name);

List<Person> findByNameStartingWithAndIdLessThan(String name, Integer id);

// where name like %? and id < ?
List<Person> findByNameEndingWithAndIdLessThan(String name, Integer id);

// where email in ? age < ?
List<Person> readByEmailInOrAgeLessThan(List<String> emails, int age);

// 级联属性查询
// where address.id > ?
List<Person> findByAddress_IdGreaterThan(Integer is);

// 可以使用@Query注解在其value属性中写JPA语句灵活查询
@Query("SELECT p FROM Person p WHERE p.id = (SELECT max(p2.id) FROM Person p2)")
Person getMaxIdPerson();

// 在@Query注解中使用占位符
@Query(value = "SELECT p FROM Person p where p.name = ?1 and p.email = ?2")
List<Person> queryAnnotationParam1(String name, String email);

// 使用命名参数传递参数
@Query(value = "SELECT p FROM Person p where p.name = :name")
List<Person> queryAnnotationParam2(@Param("name") String name);

// SpringData可以在参数上添加%
@Query("SELECT p FROM Person p WHERE p.name LIKE %?1%")
List<Person> queryAnnotationLikeParam(String name);

// SpringData可以在参数上添加%
@Query("SELECT p FROM Person p WHERE p.name LIKE %:name%")
List<Person> queryAnnotationLikeParam2(@Param("name")String name);

//在@Query注解中添加nativeQuery=true属性可以使用原生的SQL查询
@Query(value="SELECT count(*) FROM jpa_person", nativeQuery=true)
long getTotalRow();
}

下面是这个类的测试类


package com.springdata.study.test;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springdata.study.entitys.Person;
import com.springdata.study.repository.PersonRepositoiry;
import com.springdata.study.service.PersonService;

public class DataSourceTest {
private ApplicationContext applicationContext;
private PersonService personService;
private PersonRepositoiry personRepositoiry;

{
 applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 personRepositoiry = applicationContext.getBean(PersonRepositoiry.class);
 personService = applicationContext.getBean(PersonService.class);
}

@SuppressWarnings("resource")
@Test
public void testDataSource() throws SQLException {
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 DataSource dataSource = applicationContext.getBean(DataSource.class);
 System.out.println(dataSource.getConnection());
}

@Test
public void testSpringdata() {
 Object person = personService.getPerson("LQF");
 System.out.println(person);
}

@Test
public void testFindByNameStartingWithAndIdLessThan() {
 List<Person> persons = personRepositoiry.findByNameStartingWithAndIdLessThan("g", 6);
 persons = personRepositoiry.findByNameEndingWithAndIdLessThan("g", 6);
 System.out.println(persons);
}

@Test
public void testReadByEmailInOrAgeLessThan() {
 List<Person> persons = personRepositoiry.readByEmailInOrAgeLessThan(Arrays.asList("123@qq.com"), 25);
 System.out.println(persons);
}

@Test
public void testFindByAddressIdGreaterThan() {
 personRepositoiry.findByAddress_IdGreaterThan(1);
}

@Test
public void testGetMaxIdPerson() {
 Person person = personRepositoiry.getMaxIdPerson();
 System.out.println(person);
}

@Test
public void testQueryAnnotationParam() {
 List<Person> persons = personRepositoiry.queryAnnotationParam1("liqingfeng", "123@qq.com");
 System.out.println(persons);
}

@Test
public void testQueryAnnotationParam2() {
 List<Person> persons = personRepositoiry.queryAnnotationParam2("lqf");
 System.out.println(persons);
}

@Test
public void testQueryAnnotationLikeParam() {
 List<Person> persons = personRepositoiry.queryAnnotationLikeParam2("li");
 System.out.println(persons);
}

@Test
public void testGetTotalRow() {
 long count = personRepositoiry.getTotalRow();
 System.out.println(count);
}
}

@Query注解的用法(Spring Data JPA)

1.一个使用@Query注解的简单例子


@Query(value = "select name,author,price from Book b where b.price>?1 and b.price<?2")
List<Book> findByPriceRange(long price1, long price2);

2.Like表达式


@Query(value = "select name,author,price from Book b where b.name like %:name%")
List<Book> findByNameMatch(@Param("name") String name);

3.使用Native SQL Query

所谓本地查询,就是使用原生的sql语句(根据数据库的不同,在sql的语法或结构方面可能有所区别)进行查询数据库的操作。


@Query(value = "select * from book b where b.name=?1", nativeQuery = true)
List<Book> findByName(String name);

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


@Query(value = "select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price")
List<Book> findByNamedParam(@Param("name") String name, @Param("author") String author,
       @Param("price") long price);

5.SPEL表达式(使用时请参考最后的补充说明)

'#{#entityName}'值为'Book'对象对应的数据表名称(book)。


public interface BookQueryRepositoryExample extends Repository<Book, Long>{
      @Query(value = "select * from #{#entityName} b where b.name=?1", nativeQuery = true)
      List<Book> findByName(String name);
}

6.一个较完整的例子


public interface BookQueryRepositoryExample extends Repository<Book, Long> {
   @Query(value = "select * from Book b where b.name=?1", nativeQuery = true)
   List<Book> findByName(String name);// 此方法sql将会报错(java.lang.IllegalArgumentException),看出原因了吗,若没看出来,请看下一个例子
   @Query(value = "select name,author,price from Book b where b.price>?1 and b.price<?2")
   List<Book> findByPriceRange(long price1, long price2);
   @Query(value = "select name,author,price from Book b where b.name like %:name%")
   List<Book> findByNameMatch(@Param("name") String name);
   @Query(value = "select name,author,price from Book b where b.name = :name AND b.author=:author AND b.price=:price")
   List<Book> findByNamedParam(@Param("name") String name, @Param("author") String author,
           @Param("price") long price);
}

7.解释例6中错误的原因

因为指定了nativeQuery = true,即使用原生的sql语句查询。使用java对象'Book'作为表名来查自然是不对的。只需将Book替换为表名book。


@Query(value = "select * from book b where b.name=?1", nativeQuery = true)
List<Book> findByName(String name);

补充说明:

有同学提出来了,例子5中用'#{#entityName}'为啥取不到值啊?

先来说一说'#{#entityName}'到底是个啥。从字面来看,'#{#entityName}'不就是实体类的名称么,对,他就是。

实体类Book,使用@Entity注解后,spring会将实体类Book纳入管理。默认'#{#entityName}'的值就是'Book'。

但是如果使用了@Entity(name = "book")来注解实体类Book,此时'#{#entityName}'的值就变成了'book'。

到此,事情就明了了,只需要在用@Entity来注解实体类时指定name为此实体类对应的表名。在原生sql语句中,就可以把'#{#entityName}'来作为数据表名使用。

来源:https://blog.csdn.net/qq_36722039/article/details/81124382

标签:SpringData,@Query注解,JPA,原生SQL
0
投稿

猜你喜欢

  • 前端如何调用后端接口进行数据交互详解(axios和SpringBoot)

    2023-10-17 02:48:43
  • 详解C语言结构体中的函数指针

    2023-11-21 13:04:33
  • Spring AOP如何整合redis(注解方式)实现缓存统一管理详解

    2023-11-19 06:09:27
  • Java反射机制基础详解

    2023-07-17 04:36:29
  • Android Flutter实现有趣的页面滚动效果

    2023-02-14 06:58:10
  • Java数据类型的全面剖析

    2022-07-09 06:51:38
  • spring中向一个单例bean中注入非单例bean的方法详解

    2022-07-19 13:14:18
  • JavaEE中struts2实现文件上传下载功能实例解析

    2023-03-09 07:54:31
  • Android实现全局悬浮框

    2021-11-22 12:15:40
  • Android中SQLite 使用方法详解

    2022-02-08 15:31:37
  • java数学工具类Math详解(round方法)

    2022-09-10 17:01:00
  • SpringBoot项目从搭建到发布一条龙

    2023-11-21 09:28:44
  • Java各种排序算法汇总(冒泡,选择,归并,希尔及堆排序等)

    2021-10-05 14:46:23
  • Java进程间通信之消息队列

    2023-05-24 01:44:27
  • C# 获取打印机当前状态的方法

    2021-09-29 19:54:43
  • C#执行Javascript代码的几种方法总结

    2023-06-12 10:06:14
  • Android自定义View实现圆弧进度效果

    2021-08-20 02:26:21
  • Windows系统中Java调用cmd命令及执行exe程序的方法

    2021-11-27 23:00:02
  • HashMap在JDK7与JDK8中的实现过程解析

    2022-03-04 18:26:44
  • WindowsForm实现警告消息框的实例代码

    2023-05-25 00:00:54
  • asp之家 软件编程 m.aspxhome.com