基于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

相关文章

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma