基于Spring的通用范型业务类

2024-05-01 21:58
文章标签 java 通用 业务 spring 范型

本文主要是介绍基于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 {@Beanpublic StringStore stringStore() {return new StringStore();}@Beanpublic 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>{@Autowiredprivate MyBaseRepository<T,ID> myBaseRepository;/*** 不含查询条件的分页查询** @param pageNo* @param pageSize* @return*/@Overridepublic Page<T> findAll(int pageNo, int pageSize) throws Exception {return myBaseRepository.findAll(new PageRequest(pageNo,pageSize));}/*** 不含查询条件的分页查询** @param pageable* @return* @throws Exception*/@Overridepublic Page<T> findAll(Pageable pageable) throws Exception {return myBaseRepository.findAll(pageable);}/*** 获取所有数据** @return*/@Overridepublic List<T> findAll() throws Exception {return myBaseRepository.findAll();}/*** 根据ID获取数据** @param va1* @return* @throws Exception*/@Overridepublic T findOne(ID va1) throws Exception {return myBaseRepository.findOne(va1);}/*** 新增指定数据** @param entity* @throws Exception*/@Overridepublic void save(T entity) throws Exception {myBaseRepository.save(entity);}/*** 更新指定数据** @param entity* @throws Exception*/@Overridepublic void update(T entity) throws Exception {}/*** 删除指定数据** @param va1* @throws Exception*/@Overridepublic void delete(ID va1) throws Exception {myBaseRepository.delete(va1);}/*** 删除指定数据* @param entity* @throws Exception*/@Overridepublic  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;@Autowiredpublic FileServiceImpl(FileDao fileDao) {this.fileDao = fileDao;}/*** 查询所有文件** @return*/@Overridepublic List<FileEntity> findAllFile() {return fileDao.findAllFile();}
}

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

这篇关于基于Spring的通用范型业务类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/952762

相关文章

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

SpringBoot监控API请求耗时的6中解决解决方案

《SpringBoot监控API请求耗时的6中解决解决方案》本文介绍SpringBoot中记录API请求耗时的6种方案,包括手动埋点、AOP切面、拦截器、Filter、事件监听、Micrometer+... 目录1. 简介2.实战案例2.1 手动记录2.2 自定义AOP记录2.3 拦截器技术2.4 使用Fi

最新Spring Security的基于内存用户认证方式

《最新SpringSecurity的基于内存用户认证方式》本文讲解SpringSecurity内存认证配置,适用于开发、测试等场景,通过代码创建用户及权限管理,支持密码加密,虽简单但不持久化,生产环... 目录1. 前言2. 因何选择内存认证?3. 基础配置实战❶ 创建Spring Security配置文件

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

Java Thread中join方法使用举例详解

《JavaThread中join方法使用举例详解》JavaThread中join()方法主要是让调用改方法的thread完成run方法里面的东西后,在执行join()方法后面的代码,这篇文章主要介绍... 目录前言1.join()方法的定义和作用2.join()方法的三个重载版本3.join()方法的工作原