jpa接口详解
jpa接口详解
- Repository
- CrudRepository
- PagingAndSortingRepository
QueryByExampleExecutor
@Data
@Entity // 声明这个类对应了一个数据库表
@Table(name = “BB_CLOTHES”) // 声明了数据库实体对应的表信息,如果没有指定,则表名和实体的名称保持一致
public class Clothes implements Serializable{private static final long serialVersionUID = -3018808522887346432L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)//主键由数据库自动生成
private Long id;
@Column(length = 32)
private String name;
@Column(length = 32)
private Integer size;
@Column(length = 64)
private Float price;
}
Repository
中央存储库标记接口
T: 实体类
ID: 实体类的主键类型
@Indexed
public interface Repository<T, ID> {
}
CrudRepository
通用的增删改查接口
public interface CrudRepository<T, ID> extends Repository<T, ID> {
<S extends T> S save(S entity);
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
Optional<T> findById(ID id);
boolean existsById(ID id);
Iterable<T> findAll();
Iterable<T> findAllById(Iterable<ID> ids);
long count();
void deleteById(ID id);
void delete(T entity);
void deleteAll(Iterable<? extends T> entities);
void deleteAll();
}
@Autowired ClothesDao clothesDao;
@GetMapping("/testCrudRepository")
Object testCrudRepository(Clothes clothes) {
log.info("【clothes】={}", clothes);
// 保存对象
clothesDao.save(clothes);
// 保存列表里面的对象 对每条记录分别执行 insert into bb_clothes (name, price, size) values (?, ?, ?)
ArrayList<Clothes> saveList = new ArrayList<>();
Clothes saveO1 = new Clothes();saveO1.setName("save1");
Clothes saveO2 = new Clothes();saveO2.setName("save2");
saveList.add(saveO1);
saveList.add(saveO2);
clothesDao.saveAll(saveList);
// 根据id查找 返回 Optional<Clothes> 对象
Optional<Clothes> clothesOpt = clothesDao.findById(1l);
clothesOpt.ifPresent(c -> log.info("【findById】={}", c));
// 根据id查找记录是否存在
log.info("【existsById】={}", clothesDao.existsById(1l));
// 查找全部
log.info("【findAll】={}", clothesDao.findAll());
// 查找指定id列表对应的对象列表
List<Long> idList = new ArrayList<>();
idList.add(1l);
idList.add(2l);
log.info("【findAllById】.Iterable={}", clothesDao.findAllById(idList));
log.info("【count】={}", clothesDao.count());
// 根据id删除, 先查询数据库是否有该记录,如果有删除,否则删除不存在数据会抛出异常
Optional<Clothes> beDeleteObject = clothesDao.findById(9l);
beDeleteObject.ifPresent(c -> clothesDao.delete(c));
// 最终执行的删除操作 delete from bb_clothes where id=?
Optional<Clothes> opt = clothesDao.findById(24l);
opt.ifPresent(c -> {
clothesDao.delete(c);
});
// 删除所有数据 select * from bb_clothes;
// 对每条记录 delete from bb_clothes where id=?
// clothesDao.deleteAll();
// 对每条记录遍历删除 delete from bb_clothes where id=?
List<Clothes> deleteList = clothesDao.findAllById(idList);
clothesDao.deleteAll(deleteList);
return deleteList;
}
PagingAndSortingRepository
增加分页和排序功能
@NoRepositoryBean
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
}
@SuppressWarnings("unused")
@GetMapping("/testPagingAndSortingRepository")
Object testPagingAndSortingRepository(Clothes clothes) {
// 指定排序字段查找全部
Sort sort01 = new Sort(Direction.ASC, "name");
Sort sort02 = Sort.by(Direction.ASC, "name");
Order idOrder = new Order(Direction.DESC, "id");
Order idOrder2 = new Order(Direction.DESC, "name");
Sort sort03 = Sort.by(idOrder,idOrder2);
Sort sort04 = Sort.by(Direction.ASC, "id", "name");
Sort sort05 = Sort.by("id", "name");
log.info("【findAll】.sort={}", clothesDao.findAll(sort01));
int pageIndex = 1;
Pageable pageable=PageRequest.of(pageIndex-1, 2,sort01);
Pageable pageable02 = PageRequest.of(pageIndex-1, 2, Direction.ASC, "id");
Pageable pageable03 = PageRequest.of(pageIndex-1, 2, Direction.ASC, "id", "name");
Page<Clothes> page = clothesDao.findAll(pageable);
return page;
}
QueryByExampleExecutor
public interface QueryByExampleExecutor<T> {
<S extends T> Optional<S> findOne(Example<S> example);
<S extends T> Iterable<S> findAll(Example<S> example);
<S extends T> Iterable<S> findAll(Example<S> example, Sort sort);
<S extends T> Page<S> findAll(Example<S> example, Pageable pageable);
<S extends T> long count(Example<S> example);
<S extends T> boolean exists(Example<S> example);
}
@RestController
@RequestMapping("/example")
@Slf4j
public class QueryByExampleController {
@Autowired ClothesDao clothesDao;
@GetMapping("/findOne")
Object findOne(Long id) {
// 1、 精准匹配对象,查出多条会报错
Clothes ClothesObject = new Clothes();
ClothesObject.setId(id);
Example<Clothes> example = Example.of(ClothesObject);
Optional<Clothes> optClothes = clothesDao.findOne(example);
optClothes.ifPresent(c -> log.info("【findOne】.sort={}", c));
return optClothes.get();
}
@GetMapping("/findAll")
Object findAll() {
Clothes Clothes = new Clothes();
Clothes.setName("黑");
ExampleMatcher matcher = ExampleMatcher.matching();
// 注意matcher.withMatcher返回一个新的对象
matcher = matcher.withMatcher("name", match -> match.startsWith());
// matcher = matcher.withMatcher("name", match -> match.regex());
// matcher = matcher.withMatcher("name", match -> match.endsWith());
// matcher = matcher.withMatcher("name", match -> match.contains());
// matcher = matcher.withIgnorePaths("name");
// ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name", ExampleMatcher.GenericPropertyMatchers.endsWith());
Example<Clothes> example = Example.of(Clothes, matcher);
Sort sort = Sort.by(Direction.ASC, "name");
List<Clothes> findAll = clothesDao.findAll(example, sort);
log.info("【findAll】.example, sort, {}", findAll);
return findAll;
}
@GetMapping("/getPage")
Object getPage() {
Clothes Clothes = new Clothes();
Clothes.setName("衬衫");
ExampleMatcher matcher = ExampleMatcher.matching();
matcher = matcher.withMatcher("name", match -> match.contains());
Example<Clothes> example = Example.of(Clothes, matcher);
Pageable pageable = PageRequest.of(0, 15, Direction.DESC, "name");
Page<Clothes> page = clothesDao.findAll(example, pageable);
log.info("【findAll】.example, pageable={}", page);
return page;
}
}
还没有评论,来说两句吧...