Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement ordinary query by MyBatis-Plus

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces MyBatis-Plus how to achieve ordinary query, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

First, create the User table

The User table is structured as follows:

Idnameageemail1Jone18test1@baomidou.com2Jack20test2@baomidou.com3Tom28test3@baomidou.com4Sandy21test4@baomidou.com5Billie24test5@baomidou.com

The corresponding database Schema script is as follows:

DROP TABLE IF EXISTS user

CREATE TABLE user

(

Id BIGINT (20) NOT NULL COMMENT 'primary key ID'

Name VARCHAR (30) NULL DEFAULT NULL COMMENT 'name'

Age INT (11) NULL DEFAULT NULL COMMENT 'Age'

Email VARCHAR (50) NULL DEFAULT NULL COMMENT 'email'

PRIMARY KEY (id)

);

The corresponding database Data script is as follows:

DELETE FROM user

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')

Back to the top.

Second, create the basic class 2.1 User class

1 package com.example.demo.entity

two

3 import lombok.Data

four

5 @ Data

6 public class User {

7 private Long id

8 private String name

9 private Integer age

10 private String email

11}

2.2 create UserMapper interface and inherit MyBatis Plus BaseMapper interface

1 package com.example.demo.mapper

two

3 import com.baomidou.mybatisplus.core.mapper.BaseMapper

4 import com.example.demo.entity.User

five

6 public interface UserMapper extends BaseMapper {

7}

Back to the top.

3. Query method 3.1 query all records in a single table selectList

1 / *

2 * description: query all records in a single table

3 * author: blog Garden-Wukong chat structure

4 * time: 2019-01-16

5 * Github: https://github.com/Jackson0714/study-mybatis-plus.git

6 * blog Park: https://www.cnblogs.com/jackson0714

7 * /

8 @ Test

9 public void testSelect () {

10 System.out.println ("- query all records in a single table -")

11 List userList = userMapper.selectList (null)

12 Assert.assertEquals (6, userList.size ()); / / whether the total number of records in the table is equal to 6, and if so, the test passes

13 userList.forEach (System.out::println)

14}

UserMapper.selectList (null)

The corresponding SQL statement is:

SELECT id,name,age,email FROM user

If you can't find these logs, you can clean them up and run them again.

The database has six records

The total number of entries was 6, and the test passed.

3.2 single table queries single record selectById according to primary key id

1 / *

2 * description: a single table queries a single record based on the primary key id

3 * author: blog Garden-Wukong chat structure

4 * time: 2019-01-16

5 * Github: https://github.com/Jackson0714/study-mybatis-plus.git

6 * blog Park: https://www.cnblogs.com/jackson0714

7 * /

8 @ Test

9 public void testSelectById () {

10 System.out.println ("- single table queries a single record based on the primary key id -")

11 User user = userMapper.selectById (2)

12 System.out.println (user)

13}

1 userMapper.selectById (2)

2 the corresponding SQL statement is

3 SELECT id,name,age,email FROM user WHERE id=?

There is a record in the database

3.3 query selectBatchIds in batches based on id list in a single table

1 / *

2 * description: single table is queried in batches according to id list

3 * author: blog Garden-Wukong chat structure

4 * time: 2019-01-16

5 * Github: https://github.com/Jackson0714/study-mybatis-plus.git

6 * blog Park: https://www.cnblogs.com/jackson0714

7 * /

8 @ Test

9 public void testSelectByIds () {

10 System.out.println (("- single table batch query based on id list -"))

11 List idsList = Arrays.asList (2L, 4L, 6L)

12 List userList= userMapper.selectBatchIds (idsList)

13 userList.forEach (System.out::println)

14}

one

two

three

UserMapper.selectBatchIds (idsList)

The corresponding SQL statement is

SELECT id,name,age,email FROM user WHERE id IN (?)

Query results, id=2,id=4 query came out, there is no id=6 record

3.4 single table query selectByMap based on criteria

1 / *

2 * description: query based on conditions in a single table

3 * author: blog Garden-Wukong chat structure

4 * time: 2019-01-19

5 * Github: https://github.com/Jackson0714/study-mybatis-plus.git

6 * blog Park: https://www.cnblogs.com/jackson0714

7 * /

8 @ Test

9 public void testSelectByMap () {

10 System.out.println ("- single table query based on criteria -")

11 Map conditions = new HashMap ()

12 conditions.put ("name", "Jack")

13 conditions.put ("age", 20)

14 List userList= userMapper.selectByMap (conditions)

15 userList.forEach (System.out::println)

16}

UserMapper.selectByMap (conditions)

The corresponding SQL statement is

SELECT id,name,age,email FROM user WHERE name =? AND age =?

The database has a record, name= "Jack", age=20

Note: when using slectByMap, the field names in the condition need to be consistent with the database.

Otherwise, the following error will occur:

Thank you for reading this article carefully. I hope the article "how to achieve General query in MyBatis-Plus" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report