基于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实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2