Simplify development and use of general Mapper [1]

1. Are you still writing sql?
Let the general Mapper help you out of the sea of ​​suffering. Try it yourself, it is really easy to use!
2. First introduce maven dependencies, and I have posted the corresponding versions of mybatis dependencies and pagehelper

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.2.1</version>
        </dependency>
        
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.1.4</version>
        </dependency>

3. The introduction is successful, how about a try, first create a table called User, the table structure is as follows:

  `user_id` varchar(50) NOT NULL,
  `username` varchar(50) NOT NULL,
  `password` varchar(32) NOT NULL,
  `phone` varchar(18) NOT NULL,
  `email` varchar(30) DEFAULT NULL,
  `head` varchar(255) DEFAULT NULL,
  `is_delete` int(11) NOT NULL,
  `create_user` varchar(50) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `update_user` varchar(50) DEFAULT NULL,
  `update_time` datetime DEFAULT NULL

4. You can create controllers and entities by yourself, so I won’t post them here. Mapper is the key point. Write it down. First, create a Mapper file that inherits the general Mybatis Mapper, and pass your entity generics in. After you have finished writing this step, most of the simple functions of your table have been written

@Repository
public interface UserMapper extends Mapper<User> {
    
    
}

It must be noted that the Id in your entity must be annotated with @Id, otherwise it will not know that your field is ID when adding or querying @GeneratedValue
insert image description here
(generator = "UUID")

这个注解的作用是你新增的时候,自动帮你生成一个UUID,但是当批量新增的时候这个注解是不生效的

5. Introduce your Mapper at this time, and you can see that many methods have been written in it.
insert image description here
6. At this time, the basic addition, deletion, modification and query are almost ready to use. Let’s start with the query. There are many methods for query, which can be based on To use it in actual situations, let me give a few examples:
For example, you want to query a name: Zhang San, and the mobile phone number is: 13500000000.
insert image description here
You can see that there are so many methods, let me briefly explain:
selectAll(): query all is equivalent to select * from user
selectByPrimaryKey(): Query by Id
selectOne(): The query result has only one method, if there are multiple query results, an exception will be thrown
select(): The query result is a List method
selectCount(): Find the number of items according to the condition, Equivalent to select count(*) from user where ***

I will talk about the rest next time, because it involves subqueries, fuzzy queries, sorting, combined queries, etc.
insert image description here
Start testing:

        User user = new User();
        user.setUsername("张三");
        user.setPhone("13500000000");
        List<User> userList = userMapper.select(user);

Run the code, we print the sql:

SELECT user_id,username,password,phone,email,head,is_delete,create_user,create_time,update_user,update_time FROM user WHERE username = '张三' AND phone = '13500000000';

You can see that mabytis has changed the values ​​we passed in into where conditions. Let's take a look at the source code
insert image description here
insert image description here

It can be seen that it gets the attribute of the entity we passed in through reflection, and sets the attribute that is not empty as the condition of where
. Let’s not talk about (), let’s talk about the insertSelective() method.
insert image description here
The difference between this method and insert() is that insertSelective() will only insert fields that are not null, while insert will insert all of them.
8. Delete is divided into two methods:
delete(user): delete according to the incoming conditions
deleteByPrimaryKey(id): delete according to Id
9. There are also two methods for modification:
updateByPrimaryKey(): modify all values ​​according to Id
updateByPrimaryKeySelective(): only modify Conditions that are not equal to null, for example, if you only want to modify the name, you can write:

        User user = new User();
        user.setUserId("111");
        user.setUsername("李四");
        userMapper.updateByPrimaryKeySelective(user);

Equivalent to sql:

UPDATE user SET username = '李四' WHERE user_id = '111';

The basic functions are about these, the next issue will talk about how to write combined sql

Guess you like

Origin blog.csdn.net/qq_39486119/article/details/112829101