spring data 自定义接口
1 spring data jpa
虽然说spring data 提供了很多DAO 接口,但是依然可能不能满足我们日常的使用,所以,有时我们需要自定义接口方法。自定义接口方法步骤如下:
1. 创建自定义工厂类:CustomerJpaRepositoryFactoryBean, 需要继承JpaRepositoryFactoryBean
package org.zgf.spring.data.customer;
import java.io.Serializable;
import javax.persistence.EntityManager;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
/**
* 自定义工厂
* @author: zonggf
* @date: 2016年1月18日-下午2:16:18
*/
public class CustomerJpaRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
extends JpaRepositoryFactoryBean<T, S, ID> {
@Override
protected RepositoryFactorySupport createRepositoryFactory(
EntityManager entityManager) {
return new CustomerRepositoryFactory(entityManager);
}
private static class CustomerRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {
private EntityManager entityManager;
public CustomerRepositoryFactory(EntityManager entityManager) {
super(entityManager);
this.entityManager = entityManager;
}
protected Object getTargetRepository(RepositoryMetadata metadata) {
return new IBaseDaoImpl<T, I>((Class<T>) metadata.getDomainType(), entityManager);
}
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return IBaseDao.class;
}
}
}
2. 创建自定义接口:IBaseDao, 需要继承JpaRepository
package org.zgf.spring.data.customer;
import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
//必须使用该注解标明,此接口不是一个Repository Bean
@NoRepositoryBean
public interface IBaseDao <T, ID extends Serializable> extends JpaRepository<T, ID>{
//自定义接口方法
public void sayHello(String name);
}
3. 创建自定义接口实现 类:IBaseDaoImpl, 名称必须是 接口名+ Impl
package org.zgf.spring.data.customer;
import java.io.Serializable;
import javax.persistence.EntityManager;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
//必须使用该注解标明,此接口不是一个Repository Bean
@NoRepositoryBean
public class IBaseDaoImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
implements IBaseDao<T, ID> {
private EntityManager entityManager;
public IBaseDaoImpl(Class<T> domainClass, EntityManager em) {
super(domainClass, em);
this.entityManager = em;
}
@Override
public void sayHello(String name) {
System.out.println("entityManage:" + entityManager);
System.out.println("hello, " + name);
}
}
4. 配置文件中配置自定义工厂类:
<!-- 5. 配置 SpringData -->
<!-- 加入 jpa 的命名空间 扫描 Repository Bean 所在的 package, 自定义工厂类 -->
<jpa:repositories base-package="org.zgf.spring.data" entity-manager-factory-ref="entityManagerFactory" factory-class="org.zgf.spring.data.customer.CustomerJpaRepositoryFactoryBean"/>
5. 测试类
spring data 自定义接口的更多相关文章
- spring data 自定义查询
spring data 自定义查询 https://www.cnblogs.com/airycode/p/6535635.html 在方法接口上面使用@Query
- Spring Data Jpa接口简介
Repository接口 public interface Repository<T, ID> {....} 提供了按方法名称的查询方式: 提供了@Query的查询方式 可能遇到的错误: ...
- Spring data jpa中Query和@Query分别返回map结果集
引用: http://blog.csdn.net/yingxiake/article/details/51016234 http://blog.csdn.net/yingxiake/article/d ...
- Spring Data Elasticsearch 用户指南
https://www.jianshu.com/p/27e1d583aafb 翻译自官方文档英文版,有删减. BioMed Central Development Team version 2.1.3 ...
- Spring Data(一)概念和仓库的定义
Spring Data(一)概念和仓库的定义 Spring Data的主要任务是为数据访问提供一个相似的.一致的.基于Spring的编程模型,同时又保留着下面各个数据存储的特征.它使得使用数据访问技术 ...
- Spring MVC和Spring Data JPA之获取数据表数据放在List集合,显示在JSP页面
涉及到很多xml配置没写:只写具体实现的所有类 1.实体类 对应数据表SYS_SBGL, 主键是SBBM,主键是自动生成的uuid 数据表内容如下(有图有真相): package com.jinhet ...
- Spring Data JPA: 实现自定义Repository
一.前言 由于项目中的 实体(entity)默认都是继承一个父类(包含一些公共的属性,比如创建时间,修改时间,是否删除,主键id).为了实现逻辑删除,一般会自己实现RepositoryFactoryB ...
- Spring Data 之 Repository 接口
1. 介绍 Repository是一个空接口,即是一个标记性接口; 若我们定义的接口继承了Repository,则该接口会被IOC容器识别为一个 Repository Bean; 也可以通过@Repo ...
- Spring data JPA 理解(默认查询 自定义查询 分页查询)及no session 三种处理方法
简介:Spring Data JPA 其实就是JDK方式(还有一种cglib的方式需要Class)的动态代理 (需要一个接口 有一大堆接口最上边的是Repository接口来自org.springfr ...
随机推荐
- SQL Server查询所有存储过程信息、触发器、索引
1. [代码]查询所有存储过程 01 select Pr_Name as [存储过程], [参数]=stuff((select ','+[Parameter] 02 from ( 03 se ...
- C# 图书整理
C#测试驱动开发C#设计模式C#高级编程单元测试之道C#版:使用Nunit 继续添加......
- bat windows10系统垃圾清理---
@echo off color 0a title windows10系统垃圾清理--- echo ★☆ ★☆ ★☆ ★☆ ★☆★☆★☆ ★☆ ★☆ ★☆ ★☆★ echo ★☆ ★☆ ★☆ ★☆ ★☆ ...
- iOS NSMutableArray "removeObjectIdenticalTo" vs "removeObject"
NSMutableArray 有多种可以删除元素的方法. 其中 removeObject,removeObjectIdenticalTo 这两个方法是有区别的. [anArray removeObje ...
- python中的循环和编码,运算符, 格式化输出
1.while循环 现在让我们来看看python中的while循环 格式为 while 条件 循环体 (break) (continue) 中断循环的关键字有break和continue, brea ...
- G - Ice_cream's world I (并查集)
点击打开链接 ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_cream ...
- java中二维数组遍历
public class Demoshuzu2 { public static void main(String[] args) { int[][] arr2 = {{78,79,65, ...
- 老调重弹-access注入过主机卫
本文作者:i春秋签约作家——非主流 大家好,我是来自农村的非主流,今天就给在座的各位表演个绝活. 首先打开服务器上安装了主机卫士的网站. 尝试在变量id的值后面插入万恶的单引号,根据报错,我们可以分析 ...
- 把序列中的N个元素赋值给多个变量
说明: python中序列和散列 序列:可通过偏移量来进行切片的对象.列表.元组.字符串都属于序列. 散列:无法通过偏移量来进行切片的对象.比如 集合.字典 一. 变量的数量跟序列元素的个数一样 1. ...
- nginx实现动静分离--附nginx配置文件详解
转自http://www.cnblogs.com/1214804270hacker/p/9299462.html 一.认识访问静态资源与访问动态资源的区别 静态资源:指存储在硬盘内的数据,固定的数据, ...