简单的介绍一下使用场景,DAO层用Spring Data实现,dao 只有接口,实现类是容器启动时动态字节码生成,接口里定义方法,方法上@Query 里写JPQL查询语句。

基于以上的限制,如果对一个实体做查询,条件有多个,而且每个条件又不是必填的,怎么弄?参数肯定必须传,传Null或空字符串肯定不行,今天下午项目组里就有人遇到这个问题了。

我之前搞过,但是代码没找到,自己回想一下又试了试成了,然后代码丢给PG了。

这回代码贴出来,省得再丢,代码如下:
DAO接口

1
2
3
4
5
6
public interface ActivityDao extends JpaRepository<Activity, Integer> {
 
    @Query("SELECT a FROM Activity a WHERE (a.code=:code OR :code = null) AND (a.name=:name OR :name = null)")
    public List<Activity> findByCodeAndName(@Param("code") String code, @Param("name") String name);
 
}

Junit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:META-INF\\appContext.xml")
public class ActivityDaoTest extends AbstractJUnit4SpringContextTests {
 
    @Resource
    private ActivityDao dao;
 
    @Test
    public void test() {
        // List<Activity> list = dao.findByCodeAndName("33", "3");
        List<Activity> list = dao.findByCodeAndName("33", null);
        System.out.println(list.size());
    }
}

Spring Data JAP 多个不是必填的查询条件处理的更多相关文章

  1. spring data jap的使用 1

    最近一直在研究Spring Boot,今天为大家介绍下Spring Data JPA在Spring Boot中的应用,如有错误,欢迎大家指正. 先解释下什么是JPA JPA就是一个基于O/R映射的标准 ...

  2. Spring Data JPA整合REST客户端Feign时: 分页查询的反序列化报错的问题

    Type definition error: [simple type, class org.springframework.data.domain.Page]; nested exception i ...

  3. Spring Data JPA基本增删改查和JPQL查询(含完整代码和视频连接)

    问题:SpringDataJPA怎么使用? 一.考察目标 主要考核SpringDataJPA的用法 二.题目分析 spring data jpa 的使用步骤(下面有具体实现细节) 1.创建maven工 ...

  4. spring data jap操作

    package com.example.demo; import com.example.entity.UserJ; import com.example.respository.UserJRespo ...

  5. 【spring data jpa】jpa中criteria拼接in查询

    示例代码: public static Specification<GoodsType> where(final GoodsType goodsType){ return new Spec ...

  6. 【spring data jpa】 spring data jpa 中 时间格式设置between and 查询

    实例代码: //举报时间 Date createDate = entity.getCreateDate(); if (createDate != null){ predicates.add(cb.be ...

  7. JDBC、ORM、JPA、Spring Data JPA,傻傻分不清楚?一文带你厘清个中曲直,给你个选择SpringDataJPA的理由!

    序言 Spring Data JPA作为Spring Data中对于关系型数据库支持的一种框架技术,属于ORM的一种,通过得当的使用,可以大大简化开发过程中对于数据操作的复杂度. 本文档隶属于< ...

  8. 转:使用 Spring Data JPA 简化 JPA 开发

    从一个简单的 JPA 示例开始 本文主要讲述 Spring Data JPA,但是为了不至于给 JPA 和 Spring 的初学者造成较大的学习曲线,我们首先从 JPA 开始,简单介绍一个 JPA 示 ...

  9. 了解 Spring Data JPA

    前言 自 JPA 伴随 Java EE 5 发布以来,受到了各大厂商及开源社区的追捧,各种商用的和开源的 JPA 框架如雨后春笋般出现,为开发者提供了丰富的选择.它一改之前 EJB 2.x 中实体 B ...

随机推荐

  1. Microsoft SQL Server 2012 数据库安装图解教程

    本文部分引用以下文章: SQL Server 2012 安装图解教程(附sql2012下载地址)_MsSql_脚本之家 http://www.jb51.net/article/36049.htm SQ ...

  2. Winform 数据验证

    http://blog.scosby.com/post/2010/02/11/Validation-in-Windows-Forms.aspx 总结:1. CancelEventArgs e ,调用e ...

  3. ripple

     ripple模拟器非常好用,chrome上的插件 

  4. JS动态特性

    var arr=[]; arr['js']='jquery'; arr['css']='oocss'; var obj={}; for(var i in arr) { obj[i]=arr[i]; } ...

  5. 类似百度文库pdf2swf+flexpaper解决pdf在线阅读的效果

    1:工具准备swftools.exe 下载http://www.swftools.org/download.html 安装至D盘SWFTools提供了一系列将各种文件转成swf的工具:font2swf ...

  6. GPS(1)核心API及3种列出LocationProvider的方法

    GPS的常用API Android SDK为GPS提供了很多API,其中LocationManager类是这些API的核心.所有与GPS相关的操作都由LocationManager对象及其派生的对象完 ...

  7. Function 1 - hello world

    Function 1 - hello world Make a simple function called greet that returns the most-famous "hell ...

  8. poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)

    //题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...

  9. UVa 673 (括号配对) Parentheses Balance

    本来是当做水题来做的,后来发现这道题略坑. 首先输入的字符串可能是空串,所以我用了gets函数,紧接着就被scanf("%d", &n)后面的换行符坑掉了. 于是乎再加一句 ...

  10. RPi 2B 自动发送获取的IP到固定邮箱

    /************************************************************************* * RPi 2B 自动发送获取的IP到固定邮箱 * ...