SpringBoot | JPA basic query and multi-condition query, parameter is empty judgment

JPA Basic Query

  • A query specification provided by Spring Data JPA, query statement keywords, simple SQL can be named according to the method, omitting to write sql statement.
关键字                    方法命名                                 sql where字句
And                     findByNameAndPwd                    where name= ? and pwd =?
Or                      findByNameOrSex                     where name= ? or sex=?
Is,Equals               findById,findByIdEquals             where id= ?
Between                 findByIdBetween                     where id between ? and ?
LessThan                findByIdLessThan                    where id < ?
LessThanEquals          findByIdLessThanEquals              where id <= ?
GreaterThan             findByIdGreaterThan                 where id > ?
GreaterThanEquals       findByIdGreaterThanEquals           where id > = ?
After                   findByIdAfter                       where id > ?
Before                  findByIdBefore                      where id < ?
IsNull                  findByNameIsNull                    where name is null
isNotNull,NotNull       findByNameNotNull                   where name is not null
Like                    findByNameLike                      where name like ?
NotLike                 findByNameNotLike                   where name not like ?
StartingWith            findByNameStartingWith              where name like ‘?%’
EndingWith              findByNameEndingWith                where name like ‘%?’
Containing              findByNameContaining                where name like ‘%?%’
OrderBy                 findByIdOrderByXDesc                where id=? order by x desc
Not                     findByNameNot                       where name <> ?
In                      findByIdIn(Collection<?> c)         where id in (?)
NotIn                   findByIdNotIn(Collection<?> c)      where id not in (?)
True                    findByAaaTue                        where aaa = true
False                   findByAaaFalse                      where aaa = false
IgnoreCase              findByNameIgnoreCase                where UPPER(name)=UPPER(?)

JPA multi-condition query (parameter is empty judgment) statement

I encountered a multi-condition query requirement in my work. I needed to query data based on name, gender, age, and serial number. The name needed to be ambiguously queried, and the parameters might be empty.

@Query(value = "select * from people where if(?1 !='',name like concat('%',?1,'%'),1=1) and if(?2 !='',sex=?2,1=1)"+
" and if(IFNULL(?3,'') !='',age=?3,1=1) and if(IFNULL(?4,'') !='',num=?4,1=1) ",nativeQuery = true)
List<People> find(String name,String sex,Integer age,Integer num);

tips:

  1. nativeQuery = trueThe meaning of is to use native SQL, that is, the SQL statement in the annotation will take effect, and false will not take effect.
  2. The meaning in the SQL statement ?1、?2、?3、?4represents the number of parameters in the method.
  3. The writing method of fuzzy query in SQL islike concat('%', ?1, '%')
  4. if(?1 !='',name like concat('%',?1,'%'),1=1)If the parameter name passed in is not "" (the Spring type is "" instead of null), the parameter is passed into the name. If it is empty, 1=1 means the parameter is true and has no effect on the query result. The syntax of IF satisfies the basic syntax of mysql, IF(expr1,expr2,expr3), if expr1 is true (expr1 <> 0 and expr1 <> NULL), then IF() returns expr2, otherwise returns expr3
  5. if(IFNULL(?3,'') !='',age=?3,1=1)Indicates that if the passed-in age is null, it will be replaced with an empty string, and then judge whether it is empty, and if it is not empty, the parameter will be passed into age, otherwise it will be ignored and will not affect the query result. IFNULLIt is a function in mysql, which is generally used to replace NULL values. IFNULL(value1,value2), to determine whether value1 is null, and if it is null, replace it with value2.
  6. When defining the parameter, define the value and use Integer. If you use int to define, when the input parameter is NULL, the program will report a null pointer error. The reason is that int in JAVA is a value type, not an object, and cannot be set to NULL, and integer is an object type, and can be set to NULL

Guess you like

Origin blog.csdn.net/u012294515/article/details/103155805