首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何快速一键搞定JPA中所有findBy语法规则

JPA中findBy基本语法规则

1.首先先新建一个数据库,名字叫做jpatest

2.新建一个SpringBoot项目(如果新手还不会,请先阅读idea中如何快速创建SpringBoot项目)

这边需要引入jpa+mysql+web的相关依赖,如果创建的时候没有引入就需要在pom.xml进行配置

新建一个controller包(控制类所在地),entity包(实体类所在地),repository包(dao类所在地)

3.application.properties配置文件中添加mysql数据库和jpa的相关配置

#数据库驱动

#数据库url地址,其中jpatest是数据库名称

spring.datasource.url=jdbc:mysql://localhost:3308/jpatest?useUnicode=true&characterEncoding=utf-8

#用户名称

#用户密码

#创建数据库表结构(更新的状态下创建)

#显示执行的sql语句

4.新建表映射实体类

import javax.persistence.*;

/**

* Created by qiang on 2018/1/22.

*/

@Entity

@Table(name="user")

public class User {

@Id

@GenericGenerator(name = "PKUUID", strategy = "uuid2")

@GeneratedValue(generator = "PKUUID")

@Column(length = 36)

protected String id;

/**

* 名字

*/

@Column(name = "name", nullable = true, length = 30)

private String name;

/**

* 身高

*/

@Column(name = "height", nullable = true, length = 10)

private Integer height;

public User() {

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getHeight() {

return height;

}

public void setHeight(Integer height) {

this.height = height;

}

}

关于实体类的注释我会在接下来的章节中具体的介绍给大家,这篇文章先不做介绍。

5.jpa的数据操作接口(也是本章节中的重点)

import java.util.List;

/**

* Created by qiang on 2018/1/22.

*/

public interface UserRepository extends JpaRepository{

/**

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

* @param name

* @return

*/

public List findByName(String name);

/**

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

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

* @param name

* @return

*/

public List findByNameLike(String name);

/**

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

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

* @param name

* @return

*/

public List findByNameNotLike(String name);

/**

* 相当于 select *from user where name ?

* @param name

* @return

*/

public List findByNameNot(String name);

/**

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

* @param ids

* @return

*/

public List findByIdIn(List ids);

/**

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

* @param ids

* @return

*/

public List findByIdNotIn(List ids);

/**

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

* @param name

* @return

*/

public List findByNameOrderByHeightDesc(String name);

/**

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

* @param name

* @return

*/

public List findByNameOrderByHeightAsc(String name);

/**

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

* @return

*/

public List findByNameIsNull();

/**

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

* @return

*/

public List findByNameIsNotNull();

/**

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

* @param name

* @param height

* @return

*/

public List findByNameAndHeight(String name,int height);

/**

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

* @param name

* @param height

* @return

*/

public List findByNameOrHeight(String name,int height);

/**

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

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

* @param start

* @param end

* @return

*/

public List findByHeightBetween(int start,int end);

/**

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

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

* @param less

* @return

*/

public List findByHeightLessThan(int less);

/**

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

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

* @param greater

* @return

*/

public List findByHeightGreaterThan(int greater);

}

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

5.通过控制类调用dao接口

import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;

import java.util.List;

/**

* Created by qiang on 2018/1/22.

*/

@Controller

@RequestMapping(value = "jpa")

public class UserController {

@Autowired

private UserRepository userRepository;

@RequestMapping(value = "findByName",method = RequestMethod.GET)

@ResponseBody

public List findByName(String name){

return userRepository.findByName(name);

}

@RequestMapping(value = "findByHeightBetween",method = RequestMethod.GET)

@ResponseBody

public List findByHeightBetween(int start,int end){

return userRepository.findByHeightBetween(start,end);

}

@RequestMapping(value = "findByHeightLessThan",method = RequestMethod.GET)

@ResponseBody

public List findByHeightLessThan(int less){

return userRepository.findByHeightLessThan(less);

}

@RequestMapping(value = "findByHeightGreaterThan",method = RequestMethod.GET)

@ResponseBody

public List findByHeightGreaterThan(int greater){

return userRepository.findByHeightGreaterThan(greater);

}

@RequestMapping(value = "findByNameAndHeight",method = RequestMethod.GET)

@ResponseBody

public List findByNameAndHeight(String name,int height){

return userRepository.findByNameAndHeight(name,height);

}

@RequestMapping(value = "findByNameOrHeight",method = RequestMethod.GET)

@ResponseBody

public List findByNameOrHeight(String name,int height){

return userRepository.findByNameOrHeight(name,height);

}

@RequestMapping(value = "findByNameIsNull",method = RequestMethod.GET)

@ResponseBody

public List findByNameIsNull(){

return userRepository.findByNameIsNull();

}

@RequestMapping(value = "findByNameIsNotNull",method = RequestMethod.GET)

@ResponseBody

public List findByNameIsNotNull(){

return userRepository.findByNameIsNotNull();

}

@RequestMapping(value = "findByNameOrderByHeightDesc",method = RequestMethod.GET)

@ResponseBody

public List findByNameOrderByHeightDesc(String name){

return userRepository.findByNameOrderByHeightDesc(name);

}

@RequestMapping(value = "findByNameOrderByHeightAsc",method = RequestMethod.GET)

@ResponseBody

public List findByNameOrderByHeightAsc(String name){

return userRepository.findByNameOrderByHeightAsc(name);

}

@RequestMapping(value = "findByNameLike",method = RequestMethod.GET)

@ResponseBody

public List findByNameLike(String name){

return userRepository.findByNameLike("%"+name+"%");

}

@RequestMapping(value = "findByNameNotLike",method = RequestMethod.GET)

@ResponseBody

public List findByNameNotLike(String name){

return userRepository.findByNameNotLike("%"+name+"%");

}

@RequestMapping(value = "findByNameNot",method = RequestMethod.GET)

@ResponseBody

public List findByNameNot(String name){

return userRepository.findByNameNot(name);

}

@RequestMapping(value = "findByIdIn",method = RequestMethod.GET)

@ResponseBody

public List findByIdIn(String ids){

Listlist=new ArrayList();

String[] idsStr=ids.split(",");

for(String id:idsStr){

list.add(id);

}

return userRepository.findByIdIn(list);

}

@RequestMapping(value = "findByIdNotIn",method = RequestMethod.GET)

@ResponseBody

public List findByIdNotIn(String ids){

Listlist=new ArrayList();

String[] idsStr=ids.split(",");

for(String id:idsStr){

list.add(id);

}

return userRepository.findByIdNotIn(list);

}

}

到这里关于jpa中findBy基本的语法就介绍完毕了,下一节将介绍jpa中关联查询的用法。

项目GitHub地址:https://github.com/1913045515/JPA

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180122G10AOU00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券