zoukankan      html  css  js  c++  java
  • EntityManager的使用

    1、最基础的查询

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(User.class);
    Root<User> root = cq.from(User.class); //from User
    cq.select(root); //select * from User
    javax.persistence.criteria.Predicate pre = cb.equal(root.get("id").as(Integer.class),id);//id=1
    cq.where(pre);//where id=1
    Query query = entityManager.createQuery(cq);//select u from User u where u.id = 1
    
    System.out.println(query.getResultList());

    2、spring data jpa 的 toPredicate 方法

    public Predicate toPredicate(Root<RoleEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    
        List<Predicate> list = new ArrayList<Predicate>();
        list.add(cb.equal(root.get("dr").as(Integer.class), delete));
        if (StringUtils.isNotBlank(searchParam)) {/*searchParam 为传递参数*/
            list.add(cb.or(cb.like(root.get("roleName").as(String.class), cb.literal("%" + searchParam + "%")), cb.like(root.get("roleCode").as(String.class), cb.literal("%" + searchParam + "%"))));
        }
        // 角色id
        if (roleId != null && !("".equals(roleId.trim()))) {
            list.add(cb.equal(root.get("id").as(String.class), roleId));
        }
        // 管理员角色标识
        if (category != null && category > 0) {
            list.add(cb.equal(root.get("category").as(Integer.class), category));
        }
        //多角色Id
        if (roleids != null && roleids.size() > 0) {
            list.add(root.get("id").as(String.class).in(roleids));
        }
        Predicate[] predicates = new Predicate[list.size()];
        query.where(list.toArray(predicates));
        query.orderBy(cb.asc(root.get("roleType").as(String.class)), cb.asc(root.get("roleProperty").as(String.class)), cb.asc(root.get("roleCode").as(String.class)));
        return query.getRestriction();
    }

      参考 org.springframework.data.jpa.repository.support.SimpleJpaRepository 的 findAll方法,如下

    public List<T> findAll(Specification<T> spec) {
        return getQuery(spec, (Sort) null).getResultList();
    }
    
    protected TypedQuery<T> getQuery(Specification<T> spec, Sort sort) {
    
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<T> query = builder.createQuery(getDomainClass());
    
        Root<T> root = applySpecificationToCriteria(spec, query);
        query.select(root);
    
        if (sort != null) {
            query.orderBy(toOrders(sort, root, builder));
        }
    
        return applyRepositoryMethodMetadata(em.createQuery(query));
    }
    
    private TypedQuery<T> applyRepositoryMethodMetadata(TypedQuery<T> query) {
    
        if (metadata == null) {
            return query;
        }
    
        LockModeType type = metadata.getLockModeType();
        TypedQuery<T> toReturn = type == null ? query : query.setLockMode(type);
    
        applyQueryHints(toReturn);
    
        return toReturn;
    }
  • 相关阅读:
    Docker安装Redis
    C语言读取Java的字节码文件的第一步
    springdataredis之RedisMessageListenerContainer源码解析
    【转载】logback自定义目录及输出格式
    【转载】springbootdependencies 跟 springbootstarterparent
    Docker安装PostgreSQL数据库
    SpringBoot整合Redis实现发布订阅功能实践
    iso 刻录软件 openwrt
    python selenium 获取UA 睁yan
    cnvd 获取最新cookie (反爬) 睁yan
  • 原文地址:https://www.cnblogs.com/hujunzheng/p/6656178.html
Copyright © 2011-2022 走看看