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

com.zys.mybatis.dao.base.impl.DefaultBaseDaoImpl Maven / Gradle / Ivy

There is a newer version: 0.2.7
Show newest version
package com.zys.mybatis.dao.base.impl;

import com.zys.mybatis.annotation.Id;
import com.zys.mybatis.condition.ConditionKey;
import com.zys.mybatis.crud.Add;
import com.zys.mybatis.crud.Delete;
import com.zys.mybatis.crud.Query;
import com.zys.mybatis.crud.Update;
import com.zys.mybatis.dao.base.BaseDao;
import com.zys.mybatis.dto.KeyValue;
import com.zys.mybatis.page.BasePage;
import com.zys.mybatis.page.Page;
import com.zys.mybatis.utils.QueryUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;


/**
 * @author zys
 * @version 1.0
 * @date 2021/1/29 15:50
 */
public class DefaultBaseDaoImpl {

    @Autowired
    private D d;

    public  E query(E e) {
        Query query = new Query<>(e, true).setParams(QueryUtils.getParam(e));
        List es = QueryUtils.setOtherField(e.getClass(), Collections.singletonList(d.query(query)));
        if (CollectionUtils.isEmpty(es)) {
            return null;
        }
        return es.get(0);
    }

    public  E query(Query query) {
        List es = QueryUtils.setOtherField(query.getEntity(), Collections.singletonList(d.query(query)));
        if (CollectionUtils.isEmpty(es)) {
            return null;
        }
        return es.get(0);
    }

    public  E queryById(E e){
        KeyValue keyValue = QueryUtils.getIdName(e);
        return d.query(new Query<>(e,false).eq(keyValue.getKey(),keyValue.getValue()));
    }

    public  int count(E e) {
        return d.count(new Query<>(int.class,QueryUtils.getTableName(e.getClass()),"count(1)").setParams(QueryUtils.getParam(e)));
    }

    public  int count(Query query) {
        return d.count(query);
    }

    public  Page paging(Query query, BasePage basePage) {
        int count = d.count(new Query<>(int.class, query.getTable(), "count(1)").setParams(query.getParams()));
        if (count > 0) {
            List es = QueryUtils.setOtherField(query.getEntity(),d.queryAll(query.limit(basePage)));
            return new Page<>(basePage,count,es);
        }
        return new Page<>(basePage);
    }

    public  Page paging(Class eClass ,Object entity, BasePage basePage) {
        Map param = QueryUtils.getParam(entity);
        int count = d.count(new Query<>(int.class, QueryUtils.getTableName(eClass), "count(1)").setParams(param));
        if (count > 0) {
            List es = QueryUtils.setOtherField(eClass,d.queryAll(new Query<>(eClass,false).setParams(param).limit(basePage)));
            return new Page<>(basePage,count,es);
        }
        return new Page<>(basePage);
    }

    public  List queryAll(Query query) {
        return QueryUtils.setOtherField(query.getEntity(), d.queryAll(query));
    }

    public  List queryAll(E e) {
        return QueryUtils.setOtherField(e.getClass(),d.queryAll(new Query<>(e,true).setParams(QueryUtils.getParam(e)).setPage(QueryUtils.getPage(e))));
    }

    @Transactional(rollbackFor = Exception.class)
    public  int insert(E e) {
        Add add = new Add<>(e);
        int insert = d.insert(add);
        if (insert > 0) {
            for (Field field : e.getClass().getDeclaredFields()) {
                Id id = field.getDeclaredAnnotation(Id.class);
                if (id != null) {
                    //目前没做任务逻辑
                    MetaObject metaObject = SystemMetaObject.forObject(e);
                    metaObject.setValue(field.getName(),add.getId());
                    break;
                }
            }
        }
        return insert;
    }

    public  int batchInsert(List e) {
        return d.batchInsert(new Add<>(e));
    }

    public  int update(Update e) {
        return d.update(e);
    }

    public  int updateById(E e) {
        KeyValue keyValue = QueryUtils.getIdName(e);
        return d.update(new Update<>(e).eq(keyValue.getKey(),keyValue.getValue(),true));
    }

    public  int delete(E e) {
        return d.delete(new Delete<>(e.getClass()).setParams(QueryUtils.getParam(e)));
    }

    public  int delete(Delete query) {
        return d.delete(query);
    }

    public  int deleteById(E e) {
        KeyValue keyValue = QueryUtils.getIdName(e);
        return d.delete(new Delete<>(e.getClass()).eq(keyValue.getKey(),keyValue.getValue()));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy