@Query(value = "SELECT ua FROM User ua "
+ "WHERE ua.targetName LIKE %?1% "
+ "and (?2 is null or ua.createTime > ?2) "
+ "and (?3 is null or ua.createTime <= ?3) "
+ "and (?4 is null or ua.action = ?4) ")
public Page<User> findByActionAndTargetNameAndCreateTime(String targetName, Date startTime, Date endTime, String action, Pageable page);
@Query(value = "SELECT g.id, g.group_name AS groupName, count(u.id) AS totalUserCount "
+ "FROM admin_group g LEFT JOIN admin_user u ON g.id = u.group_id "
+ "WHERE g.id in (:groupIds) GROUP BY g.id", nativeQuery = true)
List<Map<String, Object>> findAllGroup(@Param("groupIds") List<Integer> groupIds);
Based on above item, additional pagination features can be added on with countQuery:
##https://blog.csdn.net/weixin_45415885/article/details/101571356
@Query(value = "SELECT * FROM emp " +
"WHERE if(?1= '' OR ?1 is null ,1=1, code like %?1%) " +
" AND if(?2= '' OR ?2 is null ,1=1, `name` like %?2%) " +
" GROUP BY dept_id ",
countQuery = "SELECT count(*) FROM (SELECT count(*) FROM emp "+
"WHERE if(?1= '' OR ?1 is null ,1=1, code like %?1%) " +
" AND if(?2= '' OR ?2 is null ,1=1, `name` like %?2%) " +
"GROUP BY dept_id ) tempTable",
nativeQuery = true)
Page<Map> findEmp( String code, String name);
一般只在一边设Eager,JPA接口默认为一对多为Lazy,多对一为Eager,但是Hibernate反向工程生成Entity时,多对一为Lazy,需要手动改为Eager。 https://blog.csdn.net/tongyang8820/article/details/71157948
ManyToOne循环引用问题
一端 @JsonManagedReference 多端 // @JsonBackReference // @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "groupId") @JsonIgnoreProperties("users")
JPA返回元组 No converter found capable of converting from type
[0;39m Illegal DefaultValue null for parameter type integer @ApiModelProperty(name = "id", value = "id", example = "1") // 需要添加example
ManyToOne 外键无法更新 @JoinColumn(name = "groupId", insertable = true, updatable = true)
动态拼接JPA自带的Query 的技巧
request parameter 设置成required,并且定义默认值
聚合查询,用原生jpa query没法好好做,改成nativeQuery,返沪对象是一个map
Based on above item, additional pagination features can be added on with
countQuery
:alternatives: http://jinzili.cc/2017/10/11/Spring-Data-JPA%E4%B8%AD%E7%9A%84nativeQuery%E5%92%8Cpageable/