Javaweb——Spring Boot 系列(11)自定义 Repository访问数据库

  • JPA 除了可以直接调用相关接口来访问和操作数据库,还支持通过继承接口自定义 Repository 实现访问数据库,自定义实现主要工作为自定义编辑一个 Specification,一个继承 JpaRepository 的 Interface 以及相应的实现,定义一个 repositoryFactoryBean。

1、Specification 自定义

  • 在项目目录下新建一个 specs 文件夹,新建一个 CustomSpecs 类,编辑如下代码:
    package com.pyc.springjpa.specs;
    
    import static com.google.common.collect.Iterables.toArray;
    
    import org.springframework.data.jpa.domain.Specification;
    import org.springframework.util.ReflectionUtils;
    import org.springframework.util.StringUtils;
    
    import javax.persistence.EntityManager;
    import javax.persistence.criteria.CriteriaBuilder;
    import javax.persistence.criteria.CriteriaQuery;
    import javax.persistence.criteria.Predicate;
    import javax.persistence.criteria.Root;
    import javax.persistence.metamodel.Attribute;
    import javax.persistence.metamodel.EntityType;
    import javax.persistence.metamodel.SingularAttribute;
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.List;
    
    public class CustomSpecs {
         
        // Define a method that returns a Specification and named byAuto, use generic as return type of this method
        // so that this method can affect any entity class,the parameter of this method is entityManager
        // and a query condition with current parameter value.
        // 定义一个返回值为 Specification 名称为byAuto的方法,使用泛型 T,表明这个Specification 可用于
        // 任何实体类;参数为 entityManager 和包含当前值的查询条件
        public static <T> Specification<T> byAuto(final EntityManager entityManager,
                                                  final T example) {
         
            //获得当前实体类对象的类型
            final Class<T> type = (Class<T>) example.getClass();
            return new Specification<T>() {
         
                @Override
                public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery,
                                             CriteriaBuilder criteriaBuilder) {
         
                    // create a Predicate list to stores the constructed query conditions
                    List<Predicate> predicates = new ArrayList<>(); //新建 Predicate 列表存储构造的查询条件
                    // get the type of entity so,and then can get the attributes of entity by using entity type
                    //获得实体类的 EntityType,从而获得实体类的属性
                    EntityType<T> entity = entityManager.getMetamodel().entity(type);
                    // Doing loop on all attributes of entity
                    // 对实体类的所有属性做循环
                    for (Attribute<T, ?> attr : entity.getDeclaredAttributes(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御承扬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值