SpringBoot JPA实践之表达式自定义属性与方法
JPA 
                                    admin 发布于:2022-06-25 12:02:44
阅读:loading
本篇文章来实现一下SpringBoot JPA的@Query注解中支持的表达式自定义与方法自定义,首先来说这两个功能可能并没有什么应用价值,同时在集成JPA环境时存在一些自定义的实现,并未在实际项目中实践,所以个人不是很建议实际去应用它们,但是作为一些有实践意义的知识点来学习参考总归是有较大的意义的,毕竟这也是一个知识点的深度挖掘。一直想着扩展一下JPA的动态查询条件,却机缘巧合的实现了自定义的表达式与自定义方法的集成,总归是令人欣喜的。
JPA原生SQL提供内置的变量{h-schema}、{h-domain}、${h-catalog}、#{#entityName}等等,今天本文将实现在SQL中内置一些我们自己的属性和方法,类似{h-schema},如表达式的调用为“?#{cdd.chendd}”,方法的调用为“?#{hello()}”,cdd为自定义属性前缀,chendd为属性名称,将输出属性值;hello()为内置的函数类中的方法名称调用,返回String类型的值。详细实现过程参考如下:
package cn.chendd.example.jpa.user;
import ...;
/**
 * UserTest
 *
 * @author chendd
 * @date 2020/4/21 20:14
 */
public class UserBasicTest extends ApplicationTest {
    @Resource
    private UserRepository userRepository;
    @Test
    public void test() {
        List<User> userList = this.userRepository.queryAll();
        System.out.println(userList);
    }
}package cn.chendd.example.jpa.user.repository;
import ...;
import java.util.List;
/**
 * 用户Repository
 *
 * @author chendd
 * @date 2020/4/21 0:56
 */
public interface UserRepository extends BaseRepository<User, String> {
    @Query(nativeQuery = true , value = "select id , name from User where 1 = 1 and ?#{cdd.chendd} = '666' and 'hello' = ?#{hello()} ")
    List<User> queryAll();
}package cn.chendd.example.jpa.configuration;
import ...;
/**
 * @author chendd
 * @date 2022/6/19 23:54
 */
@Component
public class JpaEvaluationContextExtension implements EvaluationContextExtension {
    @Override
    public String getExtensionId() {
        return "cdd";
    }
    @Override
    public Map<String, Function> getFunctions() {
        Map<String , Function> maps = Maps.newHashMap();
        Method[] methods = MethodUtils.getMethodsWithAnnotation(ConditionFunctions.class, Transient.class);
        if (methods != null) {
            for (Method method : methods) {
                maps.put(method.getName() , new Function(method));
            }
        }
        return maps;
    }
    @Override
    public Map<String, Object> getProperties() {
        Map<String, Object> map = Maps.newHashMap();
        map.put("chendd" , "666");
        return map;
    }
    /**
     * 自定义方法类
     *
     * @author chendd
     * @date 2022/6/19 23:55
     */
    public static class ConditionFunctions {
        @Transient
        public static String hello() {
            return "hello chendd";
        }
    }
}
点赞
                 
            
                 
            
                 
            
                 
            
                 
            
发表评论
        
        当前回复:作者
        
    
    
    评论列表
留言区
- SpringBoot JPA实践之框架搭建
- SpringBoot JPA实践之BaseRepository
- SpringBoot JPA实践之自动生成Entity
- SpringBoot JPA实践之API介绍
- SpringBoot JPA实践之方法属性名查询
- SpringBoot JPA实践之Example对象查询
- SpringBoot JPA实践之Named查询
- SpringBoot JPA实践之@Query查询之参数传参方式
- SpringBoot JPA实践之@Query查询之动态查询条件
- SpringBoot JPA实践之@Query查询之接口结果集
- SpringBoot JPA实践之@Query查询之分页实现
- SpringBoot JPA实践之EntityManage查询返回自定义DTO
- SpringBoot JPA实践之EntityManage查询返回自定义DTO的代理实现
- SpringBoot JPA实践之Specification查询分页
- SpringBoot JPA实践之小技巧汇总
 
                                     
                                     
                                     
                                     
                                     
                                     
                    