首页 > 代码库 > javaEE项目抽取BaseService

javaEE项目抽取BaseService

软件152 王程燕

1、书写BaseService

package cn.buaa.core.service;

import java.io.Serializable;
import java.util.List;


public interface BaseService<T> {

    //新增
    public void save(T entity);
    
    //更新
    public void update(T entity);
    
    //根据id删除
    public void delete(Serializable id);
    
    //根据id查询
    public T findObjectById(Serializable id);
    
    //查询列表
    public List<T> findObjects(); 
}

  2、实现接口

package cn.buaa.core.service.impl;

import java.io.Serializable;
import java.util.List;

import cn.buaa.core.dao.BaseDao;
import cn.buaa.core.service.BaseService;

public class BaseServiceImpl<T> implements BaseService<T> {
    
    private BaseDao<T> baseDao;
    public void setBaseDao(BaseDao<T> baseDao) {
        this.baseDao = baseDao;
    }
    @Override
    public void save(T entity) {
        baseDao.save(entity);
    }

    @Override
    public void update(T entity) {
        baseDao.update(entity);
    }

    @Override
    public void delete(Serializable id) {
        baseDao.delete(id);
    }

    @Override
    public T findObjectById(Serializable id) {
        return baseDao.findObjectById(id);
    }

    @Override
    public List<T> findObjects() {
        return baseDao.findObjects();
    }

}

  3、 设置父类中的Dao

package cn.buaa.nsfw.info.service.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

import cn.buaa.core.service.impl.BaseServiceImpl;
import cn.buaa.nsfw.info.dao.InfoDao;
import cn.buaa.nsfw.info.entity.Info;
import cn.buaa.nsfw.info.service.InfoService;

@Service("infoService")
public class InfoServiceImpl extends BaseServiceImpl<Info> implements InfoService {

    //采用这种方式注入,为了设置父类中的dao
    private InfoDao infoDao;
    
    @Resource
    public void setInfoDao(InfoDao infoDao) {
        super.setBaseDao(infoDao);
        this.infoDao = infoDao;
    }
}

  

javaEE项目抽取BaseService