cn.chahuyun.hibernateplus.HibernateFactory Maven / Gradle / Ivy
package cn.chahuyun.hibernateplus;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
import org.hibernate.query.criteria.JpaCriteriaQuery;
import org.hibernate.query.criteria.JpaRoot;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* hibernate工厂
*
* @author Moyuyanli
* @date 2024/7/18 10:42
*/
@Slf4j
public class HibernateFactory {
private static HibernateFactory factory;
private final SessionFactory sessionFactory;
protected HibernateFactory(SessionFactory session) {
this.sessionFactory = session;
}
protected static void setFactory(HibernateFactory factory) {
HibernateFactory.factory = factory;
}
public static SessionFactory getSession() {
return factory.sessionFactory;
}
/**
* 查询一个单一对象
*
* @param tClass 对象类
* @param key 主键
* @param 对象类Class
* @return 对象 或 null
*/
public static T selectOne(Class tClass, Object key) {
return factory.sessionFactory.fromSession(session -> session.find(tClass, key));
}
/**
* 查询一个单一对象
*
* 参数格式:
* key -> 对象字段
* value -> 条件
*
* 如果查询结果为多个,只拿第一个
* 如果想获取多个结果,请使用{@link #selectList}
* 更多自定义查询请自行使用 {@link SessionFactory} 建立查询
*
* @param tClass 对象类
* @param params 参数列表
* @param 对象类Class
* @return 对象 或 null
*/
public static T selectOne(Class tClass, Map params) {
if (params.isEmpty()) {
return null;
}
return factory.sessionFactory.fromSession(session -> {
JpaCriteriaQuery query = getQuery(tClass, params, session);
List list = session.createQuery(query).list();
if (list == null || list.isEmpty()) {
return null;
} else {
return list.get(0);
}
});
}
/**
* 查询集合
*
*
* @param tClass 对象类
* @param params 参数列表
* @param 对象类Class
* @return 结果集
*/
public static List selectList(Class tClass, Map params) {
if (params.isEmpty()) {
return null;
}
return factory.sessionFactory.fromSession(session -> {
JpaCriteriaQuery query = getQuery(tClass, params, session);
return session.createQuery(query).list();
});
}
/**
* 查询集合
*
*
* @param tClass 对象类
* @param 对象类Class
* @return 结果集
*/
public static List selectList(Class tClass) {
return factory.sessionFactory.fromSession(session -> {
JpaCriteriaQuery query = getQuery(tClass, new HashMap<>(), session);
return session.createQuery(query).list();
});
}
/**
* 查询集合
*
*
* @param tClass 对象类
* @param key 条件字段
* @param param 条件值
* @param 对象类Class
* @return 结果集
*/
public static List selectList(Class tClass, String key, Object param) {
if (key == null || param == null) {
return new ArrayList<>(1);
}
return factory.sessionFactory.fromSession(session -> {
HibernateCriteriaBuilder builder = session.getCriteriaBuilder();
JpaCriteriaQuery query = builder.createQuery(tClass);
JpaRoot from = query.from(tClass);
query.select(from);
query.where(builder.equal(from.get(key), param));
return session.createQuery(query).list();
});
}
/**
* 保存或更新
* 如果主键为0或null,则新增
*
* @param object 对象
* @param 对象class
* @return 新对象
*/
public static T merge(T object) {
if (object == null) {
return null;
}
return factory.sessionFactory.fromTransaction(session -> session.merge(object));
}
/**
* 删除一个对象
*
* @param object 对象
* @return true 删除成功
*/
public static Boolean delete(Object object) {
if (object == null) {
return false;
}
return factory.sessionFactory.fromTransaction(session -> {
try {
session.remove(object);
return true;
} catch (Exception e) {
log.debug(e.getMessage());
return false;
}
});
}
@NotNull
private static JpaCriteriaQuery getQuery(Class tClass, Map params, Session session) {
HibernateCriteriaBuilder builder = session.getCriteriaBuilder();
JpaCriteriaQuery query = builder.createQuery(tClass);
JpaRoot from = query.from(tClass);
query.select(from);
for (Map.Entry entry : params.entrySet()) {
query.where(builder.equal(from.get(entry.getKey()), entry.getValue()));
}
return query;
}
}