我是靠谱客的博主 轻松诺言,最近开发中收集的这篇文章主要介绍springboot jpa 单表操作,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


springboot jpa 单表操作

 

*************************

相关类与接口

 

基础接口

public interface Repository <T, ID>
public interface CrudRepository<T, ID> extends Repository<T, ID>
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID>
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
QueryByExampleExecutor<T>
public interface JpaSpecificationExecutor<T>

 使用说明:单表操作继承JpaRepository接口即可,如需动态查询可在此基础上继承JpaSpecificationExecutor

 

Sort:排序

public class Sort implements Streamable<Sort.Order>, Serializable
public static Sort by(Sort.Direction direction, String... properties)
public static Sort by(Sort.Direction direction, String... properties)
public Sort and(Sort sort)
public Sort ascending()
public Sort descending()
**********
内部枚举:Sort.Direction
public static enum Direction {
ASC,
DESC;
private Direction() {
}
public boolean isAscending() {
public boolean isDescending() {
public static Sort.Direction fromString(String value) {
public static Optional<Sort.Direction> fromOptionalString(String value) {

 

PageRequest:分页

public class PageRequest extends AbstractPageRequest {
private static final long serialVersionUID = -4541509938956089562L;
private final Sort sort;
protected PageRequest(int page, int size, Sort sort) {
public static PageRequest of(int page, int size) {
public static PageRequest of(int page, int size, Sort sort) {
public static PageRequest of(int page, int size, Direction direction, String... properties) {
public Sort getSort() {
public Pageable next() {
public PageRequest previous() {
public Pageable first() {
public boolean equals(@Nullable Object obj) {
public int hashCode() {
public String toString() {

 

Pageable

public interface Pageable {
int getPageNumber();
int getPageSize();
long getOffset();
Sort getSort();
Pageable first();
Pageable next();
Pageable previousOrFirst();
boolean hasPrevious();
static Pageable unpaged() {
return Unpaged.INSTANCE;
}
default boolean isPaged() {
return true;
}
default boolean isUnpaged() {
return !this.isPaged();
}
default Sort getSortOr(Sort sort) {
Assert.notNull(sort, "Fallback Sort must not be null!");
return this.getSort().isSorted() ? this.getSort() : sort;
}
default Optional<Pageable> toOptional() {
return this.isUnpaged() ? Optional.empty() : Optional.of(this);
}
}

 

 

*************************

使用示例

 

**************

dao

 

UserRepository

public interface UserRepository extends JpaRepository<User,Integer> {
User findUsersByName(String name);
List<User> findUsersByAge(Integer age);
Integer countDistinctByAge(Integer age);
Integer deleteByName(String name);
Integer deleteByAge(Integer age);
}

说明:自定义的delete方法需要在Transactional中使用,不然会报错

 

**************

service层

 

UserService

public interface UserService {
Integer deleteByName(String name);
Integer deleteByAge(Integer age);
}

 

**************

serviceImpl 层

 

UserServiceImpl

@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
@Transactional
public Integer deleteByName(String name) {
return userRepository.deleteByName(name);
}
@Override
@Transactional
public Integer deleteByAge(Integer age) {
return userRepository.deleteByAge(age);
}
}

 

**************

controller层

 

UserController

@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@Autowired
private UserService userService;
@RequestMapping("/save")
public String save(){
for(int i=0;i<100;i++){
User user=new User();
user.setName("瓜田李下"+i);
user.setAge(i);
userRepository.save(user);
}
return "success";
}
@RequestMapping("/delete")
public String delete(){
userRepository.deleteByName("瓜田李下"+2);
return "success";
}
@RequestMapping("/delete2")
public Integer delete2(){
return userRepository.deleteByAge(23);
}
@RequestMapping("/delete3")
public void delete3(){
userRepository.deleteById(5);
}
@RequestMapping("/findByPage")
public List<User> find(){
//测试分页
PageRequest pageRequest=PageRequest.of(0,10);
Page<User> page=userRepository.findAll(pageRequest);
return page.getContent();
}
@RequestMapping("/sort")
public List<User> sort(){
//测试分页、排序
Sort sort=Sort.by(Sort.Direction.ASC,"age");
Page<User> page=userRepository.findAll(PageRequest.of(0,10,sort));
return page.getContent();
}
}

 

 

最后

以上就是轻松诺言为你收集整理的springboot jpa 单表操作的全部内容,希望文章能够帮你解决springboot jpa 单表操作所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部