This is the 11th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

Conditional constructor

Conditional constructors are used to write complex SQL

Matters needing attention

Transmission of Wrapper in RPC calls is not supported or approved

  1. wrapperVery heavy
  2. transmissionwrapperCan be compared to yourscontrollermapReceived value (develop instant gratification, maintain crematorium)
  3. The correctRPCCall posture is write oneDTOTransfer, the called party according toDTOPerform the corresponding operations

Stitching method

Query methods instructions The sample
eq Is equal to the= eq("username", "admin") —> username = 'admin'
ne Is equal to the= ne("username", "admin") —> username <> 'admin'
gt Is greater than> gt("age", "18") —> age > 18
ge Greater than or equal to> = ge("age", "18") —> age >= 18
lt Less than< lt("age", 18) —> age < 18
le Less than or equal to< = le("age", 18) —> age <= 18
between Between values 1 and 2 between("age", 18, 24) —> age BETWEEN 18 AND 24
notBetween It’s not between value 1 and value 2 notBetween("age", 18, 24) —> age NOT BETWEEN 18 AND 24
like Fuzzy query The like (" name ", "zhang") —> Name LIKE '% % %'
notLike Not fuzzy query inside NotLike (" name ", "zhang") —> Name NOT LIKE '% zhang %'
likeLeft Left fuzzy query LikeLeft (" name ", "three") —> Name LIKE '% 3 '
likeRight Right fuzzy query inside LikeRight (" name ", "zhang") —> Name LIKE '%'
isNull Field is empty isNull("name") —> name IS NULL
isNotNull The field is not empty isNotNull("name") —> name IS NOT NULL
in In the collection In 16 (" age ", as) —> The age IN (16) as
notIn In the collection 16 notIn (" age ", as) —> Age is NOT as (16) IN
inSql The subquery in("id", "SELECT id FROM table WHERE id < 3") —> id IN (SELECT id FROM table WHERE id < 3)
notInSql The subquery notInSql("id", "SELECT id FROM table WHERE id < 3") —> id ONT IN (SELECT id FROM table WHERE id < 3)
groupBy grouping groupBy("age", "id") —> GROUP BY id, name
orderByAsc Field in the positive sequence orderByAsc("age") —> ORDER BY age ASC
orderByDesc Field in the reverse order orderByDesc("age") —> ORDER BY age DESC
having Packet filtering having("sum(age) > 10")—>HAVING SUM(age) > 10
or or Eq ("id", 1).or().eq("name", "zhang SAN ")—>Id = 1 OR id = 1
and and Eq ("id", 1).and().eq("name", "zhang SAN ")—>Id = 1 AND id = 1
exists There are exists("SELECT id FROM table WHERE age = 1")—>EXISTS (SELECT id FROM table WHERE age = 1)
notExists There are notExists("SELECT id FROM table WHERE age = 1")—>NOT EXISTS (SELECT id FROM table WHERE age = 1)

isNotnullandgedemo

@Test  
void test1(a) {  
 // Query name is not empty and mailbox is not empty, age is greater than 15
 QueryWrapper<User> wrapper = new QueryWrapper<>();  
    wrapper.  
            isNotNull("name").  
            isNotNull("email").  
            ge("age".15);  
    userMapper.selectList(wrapper).forEach(System.out::println);  
}
Copy the code

eqandselectOnedemo

@Test  
void test2(a) {  
 / / query name = test2
 QueryWrapper<User> wrapper = new QueryWrapper<>();  
    wrapper.eq("name"."test2");  
    //selectOne can query only one selectOne
 User user = userMapper.selectOne(wrapper);  
    System.out.println(user);  
}
Copy the code

betweenandselectCountdemo

@Test  
void test3(a) {  
 // query the user whose age is between [20,30]
 QueryWrapper<User> wrapper = new QueryWrapper<>();  
    wrapper.between("age".20.30);  
    long count = userMapper.selectCount(wrapper);  
    System.out.println(count);  
}
Copy the code

notLikeandlikeRightandselectMapsdemo

@Test  
void test4(a) {  
 //name does not contain BC and email starts with t (t%)
 QueryWrapper<User> wrapper = new QueryWrapper<>();  
    wrapper.  
            notLike("name"."bc").  
            likeRight("email"."t");  
    List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);  
    maps.forEach(System.out::println);  
}
Copy the code

inSqlandselectObjsAnd subquery demo

@Test  
void test5(a) {  
 QueryWrapper<User> wrapper = new QueryWrapper<>();  
    wrapper.inSql("id"."SELECT id FROM user WHERE id < 10");  
    List<Object> objects = userMapper.selectObjs(wrapper);  
    objects.forEach(System.out::println);  
}
Copy the code

orderByDescdemo

@Test  
void test6(a) {  
 QueryWrapper<User> wrapper = new QueryWrapper<>();  
    wrapper.orderByDesc("id");  
    List<User> users = userMapper.selectList(wrapper);  
    users.forEach(System.out::println);  
}
Copy the code