目录
本次案例在案例(二)的基础上进行
一、数据操作层
package com.dragonwu.repositories;
import com.dragonwu.entity.Customer;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.repository.CrudRepository;
import javax.transaction.Transactional;
import java.util.List;
/**
* @author DragonWu
* @date 2022-09-21 10:33
**/
/**
* 参数一 T :当前需要映射的实体
* 参数二 ID :当前映射的实体中的OID的类型
* PagingAndSortingRepository 继承后可以调研分页接口
*/
public interface CustomerRepository extends CrudRepository<Customer,Long>{
List<Customer> findCustomerByName(String name);
boolean existsCustomerByName(String name);
@Transactional //开启事务,一般写的业务层
@Modifying //告诉springjpa当前为非查询的操作
int deleteCustomerByName(String name);
//模糊查询
List<Customer> findCustomerByNameLike(String name);
}
这里由于JPA独有的特性,我们只需要书写方法时按照JPA规范来实现即可完成对应功能。
二、JPA的方法规范
关键字 | 方法命名 | sql where字句 |
And | findByNameAndPwd | where name= ? and pwd =? |
Or | findByNameOrSex | where name= ? or sex=? |
Is,Equals | findById,findByIdEquals | where id= ? |
Between | findByIdBetween | where id between ? and ? |
LessThan | findByIdLessThan | where id < ? |
LessThanEqual | findByIdLessThanEqual | where id <= ? |
GreaterThan | findByIdGreaterThan | where id > ? |
GreaterThanEqual | findByIdGreaterThanEqual | where id > = ? |
After | findByIdAfter | where id > ? |
Before | findByIdBefore | where id < ? |
IsNull | findByNameIsNull | where name is null |
isNotNull,NotNull | findByNameNotNull | where name is not null |
Like | findByNameLike | where name like ? |
NotLike | findByNameNotLike | where name not like ? |
StartingWith | findByNameStartingWith | where name like '?%' |
EndingWith | findByNameEndingWith | where name like '%?' |
Containing | findByNameContaining | where name like '%?%' |
OrderBy | findByIdOrderByXDesc | where id=? order by x desc |
Not | findByNameNot | where name <> ? |
In | findByIdIn(Collection<?> c) | where id in (?) |
NotIn | findByIdNotIn(Collection<?> c) | where id not in (?) |
True | findByAaaTue | where aaa = true |
False | findByAaaFalse | where aaa = false |
IgnoreCase | findByNameIgnoreCase | where UPPER(name)=UPPER(?) |
三、测试类
测试类直接调用该接口即可,非常方便
package com.dragonwu;
import com.dragonwu.entity.Customer;
import com.dragonwu.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/**
* @author DragonWu
* @date 2022-09-21 16:09
**/
@SpringBootTest(classes = App.class)
@RunWith(SpringRunner.class)
public class MethodTest {
@Autowired
private CustomerRepository customerRepository;
@Test
public void testR(){
List<Customer> dragonWu = customerRepository.findCustomerByName("DragonWu");
System.out.println(dragonWu);
}
@Test
public void testIsExist(){
boolean dragonWu = customerRepository.existsCustomerByName("DragonWu");
System.out.println("是否存在该名字:"+dragonWu);
}
@Test
public void testLike(){
List<Customer> customerByNameLike = customerRepository.findCustomerByNameLike("Dragon%");
System.out.println(customerByNameLike);
}
}