JPA的findBy语法整理

1.jpa的数据操作接口

package com.lzq.jpa.entity.repository;

import com.lzq.jpa.entity.User;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**

  • Created by qiang on 2018/1/22.

*/

public interface UserRepository extends JpaRepository<User,String>{

/**

  • 相当于 select *from user where name=?

  • @param name

  • @return

*/

public List<User> findByName(String name);

/**

  • 相当于select *from user where name like ?

  • 但是有一点需要注意的是,%需要我们自己来写

  • @param name

  • @return

*/

public List<User> findByNameLike(String name);

/**

  • 相当于select *from user where name not like ?

  • 但是有一点需要注意的是,%需要我们自己来写

  • @param name

  • @return

*/

public List<User> findByNameNotLike(String name);

/**

  • 相当于 select *from user where name <> ?

  • @param name

  • @return

*/

public List<User> findByNameNot(String name);

/**

  • 相当于 select *from user where id in (?)

  • @param ids

  • @return

*/

public List<User> findByIdIn(List<String> ids);

/**

  • 相当于 select *from user where id not in ()

  • @param ids

  • @return

*/

public List<User> findByIdNotIn(List<String> ids);

/**

  • 相当于 select *from user where name=? order by height desc

  • @param name

  • @return

*/

public List<User> findByNameOrderByHeightDesc(String name);

/**

  • 相当于 select *from user where name=? order by height asc

  • @param name

  • @return

*/

public List<User> findByNameOrderByHeightAsc(String name);

/**

  • 相当于 select *from user where name is null

  • @return

*/

public List<User> findByNameIsNull();

/**

  • 相当于 select *from user where name is not null

  • @return

*/

public List<User> findByNameIsNotNull();

/**

  • 相当于 select *from user where name =? and height=?

  • @param name

  • @param height

  • @return

*/

public List<User> findByNameAndHeight(String name,int height);

/**

  • 相当于 select *from user where name =? or height=?

  • @param name

  • @param height

  • @return

*/

public List<User> findByNameOrHeight(String name,int height);

/**

  • 相当于 select *from user where height between ? and ?

  • 需要注意的是mysql是有包含两个端点值的

  • @param start

  • @param end

  • @return

*/

public List<User> findByHeightBetween(int start,int end);

/**

  • 相当于 select *from user where height < ?

  • 需要注意的是mysql是没有包含端点值的

  • @param less

  • @return

*/

public List<User> findByHeightLessThan(int less);

/**

  • 相当于 select *from user where height > ?

  • 需要注意的是mysql是没有包含端点值的

  • @param greater

  • @return

*/

public List<User> findByHeightGreaterThan(int greater);

}

具体语法规则和对应的sql都在代码中给出来了,这边需要和大家说的是UserRepository接口的特点。我们通过继承JpaRepository《对应的实体类,主键属性值》来编写findBy等相关的函数来查询数据库。继承JpaRepository的接口在使用的时候,通过@Autowired会自动创建接口的实现类,不需要怎么去实现这个接口,这也是jpa最方便的地方。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,554评论 0 13
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,874评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,273评论 19 139
  • 这是一个小胡同 流窜着古墓中 阴沉的泉水 鸟儿拂过 留下断断续续 清脆却并不甜美的 歌颂 花儿飘过 却渗透着生命的...
    听轩鱼阅读 218评论 0 0
  • 【安妮宝贝】20180501-0502践行Day96-97 识字营: 这两天读弗洛格系列,这是一套容易引发思考的绘...
    Anne安霓阅读 224评论 0 0