hibernate basic CRUD implemented BaseBao

之前用过mybatis-plus,里面有个通用dao类,直接继承就可以实现简单的增删改查,当时只是用,但是现在做项目是hibernate,而且是原生的那种。。

There are probably dozens of tables, dozens of dao to write a simple CRUD, how the code is bloated feeling,
so I thought, can add generic reflection
with generic aim is that we're not sure that entity, so after we spent generics know, just on the line classes with the
reflection is to get generic type, which means that in practice, that can be passed in the type, why it is, because our investigation is to be operated entity of the class, and now we have a problem, how to get the actual generic type passed, Baidu a bit, searched the tools

package com.zhizhuo.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class GenericsUtils {
    /**
     * 通过反射,获得定义Class时声明的父类的范型参数的类型. 如public BookManager extends
     * GenricManager<Book>
     * 
     * @param clazz The class to introspect
     * @return the first generic declaration, or <code>Object.class</code> if cannot be determined
     */
    public static Class getSuperClassGenricType(Class clazz) {
        return getSuperClassGenricType(clazz, 0);
    }

    /**
     * 通过反射,获得定义Class时声明的父类的范型参数的类型. 如public BookManager extends GenricManager<Book>
     * 
     * @param clazz clazz The class to introspect
     * @param index the Index of the generic ddeclaration,start from 0.
     */
    public static Class getSuperClassGenricType(Class clazz, int index)
            throws IndexOutOfBoundsException {
        Type genType = clazz.getGenericSuperclass();
        if (!(genType instanceof ParameterizedType)) {
            return Object.class;
        }
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        if (index >= params.length || index < 0) {
            return Object.class;
        }
        if (!(params[index] instanceof Class)) {
            return Object.class;
        }
        return (Class) params[index];
    }
}

Below achieve
BaseDao

package com.zhizhuo.dao;

import com.zhizhuo.dao.impl.DepartmentDaoImpl;
import com.zhizhuo.model.entity.Department;
import com.zhizhuo.util.GenericsUtils;
import com.zhizhuo.util.HibernateSessionFactory;
import com.zhizhuo.util.PageHelper;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;

import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.Root;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @作者 1543057945
 * @时间 2019/12/18 14:50
 * @param <T>
 */
public abstract class BaseDao<T> {

    private Class clazz;

    /**
     * 初始化
     */
    public BaseDao() {
        //获取该类上标注的泛型类型
        Class clazz = GenericsUtils.getSuperClassGenricType(getClass());
        this.clazz=clazz;
        System.out.println(clazz);
    }
    public Session getSession() {
        return HibernateSessionFactory.getSession();
    }
    /**
     * 插入方法
     * @param t
     * @return 插入的主键
     */
    public Serializable insert(T t) {
        return getSession().save(t);
    }
    /**
     * 更新方法
     * @param t
     */
    public void updateById(T t) {
        getSession().update(t);
    }
    /**
     * 删除
     * @param t
     */
    public void deleteById(T t) {
        getSession().delete(t);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    public int deleteByIds(List<T> ids){
        CriteriaDelete crd = getSession()
                .getEntityManagerFactory()
                .createEntityManager()
                .getCriteriaBuilder()
                .createCriteriaDelete(clazz);
        Root root = crd.from(clazz);
        //创建表达式
        crd.where(root.in(ids));
        return getSession().createQuery(crd).executeUpdate();
    }
    /***
     * 通过id查找
     * @param id
     * @return
     */
    public T findById(Serializable id) {
        return (T) getSession().get(this.clazz, id);
    }
    /**
     * 查找全部
     * @return
     */
    public List<T> findAll(){
       return getSession().createCriteria(this.clazz).list();
    }
    /**
     * 分页查询
     * @param ph
     * @return
     */
    public List<T> findPageList(PageHelper ph){
        Criteria cr = getSession().createCriteria(this.clazz);
        cr.setFirstResult(ph.getStartIndex());
        cr.setMaxResults(ph.getPageSize());
        return cr.list();
    }

    /**
     * 分页查询
     * @param startIndex 开始索引
     * @param effset 偏移量
     * @return
     */
    public List<T> findPageList(int startIndex,int effset){
        Criteria cr = getSession().createCriteria(this.clazz);
        cr.setFirstResult(startIndex);
        cr.setMaxResults(effset);
        return cr.list();
    }

    /**
     * 查询总条数
     * @return
     */
    public Long findCount(){
       return (Long) getSession()
               .createCriteria(clazz)
               .setProjection(Projections.rowCount())
               .uniqueResult();
    }

    public static void main(String[] args) {
        Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
        List<String> list= Arrays.asList("297e0cf76f17c400016f17f5464f0000","297e0cf76f17c400016f17f551980001");
        BaseDao<Department> departmentDao = new DepartmentDaoImpl();
        List<Department> collect = list.stream().map(e -> {
            Department department = new Department();
            department.setDepartmentId(e);
            return department;
        }).collect(Collectors.toList());
        departmentDao.deleteByIds(collect);

       tx.commit();
       HibernateSessionFactory.closeSession();
    }
}

Is used,
Here Insert Picture Description
it is important : BaseDao <> angle brackets which entity is to be operated, in which BaseDao configuration of this type may be obtained or

test

 public static void main(String[] args) {
        Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
        List<String> list= Arrays.asList("297e0cf76f17c400016f17f5464f0000","297e0cf76f17c400016f17f551980001");
        BaseDao<Department> departmentDao = new DepartmentDaoImpl();
        List<Department> collect = list.stream().map(e -> {
            Department department = new Department();
            department.setDepartmentId(e);
            return department;
        }).collect(Collectors.toList());
        departmentDao.deleteByIds(collect);

       tx.commit();
       HibernateSessionFactory.closeSession();
    }

Well, so far

Published 34 original articles · won praise 6 · views 3643

Guess you like

Origin blog.csdn.net/qq_35986709/article/details/103601072