java spring 简单封装出自己的DAO Service实现

package com.jade.basung.bss.module.common;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.util.PagingList;
import com.jade.basung.bss.BaseException;

/**
 * dao层公共接口
 *
 * @date 15/8/28 下午5:23
 * @version 1.0
 */
public interface IBaseDao
{
    /**
     * 增加方法 - 通用
     * @param model 通用对象模型
     * @throws BaseException
     */
    void add(M model) throws BaseException;

    /**
     * 更新方法 - 通用
     * @param model 通用对象模型
     * @throws BaseException
     */
    void update(M model) throws BaseException;

    /**
     * 删除指定对象 - 通用
     * @param model 通用对象模型
     * @throws BaseException
     */
    void delete(M model) throws BaseException;

    /**
     * 根据id查找指定对象
     * @param model 通用对象模型
     * @param id 指定的id
     * @return 通用对象
     */
    M get(Class model, PK id);

    /**
     * 根据Criteria查询符合条件的信息
     * @param clazz 类名称
     * @param criteria Criteria对象
     * @return
     */
    PagingList listall(Class clazz, Criteria criteria);
}

package com.jade.basung.bss.module.common;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.util.PagingList;
import com.jade.basung.bss.BaseException;

/**
 * 数据管理层通用接口
 *
 * @version 1.0
 * @date 15/8/28 下午5:24
 */
public interface IBaseManager
{
    /**
     * 增加方法 - 通用
     * @param model 通用对象模型
     * @throws BaseException
     */
    void add(M model) throws BaseException;

    /**
     * 更新方法 - 通用
     * @param model 通用对象模型
     * @throws BaseException
     */
    void update(M model) throws BaseException;

    /**
     * 删除指定对象 - 通用
     * @param ids 要删除对象的id,可多个
     * @throws BaseException
     */
    void delete(Class clazz, PK... ids) throws BaseException;

    /**
     * 根据id查找指定对象
     * @param model 通用对象模型
     * @param id 指定的id
     * @return 通用对象
     */
    M get(Class model, PK id);

    /**
     * 根据Criteria查询符合条件的信息
     * @param clazz 类名
     * @param criteria Criteria对象
     * @return
     */
    PagingList listall(Class clazz, Criteria criteria);
}

package com.jade.basung.bss.module.common.impl;

import java.util.Date;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TemporalType;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;
import com.jade.basung.bss.BaseException;
import com.jade.basung.bss.module.common.IBaseDao;

/**
 * dao层公共接口实现
 *
 * @date 15/8/28 下午5:46
 * @version 1.0
 */
@Named("bss_baseDao")
public class BaseDao implements IBaseDao
{
    @PersistenceContext (unitName = "default")
    private EntityManager entityManager;

    public void add(M model) throws BaseException
    {
        entityManager.persist(model);
    }

    public void update(M model) throws BaseException
    {
        entityManager.merge(model);
    }

    public void delete(M model) throws BaseException
    {
       entityManager.remove(model);
    }

    public M get(Class model,PK id)
    {
        return entityManager.find(model, id);
    }

    public PagingList listall(Class clazz, Criteria criteria)
    {
        String sql = "SELECT "+clazz.getSimpleName().toLowerCase()+" FROM "+clazz.getSimpleName()+" "+clazz.getSimpleName().toLowerCase()+" ";
        return SqlUtils.sqlQuery(sql, entityManager, criteria);
    }

    /**
     * 执行insert,update,delete批处理语句  未能全部实现-待定
     */
    protected int execteBulk(final String hql, final Object... paramlist) {
        Query query = entityManager.createQuery(hql);
        setParameters(query, paramlist);
        Object result = query.executeUpdate();
        return result == null ? 0 : ((Integer) result).intValue();
    }

    protected int execteNativeBulk(final String natvieSQL, final Object... paramlist) {
        Query query = entityManager.createNativeQuery(natvieSQL);
        setParameters(query, paramlist);
        Object result = query.executeUpdate();
        return result == null ? 0 : ((Integer) result).intValue();
    }

    protected void setParameters(Query query, Object[] paramlist) {
        if (paramlist != null) {
            for (int i = 0; i < paramlist.length; i++) {
                if(paramlist[i] instanceof Date) {
                    query.setParameter(i, (Date)paramlist[i], TemporalType.TIMESTAMP);
                } else {
                    query.setParameter(i, paramlist[i]);
                }
            }
        }
    }
}

package com.jade.basung.bss.module.common.impl;

import javax.ejb.TransactionAttribute;
import javax.inject.Named;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.util.PagingList;
import com.jade.basung.bss.BaseException;
import com.jade.basung.bss.module.common.IBaseDao;
import com.jade.basung.bss.module.common.IBaseManager;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通用数据管理接口实现
 *
 * @date 15/8/28 下午7:29
 * @version 1.0
 */
@Named("bss_baseManager")
public abstract class BaseManagerImpl implements IBaseManager
{
    public IBaseDao baseDao;

    public abstract void setBaseDao(IBaseDao baseDao);

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void add(M model) throws BaseException
    {
        baseDao.add(model);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void update(M model) throws BaseException
    {
        baseDao.update(model);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void delete(Class clazz, PK... ids) throws BaseException
    {
        for (PK id : ids) {
            M model = baseDao.get(clazz, id);
            if (model != null) {
                baseDao.delete(model);
            }
        }
    }

    public M get(Class clazz,PK id)
    {
        return baseDao.get(clazz, id);
    }

    public PagingList listall(Class clazz, Criteria criteria)
    {
        return baseDao.listall(clazz, criteria);
    }
}

先封装一套通用的 DAO Service  然后对应的Dao  Service 业务逻辑再继承公用的接口 

下面是具体的使用场景




package com.jade.basung.opration.collect;

import com.jade.basung.bss.module.common.IBaseDao;

/**
 * 收藏数据访问接口
 *
 * @date 15/11/23 上午11:51
 * @version 1.0
 */
public interface CollectDao extends IBaseDao
{
}



package com.jade.basung.opration.collect.impl;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.jade.basung.bss.module.common.impl.BaseDao;
import com.jade.basung.opration.collect.Collect;
import com.jade.basung.opration.collect.CollectDao;

/**
 * 收藏数据访问接口实现
 *
 * @date 15/11/23 下午3:11
 * @version 1.0
 */
@Named("ld_collectDao")
public class JpaCollectDao extends BaseDao implements CollectDao
{
    @PersistenceContext (unitName="default")
    private EntityManager entityManager;
}

package com.jade.basung.opration.collect;

import com.jade.basung.bss.BaseException;
import com.jade.basung.bss.module.common.IBaseManager;
import com.jade.basung.opration.OprationException;

/**
 * 收藏管理访问接口
 *
 * @date 15/11/23 上午11:51
 * @version 1.0
 */
public interface CollectManager extends IBaseManager
{
    public void addCollect(Collect collect)
            throws OprationException, BaseException;

    public void delCollect(Long... ids)
            throws OprationException, BaseException;
}

package com.jade.basung.opration.collect.impl;

import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;

import com.jade.basung.bss.BaseException;
import com.jade.basung.bss.module.common.IBaseDao;
import com.jade.basung.bss.module.common.impl.BaseManagerImpl;
import com.jade.basung.commodities.goods.GoodsDao;
import com.jade.basung.opration.OprationException;
import com.jade.basung.opration.collect.Collect;
import com.jade.basung.opration.collect.CollectDao;
import com.jade.basung.opration.collect.CollectManager;
import com.jade.basung.opration.collect.Enum.CollectType;
import com.jade.basung.opration.comment.Comment;
import com.jade.basung.opration.news.NewsDao;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收藏管理访问接口实现
 *
 * @date 15/11/23 下午3:09
 * @version 1.0
 */
@Named("ld_collectManager")
public class CollectManagerImpl extends BaseManagerImpl implements CollectManager
{
    private CollectDao collectDao;
    private GoodsDao goodsDao;
    private NewsDao newsDao;

    @Override @Inject
    public void setBaseDao(@Named("ld_collectDao")IBaseDao baseDao)
    {
        this.baseDao = baseDao;
        this.collectDao = (CollectDao)baseDao;
    }

    @Inject
    public void setGoodsDao(@Named("ld_goodsDao")GoodsDao goodsDao)
    {
        this.goodsDao = goodsDao;
    }

    @Inject
    public void setNewsDao(@Named("ld_newsDao")NewsDao newsDao)
    {
        this.newsDao = newsDao;
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void addCollect(Collect collect) throws BaseException, OprationException
    {
        collectDao.add(collect); //增加收藏

        if (CollectType.Goods == collect.getType()) {
            goodsDao.collectsCount(collect.getRelId(), 1);
        } else if(CollectType.News == collect.getType()){
            newsDao.collectsCount(collect.getRelId(), 1);
        }
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void delCollect(Long... ids) throws OprationException, BaseException
    {
        for (Long id: ids) {
            Collect collect = collectDao.get(Collect.class, id);
            if (null == collect) {
                throw new OprationException("err.jade.operation.collect.collect_no_exist");
            }

            if (CollectType.Goods == collect.getType()) {
                goodsDao.collectsCount(collect.getRelId(), -1);
            } else if(CollectType.News == collect.getType()){
                newsDao.collectsCount(collect.getRelId(), -1);
            }
            collectDao.delete(collect);
        }
    }
}



spring 依赖注入  继承父类接口  子类重写父类抽象方法   父类引用指向子类对象(接口父类 A=new  子类实现 B)   然后是  spring jpa   @TransactionAttribute 事务回滚 简单的底层封装完毕


本人亲测可用    (PS:——请勿无脑拷贝我的代码,程序编码人员应该有自己的思想,而不是重复代码生产工具)

你可能感兴趣的:(springmvc,封装出自己的底层DAO,SERVICE)