用过JPA的都知道,只需要继承JpaRepository 根据Jpa的函数命名规范写出接口中的函数,不需要实现,底层就可以自动解析成各种数据库的sql语句,进行增删改查等操作。

自定义简单的查询方法

如findByUserName,findByUserNameOrEmail(String username, String email)等条件的属性名称与个数要与参数的位置与个数一一对应,JpaRepository能够解析方法名自动生成sql语句,主要的语法是findXXBy,readAXXBy,queryXXBy,countXXBy, getXXBy后面跟属性名称即可,

关键词

举例

对应的sql语句

And

findByLastnameAndFirstname

… where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

… where x.lastname = ?1 or x.firstname = ?2

Is,Equals

findByFirstnameIs,findByFirstnameEquals

… where x.firstname = ?1

Between

findByStartDateBetween

… where x.startDate between ?1 and ?2

LessThan

findByAgeLessThan

… where x.age < ?1

LessThanEqual

findByAgeLessThanEqual

… where x.age ⇐ ?1

GreaterThan

findByAgeGreaterThan

… where x.age > ?1

GreaterThanEqual

findByAgeGreaterThanEqual

… where x.age >= ?1

After

findByStartDateAfter

… where x.startDate > ?1

Before

findByStartDateBefore

… where x.startDate < ?1

IsNull

findByAgeIsNull

… where x.age is null

IsNotNull,NotNull

findByAge(Is)NotNull

… where x.age not null

Like

findByFirstnameLike

… where x.firstname like ?1

NotLike

findByFirstnameNotLike

… where x.firstname not like ?1

StartingWith

findByFirstnameStartingWith

… where x.firstname like ?1 (parameter bound with appended %)

EndingWith

findByFirstnameEndingWith

… where x.firstname like ?1 (parameter bound with prepended %)

Containing

findByFirstnameContaining

… where x.firstname like ?1 (parameter bound wrapped in %)

OrderBy

findByAgeOrderByLastnameDesc

… where x.age = ?1 order by x.lastname desc

Not

findByLastnameNot

… where x.lastname <> ?1

In

findByAgeIn(Collection ages)

… where x.age in ?1

NotIn

findByAgeNotIn(Collection age)

… where x.age not in ?1

TRUE

findByActiveTrue()

… where x.active = true

FALSE

findByActiveFalse()

… where x.active = false

IgnoreCase

findByFirstnameIgnoreCase

… where UPPER(x.firstame) = UPPER(?1)

这种方式很简单,但也有很多缺陷,不够灵活,不用担心,Spring data jpa还支持自定义查询语句。

  • 使用 @Query注解,添加JPQL语句,JPQL是一种面向对象的表达式语言,通过类名和属性访问,而不是数据库中的表名和属性,避免了不同数据库语法的不同。
  • 在@Query中添加nativeQuery = true表示使用原生的sql语句,这时候就有要使用数据库表名和字段名了。
  • 当涉及到删除和修改在需要加上@Modifying.也可以根据需要添加 @Transactional对事物的支持,查询超时的设置等。

使用JPA命名规范写的函数能够查询出整个对象,而不能只查询出一个或几个字段,因为有时候我们只需要一个bean中的几个字段就够了,不需要全部的。

例如用户表,根据id查姓名,就不需要把密码等重要信息查出来了,因为这些信息封装在一个对象中返回到前端是很危险的,即使没有显示,但也可以在浏览器上调试看到。

原生的sql当然可以做到,但是我们还想让查询出的结果封装成一个对象,以便后续的操作。

那么自定义的JPQL就可以实现这个功能了。
例如有一个用户表,如下:

@Entity
@Table(name = "yhb")
public class YHB {
    //用户编号
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer yhbh;

    //用户代码(名字拼音缩写)
    private String yhdm;

    //用户名称
    private String yhmc;

    //用户密码
    private String yhkl;

    //用户部门编号
    private String yhbm;

    //用户职务
    private String yhsf;

    //用户手机号
    private String phone;

//省略构造函数和get set

现在需要根据部门编号查询出这个部门所有人的用户编号用户名称,那么我们可以新建一个Model,就只有用户编号用户名称这两个字段:
YhbModel.java

public class YhbModel implements Serializable {

    private Integer yhbh;

    //用户名称
    private String yhmc;

在用户表的dao层可以这样写操作数据库的方法:

@Repository
public interface YhbDao extends JpaRepository<YHB,Integer>{

    //根据部门查找用户
    @Transactional
    @Query(value = "select new com.nju.software.assessment.model.YhbModel(y.yhbh,y.yhmc) from YHB y where yhbm=?1")
     List<YhbModel> findYhmcByYhbm(String yhbm);
}

@Query中的JPQL意思是根据用户部门编号yhbm查询用户的实体类YHB中的用户编号yhbh用户名称yhmc,并把查询出的结果封装成一个YhbModel对象,List<YhbModel>中类型也是YhbModel类型的,之后的service层和controller层调用的时候返回类型也是List<YhbModel>