In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of Mybatis Plus case analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Mybatis Plus case analysis article. Let's take a look at it.
1 first acquaintance with Mybatis-Plus
MyBatis-Plus, referred to as MP, 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 has a lot of features to support a lot of standard databases, wait until you learn the relevant features to learn more. Official document of MyBatis-Plus: https://baomidou.com/
The official document about the framework structure of MyBatis-Plus, that is, the underlying implementation of the framework, gives the following attempt: scan the entity class, extract the attributes in the entity class through reflection and analyze the relationship between them and the fields in the table, and finally call a bunch of methods provided by MyBatis-Plus to generate SQL statements and inject them into the MyBatis container, so as to achieve different functions of adding, deleting, modifying and querying.
2 pre-environmental preparation for entry case
Step 1: create a database table and insert data
CREATE TABLE `user` (`id` bigint (20) NOT NULL COMMENT 'primary key ID', `name` varchar (30) DEFAULT NULL COMMENT' name', `age` int (11) DEFAULT NULL COMMENT 'age', `email` varchar (50) DEFAULT NULL COMMENT 'mailbox', PRIMARY KEY (`id`) ENGINE=InnoDB DEFAULT CHARSET=utf8 INSERT INTO user (id, name, age, email) VALUES (1, 'Jone', 18,' test1@baomidou.com'), (2, 'Jack', 20,' test2@baomidou.com'), (3, 'Tom', 28,' test3@baomidou.com'), (4, 'Sandy', 21,' test4@baomidou.com'), (5, 'Billie', 24,' test5@baomidou.com')
Step 2: quickly create a SpringBoot project using Spring Initializr
Delete selected unnecessary files and folders
Step 3: import related dependencies into pom files
Com.baomidou mybatis-plus-boot-starter 3.5.1 org.projectlombok lombok true mysql mysql-connector-java 5.1.32 runtime com.alibaba druid-spring-boot-starter 1.1.17
Step 4: change the configuration file to the suffix .yml and configure the data source
Spring:
# various configurations of data sources
Datasource:
Driver-class-name: com.mysql.jdbc.Driver
Url: jdbc:mysql://localhost:3306/mybatis_plus
Username: root
Password: 123456
# Mybatis-Plus enables log printing
Mybatis-plus:
Configuration:
Log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Code writing for each layer
Domain layer entity class
/ * * @ author: mereign * @ date: 2022-3-13-14:28 * @ desc: user entity class * / / generate the all-parameter constructor of the user class during compilation @ AllArgsConstructor// generates the no-parameter constructor of the user class @ NoArgsConstructor// generates the setter and getter methods that encapsulate properties during compilation, and overrides the toString and equals, hashCode method @ Datapublic class User {private Long id; private String name Private Integer age; private String email;}
Mapper layer inherits interface
/ * * @ author: mereign * @ date: 2022-3-13-15:43 * @ desc: create mapper API inherits BaseMapper API and pass user generics * / / spring framework creates the interface implementation class @ Mapperpublic interface UserMapper extends BaseMapper {} corresponding to mapper interface
Test class
/ * @ author: mereign * @ date: 2022-3-13-15:50 * @ desc: test class simply test mp query method * / @ SpringBootTestpublic class MybatisPlusTest {@ Autowired UserMapper mapper; @ Testpublic void selectListTest () {/ / query a list collection through the condition constructor, and set the parameter to null List users = mapper.selectList (null) if there is no condition Users.forEach (System.out::println);}} getting started case query results
Small knowledge points in entry cases
The code returns red when the mapper component is injected into the ⚠ test class, but it does not affect the final program execution result. The reason is that the @ Mapper interface dynamically injects the proxy class generated by this interface into the IOC container, so when using automatic mapper injection, the interface component will not be found in the container. Solution: annotate the mapper interface with @ Repository to mark the class or interface as a persistence layer component. In this case, an annotation is used to work, and an annotation is used to find the interface component to prevent red ⚠ when injecting.
In the generated SQL statement, the table name is the lowercase of the generic initials passed by the mapper interface, and the field name in the table is the encapsulation attribute of the generic class. If you need to modify the table name bound to the entity class, you need to use the @ TableName annotation on the entity class.
This is the end of the article on "Mybatis Plus case study". Thank you for reading! I believe you all have a certain understanding of the knowledge of "Mybatis Plus case study". If you want to learn more, you are welcome to follow the industry information channel.
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.