In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to achieve the addition, deletion, modification and search of MybatisPlus, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Preface
In the persistence layer framework, there are many mybatis applications, and the proportion is gradually increasing. Usually the combination of projects is SSM. Mybatis is popular because it is flexible, easy to use and easy to optimize.
Mybatis directly executes the sql statement, and the sql statement is written in the xml file. Using mybatis requires multiple xml configuration files, which is more tedious to some extent. General database operations are related to CURD.
Mybatis-plus is an enhancement on mybatis, reducing the configuration of xml, almost without writing xml to achieve a single table CURD, which is very convenient and greatly provides the efficiency of development. We write programs to make life easier.
What is mybatis-plus?
MyBatis-Plus (MP for short) is an enhancement tool of MyBatis, which is only enhanced but not changed on the basis of MyBatis, in order to simplify development and improve efficiency.
MyBatis-Plus puts a coat on MyBatis, and almost all operations of single table CURD can be performed by MyBatis-Plus. And provides a variety of query methods, paging behavior. As a consumer, you don't need to write xml, just call the API provided by MyBatis-Plus.
Quick start
You can get started with MP in a few minutes, as long as you need to be familiar with mybatis,spring or spring boot, maven, and mastering lambda expressions can improve efficiency.
Prepare the environment:
Have Java development environment and corresponding IDE
Be familiar with Spring Boot
Be familiar with Maven
Prepare the data sheet
Create database user;use user;CREATE TABLE `user` (`id` int (11) NOT NULL AUTO_INCREMENT, `name` varchar (50) DEFAULT NULL, `email` varchar (80) DEFAULT NULL, `age` int (11) DEFAULT NULL, PRIMARY KEY (`id`) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;insert into user values (null,' Zhang San', '6666roomqq.compositure 18); configure maven in IDEA
Create a spring boot application
Quickly initialize a Spring Boot project using Spring Initializr
File-New Project in idea, fill in the project information after selecting Spring Initializr, and create the project.
Project information
Dependent selection of MyBatis Framework in SQL
Define the project name and path
Pom files add mybatis-plus dependencies
Com.baomidou mybatis-plus-boot-starter 3.0.5 mysql mysql-connector-java 8.0.25
Application.properties or application.yml add database configuration
Spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/userspring.datasource.username=rootspring.datasource.password=root
Or
Spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/user username: root password: root
Create an entity class
@ TableId sets the primary key. IdType.AUTO uses automatic growth to generate the primary key.
Create a mapper interface
Inherit BaseMapper in MyBatis Plus, use methods in MP in UserMapper, real
Now CURD.
Add @ MapperScan scan Mapper folder
test
View the database
Configure mybatis log mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Basic usage of CRUD
The operation of CRUD comes from the methods in BaseMapper. There are 17 methods in BaseMapper, and there are many methods with different parameters for CRUD operations. Inherits the methods that BaseMapper can have.
List of BaseMapper methods:
Insert operation
Note: insert () returns the value int, the number of rows in which the data was successfully inserted, and the number of successful records. GetId () gets the primary key value and automatically assigns the primary key value to the primary key id of the entity class after successfully affecting the number of rows.
Update operation
Update the previous database
Updated database
Note: the fields of null are not updated
The field of email is not set, so the field of email at this time is null
The SQL statement does not set email
Database
Delete operation
There are several ways to delete
DeleteById: press primary key to delete
Delete according to the condition in Map
Note: the deletion condition is encapsulated in Map, key is the column name, value is the value, and multiple key are connected by and.
Log:
Batch deletion
Put the id to be deleted into List and pass it to deleteBatchIds ()
Journal
Select operation
Note: if there is no query result, no error will be reported.
Journal
Batch query records
Note: according to the id query record, store multiple id to be queried into List and call selectBatchIds ()
Journal
Conditional queries using Map
Put the conditional field to be queried to Map,key is the field, and value is the conditional value. Multiple conditions are and joins. Call selectByMap (), pass in Map as a parameter, and the return value is the List collection.
Journal
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.