【自己项目代码】

@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,最后将查询结果拼到一块。(至于到底能不能一次查询多个字段,正在探索中。。。。)

最新文章

  1. 再谈 Mysql解决中文乱码
  2. eclipse服务器add and remove 工程时出现there are no resources that can be added or removed from the server
  3. python redis使用心得
  4. javascript 内部对象(1)——Math 对象
  5. Tomcat发布项目时,浏览器地址栏图标的问题
  6. linux中的livecd、liveDVD和其他安装方式简介
  7. HTML滚动条
  8. Cannot initialise keyboard run ./nano-X
  9. cocos2d-x -------之笔记篇 3D动作说明
  10. 开发者需要了解的WebKit
  11. spring 初始化
  12. 010Edit手写PE
  13. 【ARTS】01_16_左耳听风-20190225~20190303
  14. HTN规划 jshop2
  15. Text Justification 文本左右对齐
  16. WAV文件格式解析及处理
  17. 【调研与分析】标杆学习、知识管理和竞争情报的关系——From Team
  18. jeecg中ajax传值的前端js和后台代码
  19. 原生js 异步请求,responseXML解析
  20. P3321 [SDOI2015]序列统计 FFT+快速幂+原根

热门文章

  1. [WCF安全3]使用wsHttpBinding构建基于SSL与UserName授权的WCF应用程序
  2. SPOJ8222 NSUBSTR - Substrings
  3. shell中的常用通配符,字符类
  4. 全文检索引擎Solr系列——整合中文分词组件mmseg4j
  5. phpcms v9 栏目伪静态完全自定义为栏目英文目录名
  6. js三种对话框
  7. Java的 final 关键字
  8. phpMyAdmin 应用程序“DEFAULT WEB SITE”中的服务器错误
  9. wii 入门之路--fatt
  10. Python中实现switchcase