• Srping框架中使用@query注解实现复杂查询


    【自己项目代码】

    @Query("select bean from User bean where bean.org.id=?1 and bean.group.id=?2")
    public List<User> findByOrgIdAndGroupId(int orgId,int groupId);

    问题:?1 和?2 

    回答:参数中的值在执行时可以赋值给?1或者?2的位置。

     1 自己项目代码
     2 package com.jspxcms.core.repository;
     3 
     4 import java.util.Collection;
     5 import java.util.Date;
     6 import java.util.List;
     7 
     8 import org.springframework.data.domain.Page;
     9 import org.springframework.data.domain.Pageable;
    10 import org.springframework.data.jpa.domain.Specification;
    11 import org.springframework.data.jpa.repository.Modifying;
    12 import org.springframework.data.jpa.repository.Query;
    13 import org.springframework.data.repository.Repository;
    14 
    15 import com.jspxcms.common.orm.Limitable;
    16 import com.jspxcms.core.domain.User;
    17 
    18 /**
    19  * UserDao
    20  * 
    21  * @author liufang
    22  * 
    23  */
    24 public interface UserDao extends Repository<User, Integer>, UserDaoPlus {
    25     public Page<User> findAll(Specification<User> spec, Pageable pageable);
    26 
    27     public List<User> findAll(Specification<User> spec, Limitable limitable);
    28 
    29     public User findOne(Integer id);
    30 
    31     public User save(User bean);
    32 
    33     public void delete(User bean);
    34     
    35     @Modifying
    36     @Query("update User bean set bean.recommend=?2,bean.birthDate=?3 where bean.id=?1")
    37     public void recommended(Integer id,Integer recommend,Date birth);
    38     
    39     @Modifying
    40     @Query("update User bean set bean.status=?2 where bean.id=?1")
    41     public void checkUser(Integer id, int status);
    42 
    43     // ------------------------------------
    44 
    45     public User findByUsername(String username);
    46 
    47     public User findByQqOpenid(String qqOpenid);
    48 
    49     public User findByWeiboUid(String weiboUid);
    50 
    51     public User findByValidationTypeAndValidationKey(String type, String key);
    52 
    53     @Query("select count(*) from User bean where bean.username=?1")
    54     public long countByUsername(String username);
    55 
    56     @Query("select count(*) from User bean where bean.org.id in ?1")
    57     public long countByOrgId(Collection<Integer> orgIds);
    58 
    59     @Query("select count(*) from User bean where bean.group.id in ?1")
    60     public long countByGroupId(Collection<Integer> groupIds);
    61     
    62     @Query("select bean from User bean where bean.org.id=?1 and bean.group.id=?2")
    63     public List<User> findByOrgIdAndGroupId(int orgId,int groupId);
    64     
    65     @Query("select bean from User bean where bean.org.parent.id=?1 and bean.group.id=?2")
    66     public List<User> findByParentOrgIdAndGroupId(int orgId,int groupId);
    67     
    68     @Query("select count(bean.id) from User bean where bean.group = 3")
    69     public long findByCk();
    70     
    71     @Query("select count(bean.id) from User bean where bean.group = 4")
    72     public long findByinvestor();
    73     
    74 }

    【参考内容:http://www.itnose.net/detail/6095818.html】

     1      在Spring框架中,关于从数据表获取数据有不同的方法,当数据查询比较简单时,可以通过继承JpaRepository<T, L> 使用findBy***方法,通过分析方法名来实现查询,T表示要查询的数据表所对应的实体,L表示该实体主键的类型,比如Long。关于findBy方法如何通过分析方法名来实现查询,网上资料很多,不做赘述。 
     2     如果查询的数据比较复杂,查询条件比较复杂时,可以考虑使用JPA的@query方法来实现查询。关于使用方法,下面做简单介绍: 
     3 
     4 1.首先Dao层需继承Repository<T, L>,T为实体名,L为该实体的主键类型。 
     5 2.写查询方法,并使用@query进行注解,例如: 
     6 @query(select u from User u where name=?1 and age=?2)
     7 List findUser(String name,int age);
     8 在这里,User为实体名,参数中的name值在执行时可以赋值给?1的位置。 
     9 但是,事实上,如果只是从一个数据表中获取数据,使用上面所说的findBy**方法即可,并不需要使用@query来实现。 
    10 但是,当查询条件比较复杂,无法使用findBy方法时,就可以考虑使用@query.先给出一个例子: 
    11 @Query("select max(jobinfo.processes) from Jobinfo jobinfo,Taskinfo taskinfo where taskinfo.jobid=jobinfo.id and (shenweistatus=4 or shenweistatus=5) and queuename=?1")
    12 List findMaxNuclear(String queuename);
    13 
    14 通过这条语句,可以看到其涉及到两个实体,查询的是max,用Findby是无法实现的,但是这个例子就可以很好的实现。而关于1中的T就没有多大意义,应该是随便写语句中涉及到的一个实体即可。 
    15 关于其他的复杂查询都可以通过这种方法实现。 
    16 再看下面这个例子: 
    17 @Query("select jobinfo.queuename from Jobinfo jobinfo,Taskinfo taskinfo where taskinfo.jobid=jobinfo.id and (shenweistatus=1 or shenweistatus=3) and jobinfo.queuename like ?1 group by jobinfo.queuename")
    18 List findQueuenameByName(String name);
    19 
    20 里边涉及到了模糊查询,在SQL中,模糊查询应该是 like ‘%AA%’等形式的,但是这里的注解中如果加入了%,会报错,怎么解决呢?可以在调用该方法是人为的将%传进去,比如findQueuenameByName(“%”+name+”%”),这样就可以实现模糊查询。 
    21 需要注意的是,我测试发现,这种方法只能查询一个字段,如果要查询多个字段,只能写多个方法,得到多个list,最后将查询结果拼到一块。(至于到底能不能一次查询多个字段,正在探索中。。。。)
  • 相关阅读:
    上传视频到七牛云django端实现
    课程全文检索接口
    搜索引擎工作原理
    创建订单并生成支付链接接口
    支付宝支付流程
    通过课程查询商品信息
    如何使用 RESTClient 调试微信支付接口
    关于HTML使用ComDlg ActiveX 无法弹出相应对话框的问题1
    Android自定义View的实现方法,带你一步步深入了解View(四)
    Android视图状态及重绘流程分析,带你一步步深入了解View(三)
  • 原文地址:https://www.cnblogs.com/dixinyunpan/p/5856884.html
Copyright © 2020-2023  润新知