This is the 26th day of my participation in the August More Text Challenge
1. Lambda basics
- lamada
Lamada is a new java8 feature that optimizes code structure and is very readable. Its main characteristics are:
Optional type declaration: There is no need to declare parameter types, and the compiler can uniformly identify parameter values
Optional parameter parentheses: You do not need to define parentheses for one parameter, but you need to define parentheses for multiple parameters
Optional braces: If the body contains a statement, braces are not required
Optional return keyword: The compiler automatically returns the value if the body has only one expression return value. Curly braces are required to specify that the expression returns a value.
In short, if there is only one argument, the left parenthesis can be omitted, and a statement, the right parenthesis can be omitted.
The syntax is as follows:
(parameters) -> expression 或 (parameters) ->{ statements; }
Parameters: The arguments in the left parentheses are the same as those passed in by normal methods
Expression: The expression mainly contains the related syntax
- example
The following examples are just a few simple expressions that use lamda. If you want to learn more about lamda, you can go to the Internet and learn more.
// Return (String s) with no arguments -> system.out.print (s) // Accept a String object and print it on the console without returning any value (looks like returning void) () -> 5 // Return 5 x -> 2 * x // pass an argument Return 2 times of x (x, y) -> x -- y // Return 2 arguments return the difference between two arguments (int x, int y) -> x + yCopy the code
2. Lamda expression in mybatis-plus
Select * from t_base_info where age < 18;
- The normal way to write the final query is to write the conditions that need to be pieced together corresponding to the final query content (such a query statement cannot write the database field wrong if the field is written wrong query will be wrong) lamda expression can avoid the database field to query
QueryWrapper<BaseInfo> queryWrapper = new QueryWrapper();
queryWrapper.lt("age",18);
List<BaseInfo> baseInfos1 = baseMapper.selectList(queryWrapper);
Copy the code
- LambdaQueryWrapper uses this expression and all queries need only one expression to complete (but there are fewer queries if there are more than one through the connection server and or OR); Inside UpdateWrapper is the LambdaUpdateWrapper
LambdaQueryWrapper<BaseInfo> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.lt(BaseInfo::getAge,18);
List<BaseInfo> baseInfos = baseMapper.selectList(lambdaQueryWrapper);
Copy the code
- QueryWrapper.lambda
LambdaQueryWrapper<BaseInfo> lambda = new QueryWrapper<BaseInfo>().lambda();
lambdaQueryWrapper.lt(BaseInfo::getAge,18);
List<BaseInfo> baseInfoList = baseMapper.selectList(lambdaQueryWrapper);
Copy the code
- LambdaQueryChainWrapper (chain call lambda-type. Note: Kotlin is not supported)
List<BaseInfo> list = new LambdaQueryChainWrapper<>(baseMapper)
.lt(BaseInfo::getAge, 18)
.list();
Copy the code
3. Summary
Lamda, a new feature of java8, also supports lamda expressions in mybatis-plus. Today, THE main thing I want to share is the lamda writing method in Mybatis – Plus. For some simple query statements, I can try to use the lamda writing method of Mybatis – Plus. The lamDA expression code is simple, but also can avoid the use of database fields. If the SQL statement is logically complex, it depends