Srping框架中使用@query注解实现复杂查询
2024-10-06 07:26:25
【自己项目代码】
@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的位置。
自己项目代码
package com.jspxcms.core.repository; import java.util.Collection;
import java.util.Date;
import java.util.List; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository; import com.jspxcms.common.orm.Limitable;
import com.jspxcms.core.domain.User; /**
* UserDao
*
* @author liufang
*
*/
public interface UserDao extends Repository<User, Integer>, UserDaoPlus {
public Page<User> findAll(Specification<User> spec, Pageable pageable); public List<User> findAll(Specification<User> spec, Limitable limitable); public User findOne(Integer id); public User save(User bean); public void delete(User bean); @Modifying
@Query("update User bean set bean.recommend=?2,bean.birthDate=?3 where bean.id=?1")
public void recommended(Integer id,Integer recommend,Date birth); @Modifying
@Query("update User bean set bean.status=?2 where bean.id=?1")
public void checkUser(Integer id, int status); // ------------------------------------ public User findByUsername(String username); public User findByQqOpenid(String qqOpenid); public User findByWeiboUid(String weiboUid); public User findByValidationTypeAndValidationKey(String type, String key); @Query("select count(*) from User bean where bean.username=?1")
public long countByUsername(String username); @Query("select count(*) from User bean where bean.org.id in ?1")
public long countByOrgId(Collection<Integer> orgIds); @Query("select count(*) from User bean where bean.group.id in ?1")
public long countByGroupId(Collection<Integer> groupIds); @Query("select bean from User bean where bean.org.id=?1 and bean.group.id=?2")
public List<User> findByOrgIdAndGroupId(int orgId,int groupId); @Query("select bean from User bean where bean.org.parent.id=?1 and bean.group.id=?2")
public List<User> findByParentOrgIdAndGroupId(int orgId,int groupId); @Query("select count(bean.id) from User bean where bean.group = 3")
public long findByCk(); @Query("select count(bean.id) from User bean where bean.group = 4")
public long findByinvestor(); }
【参考内容:http://www.itnose.net/detail/6095818.html】
在Spring框架中,关于从数据表获取数据有不同的方法,当数据查询比较简单时,可以通过继承JpaRepository<T, L> 使用findBy***方法,通过分析方法名来实现查询,T表示要查询的数据表所对应的实体,L表示该实体主键的类型,比如Long。关于findBy方法如何通过分析方法名来实现查询,网上资料很多,不做赘述。
如果查询的数据比较复杂,查询条件比较复杂时,可以考虑使用JPA的@query方法来实现查询。关于使用方法,下面做简单介绍: 1.首先Dao层需继承Repository<T, L>,T为实体名,L为该实体的主键类型。
2.写查询方法,并使用@query进行注解,例如:
@query(select u from User u where name=?1 and age=?2)
List findUser(String name,int age);
在这里,User为实体名,参数中的name值在执行时可以赋值给?1的位置。
但是,事实上,如果只是从一个数据表中获取数据,使用上面所说的findBy**方法即可,并不需要使用@query来实现。
但是,当查询条件比较复杂,无法使用findBy方法时,就可以考虑使用@query.先给出一个例子:
@Query("select max(jobinfo.processes) from Jobinfo jobinfo,Taskinfo taskinfo where taskinfo.jobid=jobinfo.id and (shenweistatus=4 or shenweistatus=5) and queuename=?1")
List findMaxNuclear(String queuename); 通过这条语句,可以看到其涉及到两个实体,查询的是max,用Findby是无法实现的,但是这个例子就可以很好的实现。而关于1中的T就没有多大意义,应该是随便写语句中涉及到的一个实体即可。
关于其他的复杂查询都可以通过这种方法实现。
再看下面这个例子:
@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")
List findQueuenameByName(String name); 里边涉及到了模糊查询,在SQL中,模糊查询应该是 like ‘%AA%’等形式的,但是这里的注解中如果加入了%,会报错,怎么解决呢?可以在调用该方法是人为的将%传进去,比如findQueuenameByName(“%”+name+”%”),这样就可以实现模糊查询。
需要注意的是,我测试发现,这种方法只能查询一个字段,如果要查询多个字段,只能写多个方法,得到多个list,最后将查询结果拼到一块。(至于到底能不能一次查询多个字段,正在探索中。。。。)
最新文章
- 再谈 Mysql解决中文乱码
- eclipse服务器add and remove 工程时出现there are no resources that can be added or removed from the server
- python redis使用心得
- javascript 内部对象(1)——Math 对象
- Tomcat发布项目时,浏览器地址栏图标的问题
- linux中的livecd、liveDVD和其他安装方式简介
- HTML滚动条
- Cannot initialise keyboard run ./nano-X
- cocos2d-x -------之笔记篇 3D动作说明
- 开发者需要了解的WebKit
- spring 初始化
- 010Edit手写PE
- 【ARTS】01_16_左耳听风-20190225~20190303
- HTN规划 jshop2
- Text Justification 文本左右对齐
- WAV文件格式解析及处理
- 【调研与分析】标杆学习、知识管理和竞争情报的关系——From Team
- jeecg中ajax传值的前端js和后台代码
- 原生js 异步请求,responseXML解析
- P3321 [SDOI2015]序列统计 FFT+快速幂+原根