springboot集成spring data jpa使用CrudRepository中的api(gradle)

it2024-07-29  36

版本环境

java:jdk-14.0.1 springboot:v2.3.4.RELEASE gradle:gradle-6.7-rc-4 IntelliJ IDEA:2020.1.2 spring-boot-starter-data-jpa:v2.3.4.RELEASE

实体类

@NoArgsConstructor @ApiModel(value = "用户实体", description = "用户实体类") @Data @Table(name = "user") @Entity public class User implements Serializable { private static final long serialVersionUID = 4728506793752030545L; @ApiModelProperty(value = "用户名") @Column(name = "name" ) private String name; @JsonIgnore @ApiModelProperty(value = "用户密码") @Column(name = "pass" ) private String pass; @ApiModelProperty(value = "用户年龄") @Column(name = "age" ) private Integer age; @Column(name = "email" ) @ApiModelProperty(value = "用户邮件") private String email; @ApiModelProperty(value = "用户id") @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id" ) private Integer id ; }

数据库

create table mytest.user ( id int auto_increment primary key, name varchar(30) not null, pass varchar(30) not null, age int null, email varchar(30) null, constraint user_name_uindex unique (name) );

Repository

@Repository public interface UserRepository extends JpaRepository<User, Integer> { }

CrudRepository

JpaRepository所继承的PagingAndSortingRepository继承了CrudRepository,在CrudRepository中集成了一些基础的curd方法:

save

/** * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the * entity instance completely. * * @param entity must not be {@literal null}. * @return the saved entity; will never be {@literal null}. * @throws IllegalArgumentException in case the given {@literal entity} is {@literal null}. */ <S extends T> S save(S entity);

调用实例

User user=new User("lzcsave","savelzc"); userRepository.save(user);

saveAll

/** * Saves all given entities. * * @param entities must not be {@literal null} nor must it contain {@literal null}. * @return the saved entities; will never be {@literal null}. The returned {@literal Iterable} will have the same size * as the {@literal Iterable} passed as an argument. * @throws IllegalArgumentException in case the given {@link Iterable entities} or one of its entities is * {@literal null}. */ <S extends T> Iterable<S> saveAll(Iterable<S> entities);

调用实例

var users=new ArrayList<User>(); users.add(new User("lzcall1","lzcall1")); users.add(new User("lzcall2","lzcall2")); users.add(new User("lzcall3","lzcall3")); users.add(new User("lzcall4","lzcall4")); users.add(new User("lzcall5","lzcall5")); userRepository.saveAll(users);

findByID

/** * Retrieves an entity by its id. * * @param id must not be {@literal null}. * @return the entity with the given id or {@literal Optional#empty()} if none found. * @throws IllegalArgumentException if {@literal id} is {@literal null}. */ Optional<T> findById(ID id);

调用实例

var user=userRepository.findById(2); System.out.println(user.toString());

existsById

/** * Returns whether an entity with the given id exists. * * @param id must not be {@literal null}. * @return {@literal true} if an entity with the given id exists, {@literal false} otherwise. * @throws IllegalArgumentException if {@literal id} is {@literal null}. */ boolean existsById(ID id);

调用实例

boolean flag=userRepository.existsById(1); System.out.println(flag);

findAll

/** * Returns all instances of the type. * * @return all entities */ Iterable<T> findAll();

调用实例

var all=userRepository.findAll(); all.forEach(System.out::println);

findAllById

/** * Returns all instances of the type {@code T} with the given IDs. * <p> * If some or all ids are not found, no entities are returned for these IDs. * <p> * Note that the order of elements in the result is not guaranteed. * * @param ids must not be {@literal null} nor contain any {@literal null} values. * @return guaranteed to be not {@literal null}. The size can be equal or less than the number of given * {@literal ids}. * @throws IllegalArgumentException in case the given {@link Iterable ids} or one of its items is {@literal null}. */ Iterable<T> findAllById(Iterable<ID> ids);

调用实例

var idList=new ArrayList<Integer>(); idList.add(2); idList.add(4); idList.add(7); idList.add(9); var all=userRepository.findAllById(idList); all.forEach(System.out::println);

count

/** * Returns the number of entities available. * * @return the number of entities. */ long count();

调用实例

long count=userRepository.count(); System.out.println(count);

deleteById

/** * Deletes the entity with the given id. * * @param id must not be {@literal null}. * @throws IllegalArgumentException in case the given {@literal id} is {@literal null} */ void deleteById(ID id);

调用实例

userRepository.deleteById(2);

delete

/** * Deletes a given entity. * * @param entity must not be {@literal null}. * @throws IllegalArgumentException in case the given entity is {@literal null}. */ void delete(T entity);

调用实例

User user=new User(); user.setId(5); user.setName("jinx"); user.setPass("123"); user.setAge(123); user.setEmail("aksjlaklsdf"); userRepository.delete(user);

注:只有当操作的对象的所有属性值和数据库中对应列的值完全相等、一一对应时,才会成功删除。若对象只有部分值和数据表中相同,则视为未找到需要删除掉记录,不执行删除。

deleteAll

/** * Deletes the given entities. * * @param entities must not be {@literal null}. Must not contain {@literal null} elements. * @throws IllegalArgumentException in case the given {@literal entities} or one of its entities is {@literal null}. */ void deleteAll(Iterable<? extends T> entities); /** * Deletes all entities managed by the repository. */ void deleteAll();

有参数的用法是将需要删除的实体的List作为参数,执行后数据库将删除List中对象对应的记录,无参调用会将整表记录全部删除

最新回复(0)