com.ny.apps.dao.BaseDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.ny.apps.dao.BaseDaoImpl.java

Source

/**
 * 
 * @Title BaseDaoImpl.java
 * @Description TODO
 * Copyright: Copyright (c) 2013, Smiletony and/or its affiliates. All rights reserved.
 * SMILETONY PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @author NY
 * @date 2013-10-11 ?10:16:31
 * 
 */
package com.ny.apps.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @Description TODO
 * @author <a href="mailto:mynameisny@qq.com">Tony Joseph</a>
 * @version 1.0
 *
 */
@Service(value = "baseDao")
public class BaseDaoImpl implements BaseDao {
    @PersistenceContext
    private EntityManager entityManager;

    /* ? Javadoc
     * @see com.ny.apps.dao.BaseDao#persist(java.lang.Object)
     */
    @Override
    @Transactional
    public void persist(Object entity) {
        entityManager.persist(entity);
    }

    /* ? Javadoc
     * @see com.ny.apps.dao.BaseDao#merge(java.lang.Object)
     */
    @Override
    @Transactional
    public <T> T merge(T entity) {
        return entityManager.merge(entity);
    }

    /* ? Javadoc
     * @see com.ny.apps.dao.BaseDao#findFirst(java.lang.String, java.lang.Object[])
     */
    @Override
    public Object findFirst(String queryStr, Object... params) {
        List<?> list = find(0, 1, queryStr, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    /* ? Javadoc
     * @see com.ny.apps.dao.BaseDao#find(java.lang.String, java.lang.Object[])
     */
    @Override
    public List<?> find(String queryStr, Object... params) {
        Query query = prepareParameterizedQuery(entityManager.createQuery(queryStr), params);
        List<?> list = query.getResultList();
        return list;
    }

    public List<?> findByNativeSQL(String queryStr, Object... params) {
        Query query = prepareParameterizedQuery(entityManager.createNativeQuery(queryStr), params);
        List<?> list = query.getResultList();
        return list;
    }

    /* ? Javadoc
     * @see com.ny.apps.dao.BaseDao#find(int, int, java.lang.String, java.lang.Object[])
     */
    @Override
    public List<?> find(int start, int delta, String queryStr, Object... params) {
        Query query = prepareParameterizedQuery(entityManager.createQuery(queryStr), params);
        query.setFirstResult(start);
        query.setMaxResults(delta);
        List<?> list = query.getResultList();
        return list;
    }

    public List<?> findByNativeSQL(int start, int delta, String queryStr, Object... params) {
        Query query = prepareParameterizedQuery(entityManager.createNativeQuery(queryStr), params);
        List<?> list = query.getResultList();
        return list;
    }

    /* ? Javadoc
     * @see com.ny.apps.dao.BaseDao#update(java.lang.String, java.lang.Object[])
     */
    @Override
    @Transactional
    public int update(String queryStr, Object... params) {
        Query query = (prepareParameterizedQuery(entityManager.createQuery(queryStr), params));
        int result = query.executeUpdate();
        return result;
    }

    /* ? Javadoc
     * @see com.ny.apps.dao.BaseDao#load(java.lang.Class, java.io.Serializable)
     */
    @Override
    @Transactional
    public <T> T load(Class<T> entityType, Serializable id) {
        T t = entityManager.find(entityType, id);
        return t;
    }

    /* ? Javadoc
     * @see com.ny.apps.dao.BaseDao#remove(java.lang.Object)
     */
    @Override
    @Transactional
    public void remove(Object entity) {
        entityManager.remove(entity);
    }

    public <T> void RemoveAll(Class<T> entityType, Set<Serializable> ids) {
        if (ids.isEmpty()) {
            return;
        }
        String whereClause = buildOrIdWhereClause(ids.size(), "e.id");
        update(concatenateString("delete from ", entityType.getSimpleName(), "e where ", whereClause),
                ids.toArray());
    }

    /**
     * ??
     * @param query 
     * @param params ?
     * @return
     */
    public static Query prepareParameterizedQuery(Query query, Object... params) {
        if (params == null) {
            return query;
        }
        int i = 0;
        for (Object param : params) {
            if (param == null) {
                continue;
            }
            query.setParameter(++i, param);
        }
        return query;
    }

    /**
     * 
     * @param params ?
     * @return ?
     */
    public static String concatenateString(Object... params) {
        StringBuilder sb = new StringBuilder();
        for (Object param : params) {
            sb.append(param);
        }
        return sb.toString();
    }

    /**
     * ???'or'???.<br />
     * :<br />
     * : range = 3, refKey = "userId"<br />
     * : "(userId = ? or userId = ? or userId = ?)".
     * 
     * @param range ??
     * @param refKey ??
     * @return 'or'???
     */
    public static String buildOrIdWhereClause(int range, String refKey) {
        StringBuilder sb = new StringBuilder();
        if (range <= 0) {
            return " (1 = 1)";
        }
        sb.append("(");
        for (int i = 0; i < range; i++) {
            sb.append(refKey + " = ?");
            if (i != (range - 1)) {
                sb.append(" or ");
            }
        }
        sb.append(")");
        return sb.toString();
    }
}