Article Directory
- Overview of MyBatisPlus and how to use MyBatisPlus and the QueryWrapper conditional query and paging query in MyBatisPlus
-
- Overview of MyBatisPlus
- MyBatis-Plus features
- Steps to use MyBatisPlus
- Condition builder QueryWrapper is used to set query conditions
- Paging query
Overview of MyBatisPlus and how to use MyBatisPlus and the QueryWrapper conditional query and paging query in MyBatisPlus
Overview of MyBatisPlus
Why study it? MyBatisPlus can save us a lot of working time, and all CRUD codes can be automated!
MyBatis-Plus (MP for short) is an enhancement tool for MyBatis. On the basis of MyBatis, it only enhances and does not change. It is born to simplify development and improve efficiency.
The vision that MyBatis-Plus expects is:
Our vision is to become the best partner of MyBatis, just like the 1P and 2P in Contra, the combination of base and friends, double the efficiency.
as the picture shows:
MyBatis-Plus is an enhancement tool for MyBatis. On the basis of MyBatis, it only enhances without making changes. It is born to simplify development and improve efficiency. Using it can be like using Hibernate, for single-table operations only call methods without writing SQL to complete the corresponding database operations, rapid development saves a lot of time. For multi-table operations, or when you want to write SQL, you can also use the original MyBatis method of writing SQL.
MyBatis-Plus features
characteristic
• No intrusion: only enhance and do not make changes, the introduction of it will not affect the existing project, as smooth as silk
• Low loss: basic CURD will be automatically injected at startup, performance is basically no loss, direct object-oriented operation
• Powerful CRUD operations: built-in general Mapper, general Service, only a small amount of configuration can realize most of the CRUD operations of a single table, and a more powerful condition builder to meet various usage requirements
• Support Lambda form invocation: through Lambda expressions, it is convenient to write various query conditions, no need to worry about writing wrong fields
• Support multiple databases: support MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Postgre, SQLServer2005, SQLServer and other databases
• Supports automatic generation of primary keys: supports up to 4 primary key strategies (including a distributed unique ID generator-Sequence), which can be configured freely, which perfectly solves the primary key problem
• Support XML hot reload: Mapper corresponding XML supports hot reload, for simple CRUD operations, it can even start without XML
• Support ActiveRecord mode: Support ActiveRecord form call, entity classes only need to inherit Model class to perform powerful CRUD operations
• Support custom global general operations: support global general method injection (Write once, use anywhere)
• Support keyword automatic escaping: support database keywords (order, key...) automatic escaping, and keywords can be customized
• Built-in code generator: Use code or Maven plug-in to quickly generate Mapper, Model, Service, Controller layer code, support template engine, and more custom configurations for you to use
• Built-in paging plug-in: Based on MyBatis physical paging, developers do not need to care about specific operations. After configuring the plug-in, writing paging is equivalent to ordinary List query
• Built-in performance analysis plug-in: Sql statement and its execution time can be output. It is recommended to enable this function during development and testing to quickly detect slow queries
• Built-in global interception plug-in: Provides intelligent analysis and blocking of delete and update operations of the entire table, and can also customize interception rules to prevent misoperations
• Built-in Sql injection stripper: supports Sql injection stripping, effectively preventing Sql injection attacks
Steps to use MyBatisPlus
1. Create the database mybatis_plus
As shown below:
2. Create the user table
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
--真实开发中,version(乐观锁),delete(逻辑删除),gmt_create,gmt_modified
INSERT INTO USER (id, NAME, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
The above is the creation code, the following figure is the creation effect as shown below:
3. Write the project, initialize the project! Use SpringBoot to initialize!
4. Import dependencies
Note: We use mybatis-plus to save us a lot of code, try not to import mybatis and mybatis-plus at the same time, so there will be a version difference problem
As shown below:
Need to import the database mysql driver and mybatis-plus module, in order to simplify the operation also introduces the lombok dependency, as shown below
Configure the data source DataSource in the configuration file
After importing dependencies, because mysql dependencies are imported, a data source needs to be configured in the configuration file, as shown below:
5. Write dynamic proxy interface
6. Methods in the BaseMapper interface
7. Scan the dynamic proxy interface
Finally, be sure to scan all dynamic proxy interfaces on the start class, as shown below:
Condition builder QueryWrapper is used to set query conditions
Very important: Wrapper
We can use it instead of writing some complex SQL!
selectList condition query
First look at a conditional query using selectList, as shown below:
selectOne condition query
The case of query using the selectOne method is as follows:
The between method in QueryWrapper
Query users between 20 and 30 years old, as shown below:
Fuzzy query like, notLike, likeRight methods in QueryWrapper
Use the condition object QueryWrapper to perform fuzzy query as shown below:
The inSql method in QueryWrapper
The inSql method uses the results of the query clause to query, as shown below:
OrderByDesc method in QueryWrapper
Use OrderByDesc to sort the query results in descending order, as shown below:
Paging query
Pagination is used a lot on the website!
1. The original limit is paging;
2.pageHelper third-party plugin
3. MyBatisPlus also has a built-in paging plug-in that can be used directly
how to use:
First, you need to configure a paging plugin in the configuration class of MyBatisPlus, as shown below:
The second is to test as shown below: