Please refer to Mybatis official website for specific usage

Conditional constructor for MybatisPlus

Before the order

MybatisPlus general CRUD interface MybatisPlus conditional constructor MybatisPlus code generator tutorial details The configuration of MybatisPlus

preface

In MP, the Wrapper interface implementation class relationship is as follows:

As you can see, AbstractWrapper and AbstractChainWrapper are the key implementations. Next we will focus on AbstractWrapper and its subclasses.

Description: The parent classes of QueryWrapper(LambdaQueryWrapper) and UpdateWrapper(LambdaUpdateWrapper) are used to generate WHERE conditions for SQL, Entity attributes are also used to generate SQL WHERE conditions Note: The WHERE conditions generated by entity do not have any behavior associated with where conditions generated using the various apis

Mybatis. Plus/Guide/WRApp…

1 、allEq

1.1, description,

allEq(Map<R, V> params)
allEq(Map<R, V> params, boolean null2IsNull)
allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
Copy the code
  • Total EQ (or individual isNull)

Description of individual parameters:

  • Params: Key indicates the database field name and value indicates the field value

Null2IsNull: If the value of map is true, isNull is invoked. If the value of map is false, null is ignored. Example 1: AllEq ({id:1,name:” allEq “,age:null}) –> id = 1 and name = “allEq” and age is null AllEq ({id:1,name:” allEq “,age:null}, false) –> id = 1 and name = “allEq”

1.2. Test cases

@Test
public void testAllEq(a){

  QueryWrapper<User> queryWrapper = new QueryWrapper<>();

  / / build the map
  Map<String, Object> map = new HashMap<>();
  map.put("name"."jack");
  map.put("age".null);

  // WHERE name = ? AND age IS NULL
  // queryWrapper.allEq(map);

  // WHERE name = ?
  // queryWrapper.allEq(map,false);

  // SELECT id,name,age,email AS mail,user_name FROM tb_user
  // queryWrapper.allEq(false,map,true);

  // WHERE age IS NULL
  queryWrapper.allEq((k,v) -> k.equals("name"),map);

  List<User> users = userMapper.selectList(queryWrapper);
  for(User user : users) { System.out.println(user); }}Copy the code

2. Basic comparison operation

  • Eq is =
  • Ne does not equal <>
  • Gt is greater than the >
  • Ge is greater than or equal to >=
  • Lt < <
  • Le is less than or equal to <=
  • Between between Between The value is 1 AND 2
  • NotBetween NOT BETWEEN The value is 1 AND 2
  • In field in (value.get(0), value.get(1),…
  • Not In field not In (v0, v1…)

Test cases:

/* Basic comparison operation */
@Test
public void testWrapper(a){

  QueryWrapper<User> queryWrapper = new QueryWrapper<>();

  // WHERE email = ? AND age >= ? AND name IN (? ,?)
  queryWrapper.eq("email"."[email protected]")
    .ge("age".20)
    .in("name"."Son desire"."Should any jerk");

  List<User> users = userMapper.selectList(queryWrapper);
  for(User user : users) { System.out.println(user); }}Copy the code

3, fuzzy query

  • like
    • LIKE ‘% % values’
    • Example: like(“name”, “king “) –> name like ‘% king %’
  • notLike
    • NOT LIKE ‘% value %’
    • NotLike (“name”, “king “) –> name not like ‘% king %’
  • likeLeft
    • LIKE ‘% values’
    • Example: likeLeft(“name”, “king “) –> name like ‘% king ‘
  • likeRight
    • LIKE ‘% values’
    • LikeRight (“name”, “king “) –> name like ‘king %’

Test cases:

/* Fuzzy query */
@Test
public void testWrapperLike(a){

  QueryWrapper<User> queryWrapper = new QueryWrapper<>();

  queryWrapper.like("name"."Child");

  List<User> users = userMapper.selectList(queryWrapper);
  for(User user : users) { System.out.println(user); }}Copy the code

4, sorting,

  • orderBy
    • Sort: ORDER BY…
    • OrderBy (true, true, “id”, “name”) –> Order by id ASC,name ASC
  • orderByAsc
    • Sort: ORDER BY… ASC
    • OrderByAsc (“id”, “name”) –> Order by id ASC,name ASC
  • orderByDesc
    • Sort: ORDER BY… DESC
    • OrderByDesc (” ID “, “name”) –> Order by ID DESC,name DESC

Test cases:

/** ** sort operation */
public void orderByTest(a){
  QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
  // In descending order of age
  userQueryWrapper.orderByDesc("age");

  List<User> users = userMapper.selectList(userQueryWrapper);
  for(User user : users) { System.out.println(user); }}Copy the code

5, logical query

  • or

    • Joining together the OR
    • Calling or actively means that the next method is not joined with and! (Default to use and if you do not call OR)
  • and

    • AND nested
    • Example: and (I – > appropriate precautions q (” name “, “li bai”). Ne (” status “, “live”) — – > the and (name = ‘lee)

Test cases:

/** select */
@Test
public void testWrapper2(a){

  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  queryWrapper.eq("name"."jack").or().eq("age".28).select("name");
  List<User> users = userMapper.selectList(queryWrapper);
  for(User user : users) { System.out.println(user); }}Copy the code