Mybatis learning framework learning (three)

Mybatis learning (three)

Chapter 3 Addition, deletion, modification and investigation of Mybatis mapper method



Preface

The original addition, deletion, modification, and check operation of Mybatis has been introduced in Chapters 1 and 2. This article focuses on the addition, deletion, modification, and check of the Mybatis mapper interface method.
When using the mapper interface, it is recommended to install the Free Mybatis plugin.


One, add through the mapper interface

First create an interface class UserMapper, a sub-configuration file UserMapper.xml and the main function UserMapperTest, and associate the sub-configuration file with the main configuration file, as shown below:

(Two ways of writing are given here, personally recommend using the second one)

<!--    <mapper resource="com/colin/mapper/UserMapper.xml"/>-->
<!--    <mapper class="com.colin.mapper.UserMapper"/>-->

Define an insert method in the interface class:

void insertUser(String name, String password);

If you have installed the Free Mybatis plug-in, you will find that the method name is red, press alt+Enter, it will automatically jump to the sub-configuration file, and automatically generate the insert statement template. (The method name is different, the final effect is also different) Then fill in the sql statement:

<insert id="insertUser">
        insert into user values (null,#{
    
    param1},#{
    
    param2})
</insert>

When writing the sql statement this time, #{} will give automatic prompts such as param1, enter according to the prompts, and you cannot change them at will.

The implementation process of the mapper is as follows:

// How to play the mapper interface method

  • 1. Get an instance of sqlSession
  • 2. Obtain interface instance through sqlSession instance
  • 3. Call the interface method
  • 4. Manual submission
  • 5. Close resources

After that, we return to the main function to implement method calls:

SqlSession sqlSession = null;
        try {
    
    
            sqlSession = new DBUtil().getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            userMapper.insertUser("louzhaoyu", "0624");
            sqlSession.commit();

        } finally {
    
    

            if (sqlSession != null) {
    
    
                sqlSession.close();
            }
        }

The console shows that the execution is successful, refresh the database, you can also see the corresponding data.

2. Query, modify and delete display

Interface class:

    void deleteById(int id);

    User selectById(int id);

    void updateUser(int id, String name, String password);

    void updateUser1(User user);

    List<User> selectAll();

Sub configuration file:

  <update id="updateUser">
    update user set name = #{
    
    param2}, password = #{
    
    param3} where id = #{
    
    param1}
  </update>
  <delete id="deleteById">
    delete from user where id = #{
    
    fangshadouxing}
  </delete>
  <select id="selectAll" resultType="com.colin.bean.User">
    select id, name, password from user
  </select>
  <select id="selectById" resultType="com.colin.bean.User">
    select id, name, password from user where id = #{
    
    fanghdlkfjdslf}
  </select>

to sum up

The above is the content to be talked about today. This article explains in detail one type of addition, deletion, modification, and checking. The others are only briefly mentioned. Typing more codes helps to understand the mapper interface to realize addition, deletion, modification, and checking.

Guess you like

Origin blog.csdn.net/weixin_44955134/article/details/111721323