Spring Data Jpa的主要类   七个Repository接口:     1.Repository     2.CrudRepository     3.PagingAndSortingRepository     4.QueryByExampleExecutor     5.JpaRepository     6.JpaSpeccificationExecutor     7.QueryDslPredicateExecutor   两个实现类:     1.SimpleJpaRepository     2.QueryDslJpaRepository ***** 本框架中`JpaPlusRepository`实现了`JpaRepositoryImplementation`、`QuerydslPredicateExecutor` 在保留原方法基础上新增对`QueryDsl`的支持。 ``` public interface JpaPlusRepository<T, ID> extends JpaRepositoryImplementation<T, ID>, QuerydslPredicateExecutor<T> { T findOne(ID id); <S> Page<S> findAll(JPAQuery<S> query, Pageable pageable); <O extends T> List<O> insert(O... entities); int insertOrUpdate(T... entities); int delete(ID... ids); int delete(JPADeleteClause delete); int delete(JPADeleteClause delete, Predicate... predicate); int update(JPAUpdateClause update); int update(JPAUpdateClause update, Predicate... predicate); } ``` ### 依赖模块 ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` ### 启用注解 `@EnableJpaPlusRepositories ` ``` @EnableJpaPlusRepositories @SpringBootApplication public class GotvApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication app = new SpringApplication(GotvApplication.class); app.run(args); } } ``` ## 如何使用 ### 1. 单表分页查询 ``` @RequestMapping(value = "page") public PaginationResult page(BaseVo base, PaginationVo page) { // 对象 QSLog qsLog = QSLog.sLog; // 条件 Predicate predicate = qsLog.id.isNotNull(); if (ObjectUtils.isNotEmpty(base.getKeyword())) { Predicate p1 = qsLog.id.like("%" + base.getKeyword() + "%"); Predicate p2 = qsLog.method.likeIgnoreCase("%" + base.getKeyword() + "%"); Predicate p3 = qsLog.type.likeIgnoreCase("%" + base.getKeyword() + "%"); predicate = ExpressionUtils.and(predicate, ExpressionUtils.anyOf(p1, p2, p3)); } // 排序 Sort sort = Sort.by(Direction.ASC, SLog.FIELDS.createDate); // 分页 Pageable pageable = PageRequest.of(page.getPage(), page.getLimit(), sort); // 查询数据 Page<SLog> data = mSLogRepository.findAll(predicate, pageable); // 构造 return PaginationHelper.create(data); } ``` ### 2. 多表分页查询 ``` @RequestMapping(value = "page") public PaginationResult page(BaseVo base, PaginationVo page) { // 对象 QUser qUser = QUser.user; QSRole qRole = QSRole.sRole; QUserRole qUserRole = QUserRole.userRole; // 条件 Predicate predicate = qUser.id.isNotNull(); if (ObjectUtils.isNotEmpty(base.getKeyword())) { Predicate p1 = qUser.id.like("%" + base.getKeyword() + "%"); Predicate p2 = qUser.username.likeIgnoreCase("%" + base.getKeyword() + "%"); Predicate p3 = qUser.nickname.likeIgnoreCase("%" + base.getKeyword() + "%"); predicate = ExpressionUtils.and(predicate, ExpressionUtils.anyOf(p1, p2, p3)); } // 分页 Pageable pageable = PageRequest.of(page.getPage(), page.getLimit(), Sort.by(Direction.DESC, User.FIELDS.createDate)); // 查询字段 QBean<User> selectBean = Projections.fields(User.class, qUser.id.longValue(), qUser.username, qUser.nickname, qUser.type, qUser.isEnable, qUser.createDate, qUser.updateDate, qRole.id.longValue().as(User.FIELDS.roleId), qRole.name.as(User.FIELDS.roleName)); // 构造查询 JPAQuery<User> jpaQuery = mJPAQueryFactory.select(selectBean) .from(qUser) .leftJoin(qUserRole) .on(qUser.id.eq(qUserRole.pk.users)) .leftJoin(qRole) .on(qRole.id.eq(qUserRole.pk.roles)) .where(predicate); // 分页查询 Page<User> data = mUserRepository.findAll(jpaQuery, pageable); // 构造 return PaginationHelper.create(data); } ``` ### 3. 添加 ``` @RequestMapping(value = "save") @Transactional public BaseResult save(BaseVo base) { User user = new User(); int row = mUserRepository.insertOrUpdate(user); return (row >= 1) ? R.succ() : R.fail(); } ``` ### 4. 修改 ``` @RequestMapping(value = "update") @Transactional public BaseResult update(BaseVo base) { QUser qUser = QUser.user; int row = mUserRepository.update(mJPAQueryFactory.update(qUser).set(qUser.isEnable, 1).where(qUser.id.in(base.getIds()))); return (row >= 1) ? R.succ() : R.fail(); } ``` ### 5. 删除 ``` @RequestMapping(value = "delete") @Transactional public BaseResult delete(BaseVo base) { QUser qUser = QUser.user; int row = mUserRepository.delete(mJPAQueryFactory.delete(qUser).where(qUser.id.in(base.getIds()))); return (row >= 1) ? R.succ() : R.fail(); } ``` 上述只是部分示例,拓展后常规CRUD方法共计48个