当前位置:网站首页>Using dependent packages to directly implement paging and SQL statements
Using dependent packages to directly implement paging and SQL statements
2022-07-28 12:41:00 【Sherlock Holmes est】
<dependency>
<artifactId>mybatis-plus-boot-starter</artifactId>
<groupId>com.baomidou</groupId>
<version>3.3.1</version>
</dependency>There are many methods in this package , This is mainly about LambdaQueryWrapper class .
For example, you need to student Select name It's not empty 、 also age Greater than 18 Of the students :
The entity classes are as follows :
@Data
@TableName("student")
public class Student{
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String address;
}application LambdaQueryWrapper class :
LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper();
queryWrapper.isNotNull(Student::name);// The name is not empty
queryWrapper.gt(Student::age, 18);// Older than 18 year Then combine this filter with paging
<dependency>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<groupId>com.github.pagehelper</groupId>
<version>1.3.0</version>
</dependency>pageHelper Provide PageHelper and PageInfo The method in
int pageNum = 0;
int pageSize = 10;
PageHelper.startPage(pageNum, pageSize);// Open paging ——pageNum: The current number of pages ,pageSize: Page size
PageInfo<Student> pageInfo = new PageInfo<>(studentMapper.selectList(queryWrapper));Combine these two parts :
@Service
@AllArgsConstructor
public class StudentServiceImpl implements StudentService{
private final StudentMapper studentMapper;
@Override
public List<Student> selectByPage() {
int pageNum = 0;
int pageSize = 10;
PageHelper.startPage(pageNum, pageSize);// Open paging
LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper();
queryWrapper.isNotNull(Student::name);// The name is not empty
queryWrapper.gt(Student::age, 18);// Older than 18 year
PageInfo<Student> pageInfo = new PageInfo<>(studentMapper.selectList(queryWrapper));
return pageInfo.getList();
}
}and studentMapper When using , We need to pay attention to , In the corresponding Mapper Interface should inherit BaseMapper< Entity class >.
Here, for example. :
public interface StudentMapper extends BaseMapper<Student> {
}meanwhile , The way to inject here is @AllArgsConstructor And final combination , Rather than using @Resource, Pay attention here , Use @AllArgsConstructor And final when ,@Value It will fail , Get no value , If you want to use @Value, It can be used @RequiredArgsConstructor, But use @RequiredArgsConstructor Can only be used to @NotNull or final The value of takes effect .
LambdaQueryWrapper Common methods :
| Function name | explain | Example | SQL paraphrase |
| eq | be equal to | eq("name"," Program ") | name=' Program ' |
| ne | It's not equal to | ne("name"," Program ") | name<>' Program ' |
| gt | 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 | stay … Between | between("age",18,30) | age between 18 and 30 |
| notBetween | be not in … Between | notBetween("age",18,30) | age not between 18 and 30 |
| like | % value % | like("name"," hat ") | name like '% hat %' |
| notLike | not % value % | notLike("name"," hat ") | name not like '% hat %' |
| likeLeft | % value | likeLeft("name"," hat ") | name like '% hat ' |
| likeRight | value % | likeRight("name"," hat ") | name like ' hat %' |
| isNull | Value is empty | isNull("name") | name is null |
| isNotNull | The value is not empty. | isNotNull("name") | name is not null |
| in | stay … In | in("age","18","20",30") | age in (18, 20, 30) |
| notIn | be not in … In | notIn("age","18","20",30") | age not in (18, 20, 30) |
| inSql | in sql sentence | inSql("age","select age from stu where id=1") | age in (select age from stu where id=1) |
| notInSql | not in sql sentence | notInSql("age","select age from stu where id=1") | age not in (select age from stu where id=1) |
| groupBy | from … grouping | groupBy("name", "age") | group by name, age |
| orderBy | according to … Sort | orderBy(true, false, "name", "age") | order by name desc, age desc notes : take false Change it to true, Then the sorting field is in ascending order |
| orderByAsc | according to … Ascending order | orderByAsc( "name", "age") | order by name asc, age asc |
| orderByDesc | according to … In descending order | orderByDesc( "name", "age") | order by name desc, age desc |
| having | Screening | having("sum(age) > {0} and order = {1}", 10, "01") | having sum(age) > 10 and order = "01" |
| or | perhaps | eq("name", " Program ").or().gt("age", 18) | name = " Program " or age > 18 notes : Don't use or perhaps and According to shimmer and |
| and | also | and(e -> e.eq("name", " Program ").or().gt("age", 18)) | and(name = " Program " or age > 18) |
| apply | Splicing | apply("sum(age) > {0}", 10) | sum(age) > 10 |
| last | Ignore the optimization rules and splice directly to the end | last("where id = 12") | sql The statement is added directly to the end notes : Yes SQL Injection risk |
| exists | Specify a subquery , Detect the existence of lines | exists("select name from stu where age > 49") | exists(select name from stu where age > 49) notes : Return to true or false, Not a specific value |
| notExists | Specify a subquery , Detection line does not exist | notExists("select name from stu where age > 49") | not exists(select name from stu where age > 49) |
| nested | Normal nesting , No and perhaps or | (e -> e.eq("name", " Program ").or().gt("age", 18)) | (name = " Program " or age > 18) |
边栏推荐
- 牛客网二叉树题解
- Developing NES games with C language (cc65) 08. Background collision
- SQL injection LESS18 (header injection + error injection)
- 用C语言开发NES游戏(CC65)10、游戏循环
- 金山云冲刺港股拟双重主要上市:年营收90亿 为雷军力挺项目
- 用C语言开发NES游戏(CC65)09、滚动
- Come to tdengine Developer Conference and have an insight into the future trend of data technology development
- Minimally invasive electrophysiology has passed the registration: a listed enterprise with annual revenue of 190million minimally invasive mass production
- FlexPro软件:生产、研究和开发中的测量数据分析
- 与元素类型 “item” 相关联的 “name” 属性值不能包含'&lt;” 字符解决办法
猜你喜欢

arduino pro mini ATMEGA328P 连线和点亮第一盏LED(同时记录烧录失败的问题stk500_recv)

卸载 Navicat:正版 MySQL 官方客户端,真香!

Foam exploded three times, why did Luo Yonghao put all his eggs in one basket to do ar?

Tik tok "founder" Yang Luyu, farewell byte?
![[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou

Library automatic reservation script

AVL树(平衡搜索树)

Laravel form data validation

洪九果品通过聆讯:5个月经营利润9亿 阿里与中国农垦是股东

开源汇智创未来 | 2022 开放原子全球开源峰会 OpenAtom openEuler 分论坛圆满召开
随机推荐
03 pyechars 直角坐标系图表(示例代码+效果图)
用C语言开发NES游戏(CC65)06、精灵
1331. Array sequence number conversion: simple simulation question
stm32 回环结构接收串口数据并处理
How can a novice quickly complete the establishment of a website? Come to the free "fitting room" experience
Foam exploded three times, why did Luo Yonghao put all his eggs in one basket to do ar?
Implementation method of mouse hover, click and double click in ue4/5
Distributed session solution
SQL injection less24 (secondary injection)
SuperMap game engine license module division
软件架构师必需要了解的 saas 架构设计?
Laravel form data validation
Is it difficult for cloud native machine learning to land? Lingqueyun helps enterprises quickly apply mlops
Hongjiu fruit passed the hearing: five month operating profit of 900million Ali and China agricultural reclamation are shareholders
[nuxt 3] (XII) project directory structure 3
DIY system home page, your personalized needs PRO system to meet!
微创电生理通过注册:年营收1.9亿 微创批量生产上市企业
AVL树(平衡搜索树)
Kuzaobao: summary of Web3 encryption industry news on July 13
OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程