In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what is MyBatisPlu". In daily operation, I believe many people have doubts about what is MyBatisPlu. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is MyBatisPlu?" Next, please follow the editor to study!
What is MP
MP's full name is Mybatis-Plus, and to paraphrase the official explanation, it means to be MyBatis's best partner, or gay friend for short. It is only enhanced but not changed on the basis of MyBatis, in order to simplify development and improve efficiency.
1. Three characteristics
1) the moisturizer is silent
Only enhance but not change, the introduction of it will not affect the existing project, such as silky smooth.
2) efficiency is supreme
With simple configuration, you can quickly perform single-table CRUD operations, thus saving a lot of time.
3) rich features
Code generation, physical paging, performance analysis and other functions are available.
two。 Support database
Mysql 、 mariadb 、 oracle 、 db2 、 h3 、 hsql 、 sqlite 、 postgresql 、 sqlserver 、 presto 、 Gauss 、 Firebird
Phoenix, clickhouse, Sybase ASE, OceanBase, Dameng Database, Xugu Database, National people's Congress Jinwang Database, NTU General Database
3. Frame structure
To be honest, as long as you can see the above content on the official website, then let's actually do it first!
II. MP actual combat
1. Hand-touch project exercise
1) Database and table preparation
Sql statement:
Use test; CREATE TABLE `student` (`id` int (0) NOT NULL AUTO_INCREMENT, `dept_ id` int (0) NULL DEFAULT NULL, `name` varchar (16) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `remark` varchar (32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Dynamic -- Records of student-INSERT INTO `student` VALUES (1, 1, 'small dish', 'pay attention to the small dish and don't get lost!') ; INSERT INTO `student` VALUES (2,2, 'Xiao Ming', 'study hard and make progress every day!')
2) pom dependence
Org.springframework.boot spring-boot-starter-test org.projectlombok lombok 1.16.16 com.baomidou mybatis-plus-boot-starter 3.2.0 mysql mysql-connector-java 8.0.21 com.alibaba druid 1.2.1 junit junit 4.13.1
3) configuration file
Spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver
4) entity class
@ Data @ Builder @ TableName ("student") public class User {@ TableId (type = IdType.AUTO) private Integer id; private Integer deptId; private String name; private String remark;}
5) Mapper
Public interface UserMapper extends BaseMapper {}
6) Test class
@ RunWith (SpringRunner.class) @ SpringBootTest public class MapperTest {@ Autowired private UserMapper userMapper; @ Test public void getAll () {List users = userMapper.selectList (null); users.forEach (System.out::println);}} / * * OUTPUT: User (id=1, deptId=1, name= snacks, remark= follow snacks and don't get lost!) User (id=2, deptId=1, name= Xiaoming, remark= study hard and make progress every day!) * * /
Small vegetable knot:
In the above results, we can see that all the data in the database has been printed out (two pieces). Instead of seeing the mapper.xml file we need to write, we just use the selectList () method in usermapper, and UserMapper inherits the BaseMapper interface, which is provided to us by MybatisPlus. Let's take a look at what methods this interface provides to us.
2. CRUD basic fuck
1) insert
@ Test public void insert () {/ / the builder mode construction object in lombok User user = User.builder (). DeptId (1). Name ("Xiaohua"). Remark ("Xiaohua") .build (); int insertFlag = userMapper.insert (user); log.info ("insert influence rows, {} | Xiaohua's ID: {}", insertFlag, user.getId ()) } / * * OUTPUT: number of rows affected by insertion. 1 | Xiaohua's ID: 8 * /
You can see that we not only insert the data, but also get the ID of the inserted data, but it is worth noting that although the ID here is self-increasing, it is not the default ID generation strategy of MP, but what we specify in the entity class:
The following primary key generation strategies are supported in MP:
Now that we have seen the @ TableId annotation, let's look at a common annotation @ TableField
We can see from the annotation name that @ TableId is used to mark the primary key ID, while @ TableField is used to mark other fields.
You can see that there are still many values in this annotation. Here are some commonly used values:
Value
It is used to solve the problem of inconsistent field names and hump naming. For example, if the attribute name in the entity class is remark, but the field in the table is describe, you can use @ TableField (value= "describe") to convert. Hump conversion if there is a configuration hump name in the global, this place can not be written.
Exist
For fields that do not exist in the data table, we can use @ TableField (exist = false) to mark them.
Condition
Custom operation rules are used to preprocess WHERE entity conditions. For example, I configured @ TableField (condition = SqlCondition.LIKE), and the output SQL is: select table where name LIKE CONCAT ('%', value,'%'), where the SqlCondition value is as follows:
Update
It is used to preprocess set field custom injection. For example, I configured @ TableField (update = "% s 1"), where% s populates the field, and the output SQL is: update table name set field = field + 1 where condition
Select
If we have a field remark of type text and do not want to query this field when querying, we can use @ TableField (select = false) to constrain the query without querying this field.
2) update
There are two update operations for MybatisPlus:
Int updateById (Param ("et") T entity); int update (@ Param ("et") T entity, @ Param ("ew") Wrapper updateWrapper)
Update according to ID
@ Test public void update () {User user = User.builder (). Id (3). Name ("Xiaohua"). Remark ("Xiaohua loves playing games"). Build (); userMapper.updateById (user);} / * * Update results: User (id=3, deptId=1, name= Xiaohua, remark= Xiaohua loves playing games) * /
Update according to condition
@ Test public void update () {UpdateWrapper updateWrapper = new UpdateWrapper (); updateWrapper.eq ("name", "Xiaohua"). Set ("remark", "Xiaohua loves to play chess"); userMapper.update (null, updateWrapper);} / * * Update results: User (id=3, deptId=1, name= Xiaohua, remark= Xiaohua) * / /
We can also put the updated conditions into the user object:
@ Test public void update () {UpdateWrapper updateWrapper = new UpdateWrapper (); updateWrapper.eq ("name", "Xiaohua"); User user = User.builder () .remark ("Xiao Hua loves swimming"). Build (); userMapper.update (user, updateWrapper);} / * * Update results: User (id=3, deptId=1, name= Xiaohua, remark= Xiaohua loves swimming) * * /
3) delete
There are more ways to delete than update in MybatisPlus, and there are four ways:
Int deleteById (Serializable id); int deleteByMap (@ Param ("cm") Map columnMap); int delete (@ Param ("ew") Wrapper wrapper); int deleteBatchIds (@ Param ("coll") Collection
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.