All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mycomm.dao.dao4comm.base.MyEntityManager Maven / Gradle / Ivy

There is a newer version: 1.0.8
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycomm.dao.dao4comm.base;

import com.mycomm.dao.dao4comm.ResultHelp;
import com.mycomm.dao.dao4comm.orm.MySession;
import com.mycomm.dao.dao4comm.util.MyQueryCondition;
import java.lang.reflect.ParameterizedType;
import java.util.LinkedHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import java.util.ArrayList;

/**
 *
 * @author jw362j
 * @param 
 */
public abstract class MyEntityManager extends BaseDaoSupport {

    private static final Logger log = LoggerFactory.getLogger(MyEntityManager.class);
    @SuppressWarnings("FieldMayBeFinal")
    private Class entityClass;
    private boolean tableExistCheck;
//    private SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制  

    public MyEntityManager() {
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型
        this.entityClass = (Class) pt.getActualTypeArguments()[0]; // 获取第一个类型参数的真实类型
        System.out.println("clazz ---> " + entityClass);
    }

    @PostConstruct
    public void init() {
        initTable();
    }

    public long getCount() {
        if (entityClass == null) {
            log.info("entityClass is null!");
            return 0l;
        }
        return myOrmSession.getCount(entityClass);
    }

    public long save(T entity) {
        if (entity == null) {
            return 0l;
        }
        return myOrmSession.save(entity);
    }

    public int[] saveList(List entitys) {
        if (entitys == null || entitys.size() <= 0) {
            return null;
        }
        return myOrmSession.saveList((List) entitys);
    }

    public void deleteList(long[] ids) {
        if (ids == null || ids.length <= 0) {
            return;
        }

        myOrmSession.deleteList(entityClass, ids);
    }

    public void delete(long... entityids) {
        if (entityids == null || entityids.length <= 0) {
            return;
        }
        long[] deletes = new long[entityids.length];
        int x = 0;
        for (long myid : entityids) {
            deletes[x++] = myid;
        }
        deleteList(deletes);
    }

    public int[] updateList(List entities) {
        if (entityClass == null || entities == null) {
            log.info("the entityClass is null or entity is null in save!");
            return null;
        }
        return myOrmSession.updates(entities.toArray());
    }

    public int update(T entity) {
        if (entity == null) {
            return -1;
        }
        List ls = new ArrayList();
        ls.add(entity);
        int rs[] = updateList(ls);
        ls.clear();
        if (rs == null || rs.length <= 0) {
            return 0;
        }
        return rs[0];
    }

    public T find(long entityId) {
        return (T) myOrmSession.find(entityClass, entityId);
    }

    public ResultHelp getScrollData(long firstindex, int maxresult, String[] fields, String whereSql, Object[] queryParams, int[] queryParamsTypes, Map orderby) {
        return (ResultHelp) myOrmSession.find(entityClass, new MyQueryCondition(firstindex, maxresult, fields, whereSql, queryParams, queryParamsTypes, orderby));
    }

    public ResultHelp getScrollData(MyQueryCondition condition) {
        if (condition == null) {
            return null;
        }
        return getScrollData(condition.getFirstindex(), condition.getMaxresult(), condition.getColums(), condition.getWheresql(), condition.getQueryParamsNames(), condition.getQueryParamsTypes(), condition.getOrderby());
    }

    public ResultHelp getScrollData(long firstindex, int maxresult, String wherejpql, Object[] queryParams) {
        return getScrollData(firstindex, maxresult, null, wherejpql, queryParams, null, null);
    }

    public ResultHelp getScrollData(long firstindex, int maxresult, LinkedHashMap orderby) {
        return getScrollData(firstindex, maxresult, null, null, null, null, orderby);
    }

    public ResultHelp getScrollData(long firstindex, int maxresult) {
        return getScrollData(firstindex, maxresult, null, null, null, null, null);
    }

    public ResultHelp getScrollData() {
        return getScrollData(-1, -1);
    }

    private void initTable() {
        if (tableExistCheck) {
            return;
        }
        myOrmSession.checkTableExist(entityClass);
        tableExistCheck = true;
    }
    
    public long[] findIDs(T clz, MyQueryCondition condition){
        return myOrmSession.findIDs(entityClass, condition);
    }
    public T[] findByIds(T clz, long[] theId){
        return (T[])myOrmSession.findByIds(entityClass, theId);
    }

    public MySession getMyOrmSession() {
        return myOrmSession;
    }
    
}