Mybatis learning notes: simple addition, deletion, modification and query of mybatis

Keywords: Mybatis

1, Foreword

This article is mainly based on the learning notes recorded in the official documents and the crazy God Mybatis tutorial.

2, CRUD

In the previous article Mybatis learning notes (II): write the first mybatis program based on Maven project The select statement written is as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hjxlog.mapper.UserMapper">
    <select id="selectUserList" resultType="com.hjxlog.domain.User">
        select * from public.user
    </select>
</mapper>

2.1 namespace attribute

The namespace attribute in the < mapper > tag is the location of the corresponding mapper interface. The package name should be written in full.

2.2 select

Add a mapper interface method:

    User selectById(Integer id);

Corresponding xml implementation:

    <select id="selectById" resultType="com.hjxlog.domain.User" parameterType="int">
        select * from public.user where id = #{id,jdbcType=INTEGER}
    </select>
  • id: the method name in the corresponding mapper interface
  • resultType: return value type. If list < T >, the resultType is the returned generic type.
  • parameterType: parameter type

2.3 insert

Add a mapper interface method:

    boolean insert(User user);

Corresponding xml implementation:

    <insert id="insert" parameterType="com.hjxlog.domain.User">
        insert into public.user
        (id,name,password)
        values
        (
        #{id,jdbcType=INTEGER},
        #{name,jdbcType=VARCHAR},
        #{password,jdbcType=VARCHAR}
        )
    </insert>

Test:

    @Test
    public void insert(){
        // Get sqlSession object
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        try {
            // Execute sql
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            User user = new User(4,"huang","123456");
            boolean rlt = userMapper.insert(user);
            System.out.println(rlt);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // Close sqlSession
            sqlSession.close();
        }
    }

After executing the test code, it is found that it does not work and the database is not added. This is because the addition, deletion and modification of Mybatis need to commit the transaction! You need to add a statement at the end:

sqlSession.commit();

2.4 update

mapper interface method:

    boolean update(User user);

xml implementation:

    <update id="update" parameterType="com.hjxlog.domain.User">
        update public.user
        set name = #{name,jdbcType=VARCHAR},password = #{password,jdbcType=VARCHAR}
        where id = #{id,jdbcType=INTEGER}
    </update>

Test:

    @Test
    public void update(){
        // Get sqlSession object
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        try {
            // Execute sql
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            User user = new User(4,"huang-update","123456");
            boolean rlt = userMapper.update(user);
            System.out.println(rlt);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // Close sqlSession
            sqlSession.close();
        }
    }

2.5 delete

mapper interface:

    boolean delete(int id);

xml implementation:

    <delete id="delete" parameterType="int">
        delete from public.user where id = #{id,jdbcType=INTEGER}
    </delete>

Test:

    @Test
    public void delete() {
        // Get sqlSession object
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        // Execute sql
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        boolean rlt = userMapper.delete(4);
        System.out.println(rlt);
        sqlSession.commit();
        sqlSession.close();
    }

3, Application of Map in Mybatis

In the above example, when we want to modify the user attribute, we pass in the whole object. When we want to locally update a field value, we also need to find the user, assign a value to the field to be modified, and then execute the mybatis statement.

This is not flexible enough. When we have many fields or parameters, we can try to use Map as a parameter to pass to Mybatis, for example:

mapper interface:

    boolean update2(Map map);

xml implementation:

    <update id="update2" parameterType="map">
        update public.user
        set password = #{password,jdbcType=VARCHAR}
        where id = #{id,jdbcType=INTEGER}
    </update>

Test:

    @Test
    public void update2() {
        // Get sqlSession object
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        // Execute sql
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("password","edit-password");
        map.put("id",3);
        boolean rlt = userMapper.update2(map);
        System.out.println(rlt);
        sqlSession.commit();
        sqlSession.close();
    }

mybatis assigns values through the key and value of the map.

Maps can be used almost everywhere, but they are not the first choice. In later maintenance, if you see all maps, it is difficult to debug and find problems.

4, Summary

  1. Add, delete, or modify transactions that need to be committed.
  2. The package name path needs to be written correctly.
  3. Entity classes and database fields need one-to-one correspondence (you can use resultMap mapping later).
  4. If there are many object fields, but few fields need to be updated, consider using map.

Posted by infernon on Sun, 28 Nov 2021 10:28:36 -0800