Java的泛型封装方式

Java的泛型封装方式

package base;

import java.util.List;

public interface DaoSupport<T> {

    /**
     * 保存实体
     * 
     * @param entity
     */
    void save(T entity);

    /**
     * 删除实体
     * 
     * @param id
     */
    void delete(Long id);

    /**
     * 更新实体
     * 
     * @param entity
     */
    void update(T entity);

    /**
     * 按id查询
     * 
     * @param id
     * @return
     */
    T getById(Long id);

    /**
     * 按id查询
     * 
     * @param ids
     * @return
     */
    List<T> getByIds(Long[] ids);

    /**
     * 查询所有
     * 
     * @return
     */
    List<T> findAll();

}
package base;

import java.lang.reflect.ParameterizedType;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

// @Transactional注解可以被继承
// @Transactional注解对父类中声明的方法无效
@Transactional
@SuppressWarnings("unchecked")
public abstract class DaoSupportImpl<T> implements DaoSupport<T> {

    @Resource
    private SessionFactory sessionFactory;
    private Class<T> clazz;

    public DaoSupportImpl() {
        // 使用反射技术得到T的真实类型
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型
        this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 获取第一个类型参数的真实类型
        System.out.println("clazz ---> " + clazz);
    }

    /**
     * 获取当前可用的Session
     * 
     * @return
     */
    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    public void save(T entity) {
        getSession().save(entity);
    }

    public void update(T entity) {
        getSession().update(entity);
    }

    public void delete(Long id) {
        Object obj = getById(id);
        if (obj != null) {
            getSession().delete(obj);
        }
    }

    public T getById(Long id) {
        if (id == null) {
            return null;
        } else {
            return (T) getSession().get(clazz, id);
        }
    }

    public List<T> getByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return Collections.EMPTY_LIST;
        } else {
            return getSession().createQuery(//
                    "FROM " + clazz.getSimpleName() + " WHERE id IN (:ids)")//
                    .setParameterList("ids", ids)//
                    .list();
        }
    }

    public List<T> findAll() {
        return getSession().createQuery(//
                "FROM " + clazz.getSimpleName())//
                .list();
    }


}
package service;

import pojo.User;
import base.DaoSupport;

public interface UserService extends DaoSupport<User> {

}
package service.impl;

import java.util.List;

import base.DaoSupportImpl;

import pojo.User;
import service.UserService;

public class UserServiceImpl extends DaoSupportImpl<User> implements UserService {


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值