基于Spring的通用范型业务类

我们在做日常开发的时候在写业务的时候都会写常用的增删改查,每个业务对象都要写一遍这是非常繁琐和浪费时间的事情。
在DAO层面Spring 提供了Spring data jpa来帮我们解决这个问题,只需要声明一个接口继承自JpaRepository就能使用spring为我们提供的一些基本的数据库操作方法,但是在Service层怎么办呢?
在spring4之后我们有了解决办法,spring4的依赖注入提供了新的功能:基于范型的依赖注入
我们看看官方的说明,官方是在讲quallfier的时候带过的

7.9.5 Using generics as autowiring qualifiers
In addition to the @Qualifier annotation, it is also possible to use Java generic types as an implicit form of qualification. For example, suppose you have the following configuration:

@Configuration
public class MyConfiguration {

    @Bean
    public StringStore stringStore() {
        return new StringStore();
    }

    @Bean
    public IntegerStore integerStore() {
        return new IntegerStore();
    }
}
Assuming that beans above implement a generic interface, i.e. Store<String> and Store<Integer>, you can @Autowire the Store interface and the generic will be used as a qualifier:

@Autowired
private Store<String> s1; // <String> qualifier, injects the stringStore bean

@Autowired
private Store<Integer> s2; // <Integer> qualifier, injects the integerStore bean
Generic qualifiers also apply when autowiring Lists, Maps and Arrays:

// Inject all Store beans as long as they have an <Integer> generic
// Store<String> beans will not appear in this list
@Autowired
private List<Store<Integer>> s;

讲的是StringStore、IntegerStore分别实现了一个Store接口,这样在进行注入的时候就能自动注入准确的bean

@Autowired
private Store<String> s1; // <String> qualifier, injects the stringStore bean

@Autowired
private Store<Integer> s2; // <Integer> qualifier, injects the integerStore bean

好了,有了这个认识那么我们就来设计以下怎么来实现我们的通用范型Service
首先我们还是要定义一下我们的Repository

package com.spring.common.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;

import java.io.Serializable;

/**
 * Created by jacky on 16-8-26.
 */
@NoRepositoryBean
public interface MyBaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
}

package com.spring.dao;

import com.spring.common.dao.MyBaseRepository;
import com.spring.entity.FileEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ com.spring.dao
 * @author cj
 * 2017/12/23
 **/
@Repository
public interface FileDao extends MyBaseRepository<FileEntity,String>{

    /**
     * 只查询文件名、文件id、文件类型不加载具体的文件内容
     * @return
     */
    @Query("select new FileEntity (f.fileid,f.filename,f.filetype,f.fileext)from FileEntity f")
    List<FileEntity> findAllFile();
}

下面我们来定义我们的BaseService

package com.spring.common.service;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

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

public interface BaseService<T,ID extends Serializable> {
    /**
     * 不含查询条件的分页查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<T> findAll(int pageNo, int pageSize) throws Exception;

    /**
     * 不含查询条件的分页查询
     * @param pageable
     * @return
     * @throws Exception
     */
    public Page<T> findAll(Pageable pageable) throws Exception;


    /**
     * 获取所有数据
     *
     * @return
     */
    public List<T> findAll() throws Exception;

    /**
     * 根据ID获取数据
     * @param va1
     * @return
     * @throws Exception
     */
    public T findOne(ID va1) throws Exception;

    /**
     * 新增指定数据
     * @param entity
     * @throws Exception
     */
    public void save(T entity) throws Exception;

    /**
     * 更新指定数据
     * @param entity
     * @throws Exception
     */
    public void update(T entity) throws Exception;

    /**
     * 删除指定数据
     * @param va1
     * @throws Exception
     */
    public void delete(ID va1) throws Exception;

    /**
     * 删除指定数据
     * @param entity
     * @throws Exception
     */
    public void delete(T entity) throws Exception;
}

package com.spring.common.service;

import com.spring.common.dao.MyBaseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

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

/**
 * com.hw.myp2c.common.service
 * jackycheng
 * 2017/11/22
 **/
public abstract class BaseServiceImpl<T,ID extends Serializable> implements BaseService<T,ID>{

    @Autowired
    private MyBaseRepository<T,ID> myBaseRepository;

    /**
     * 不含查询条件的分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<T> findAll(int pageNo, int pageSize) throws Exception {
        return myBaseRepository.findAll(new PageRequest(pageNo,pageSize));
    }

    /**
     * 不含查询条件的分页查询
     *
     * @param pageable
     * @return
     * @throws Exception
     */
    @Override
    public Page<T> findAll(Pageable pageable) throws Exception {
        return myBaseRepository.findAll(pageable);
    }

    /**
     * 获取所有数据
     *
     * @return
     */
    @Override
    public List<T> findAll() throws Exception {
        return myBaseRepository.findAll();
    }

    /**
     * 根据ID获取数据
     *
     * @param va1
     * @return
     * @throws Exception
     */
    @Override
    public T findOne(ID va1) throws Exception {
        return myBaseRepository.findOne(va1);
    }

    /**
     * 新增指定数据
     *
     * @param entity
     * @throws Exception
     */
    @Override
    public void save(T entity) throws Exception {
        myBaseRepository.save(entity);
    }

    /**
     * 更新指定数据
     *
     * @param entity
     * @throws Exception
     */
    @Override
    public void update(T entity) throws Exception {

    }

    /**
     * 删除指定数据
     *
     * @param va1
     * @throws Exception
     */
    @Override
    public void delete(ID va1) throws Exception {
        myBaseRepository.delete(va1);
    }


    /**
     * 删除指定数据
     * @param entity
     * @throws Exception
     */
    @Override
    public  void delete(T entity) throws Exception{
        myBaseRepository.delete(entity);
    }
}

这时候我们就定义好了我们的范型Service,接下来我们就需要定义我们具体的业务类了,注意我的类声明时的extends和implements

package com.spring.service;

import com.spring.common.service.BaseService;
import com.spring.entity.FileEntity;

import java.util.List;

/**
 * com.spring.service
 * @author jacky
 * @date 2017/12/23
 **/
public interface FileService extends BaseService<FileEntity, String> {

    /**
     * 查询所有文件
     * @return
     */
    List<FileEntity> findAllFile();
}

package com.spring.service;

import com.spring.common.service.BaseServiceImpl;
import com.spring.dao.FileDao;
import com.spring.entity.FileEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * com.spring.service
 *
 * @author jacky
 * @date 2017/12/23
 *
 * 与数据库交互实现数据库层面的文件读写
 *
 **/
@Service
@Transactional(readOnly = true)
public class FileServiceImpl extends BaseServiceImpl<FileEntity, String> implements FileService {
    private FileDao fileDao;

    @Autowired
    public FileServiceImpl(FileDao fileDao) {
        this.fileDao = fileDao;
    }

    /**
     * 查询所有文件
     *
     * @return
     */
    @Override
    public List<FileEntity> findAllFile() {
        return fileDao.findAllFile();
    }
}

这样一些基础的增删改查我们只需要在我们的BaseService中进行定义和实现就行了,在具体的业务类中我们就可以定义一些特定的业务方法了。
详细代码地址spring

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值