com.luna.common.repository.UserRepository2Impl.java Source code

Java tutorial

Introduction

Here is the source code for com.luna.common.repository.UserRepository2Impl.java

Source

/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.luna.common.repository;

import com.luna.common.entity.BaseInfo;
import com.luna.common.entity.SchoolInfo;
import com.luna.common.entity.User;
import com.luna.common.entity.search.Searchable;
import com.luna.common.repository.callback.DefaultSearchCallback;
import com.luna.common.repository.callback.SearchCallback;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * <p>?DAO@Repository??Repository??</p>
 * <pre>
 *     JpaRepository?+<jpa:repositories repository-impl-postfix="Impl"></jpa:repositories>repository-impl-postfix
 *     repository-impl-postfixImpl
 * </pre>
 * <p>User: Zhang Kaitao
 * <p>Date: 13-1-14 ?4:30
 * <p>Version: 1.0
 */
public class UserRepository2Impl {

    private String findAllQL = "from User o where 1=1 ";
    private String countAllQL = "select count(o) from User o where 1=1 ";

    @PersistenceContext
    private EntityManager entityManager;

    private RepositoryHelper repositoryHelper = new RepositoryHelper(User.class);

    public BaseInfo findBaseInfoByUserId(Long userId) {
        String ql = "select bi from BaseInfo bi where bi.user.id=?1";
        Query query = entityManager.createQuery(ql);
        query.setParameter(1, userId);
        query.setMaxResults(1);
        List<BaseInfo> baseInfoList = query.getResultList();
        if (baseInfoList.size() > 0) {
            return baseInfoList.get(0);
        }
        return null;
    }

    public List<SchoolInfo> findAllSchoolTypeByUserId(Long userId) {
        String ql = "select si from SchoolInfo si where si.user.id=?1";
        Query query = entityManager.createQuery(ql);
        query.setParameter(1, userId);
        return query.getResultList();
    }

    /**
     * ?/?
     *
     * @param searchable
     * @return
     */
    public Page<User> findAllByDefault(final Searchable searchable) {
        long total = countAllByDefault(searchable);
        List<User> contentList = repositoryHelper.findAll(findAllQL, searchable, SearchCallback.DEFAULT);
        return new PageImpl(contentList, searchable.getPage(), total);
    }

    /**
     * ?
     *
     * @param searchable
     * @return
     */
    public long countAllByDefault(final Searchable searchable) {
        return repositoryHelper.count(countAllQL, searchable, SearchCallback.DEFAULT);
    }

    private SearchCallback customSearchCallback = new DefaultSearchCallback() {
        @Override
        public void prepareQL(StringBuilder hql, Searchable search) {
            if (search.containsSearchKey("realname")) {
                hql.append(
                        " and exists(select 1 from BaseInfo bi where o = bi.user and bi.realname like :realname )");
            }
        }

        @Override
        public void setValues(Query query, Searchable search) {
            if (search.containsSearchKey("realname")) {
                query.setParameter("realname", "%" + search.getValue("realname") + "%");
            }
        }
    };

    /**
     * ?
     *
     * @param searchable
     * @return
     */
    public long countAllByCustom(final Searchable searchable) {
        return repositoryHelper.count(countAllQL, searchable, customSearchCallback);
    }

    /**
     * ?/?
     *
     * @param searchable
     * @return
     */
    public Page<User> findAllByCustom(final Searchable searchable) {
        long total = countAllByCustom(searchable);
        List<User> contentList = repositoryHelper.findAll(findAllQL, searchable, customSearchCallback);
        return new PageImpl(contentList, searchable.getPage(), total);
    }

}