MyBatis is developed using annotations

This is the 26th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Background: In order to simplify the code and improve the simplicity of the program at MyBatis level, there is the development of annotations. This article focuses on using annotations to implement simple add, delete, change and check. It’s also very simple to use. Sql annotations are mainly of the following four types:

1.@Insert 2.@Delete 3.@Update 4.@Select For annotation development, we no longer need the usermapper. XML configuration file, only the core configuration file mybatis-config. XML binding interface, It used to be registering mapper.xml files. However, the mapper.xml file is recommended for practical development

  1. Annotations are implemented on the interface

    Write an interface, and add annotations to the interface, write SQL statements in the annotations

// Query all users with annotations
    @Select("select * from users")
    List<User> getUserAll(a);
Copy the code
  1. Interfaces need to be bound in the core configuration file
<! -- Bind interface -->
    <mappers>
        <mapper class="dao.UserDao"/>
    </mappers>
Copy the code
  1. The test class
    // Query all users with annotations
    @Test
    public void getUserAll(a){
        SqlSession sqlSession = Mybatisutil.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> userAll = mapper.getUserAll();
        for (User user : userAll) {
            System.out.println(user);
        }
        sqlSession.close();
    }
Copy the code

Essence: Reflection mechanism implemented

Bottom layer: dynamic proxy

Use annotations for simple additions, deletions, changes, and reviews

We can implement automatic commit transactions when the utility class is created

public static SqlSession getSqlSession(a){
        return sqlSessionFactory.openSession(true);
    }
Copy the code

Write interfaces and add annotations

   @Select("select * from users")
    List<User> getUserAll(a);

    // Use annotations to query the user with the specified id
    // When a method has multiple arguments, all arguments must be preceded by @param ("") annotation
    @Select("select * from users where id = #{ids}")
    User getUserByIds(@Param("ids") int id);

    // Add a user using annotations
    @Insert("insert into users (id,username,password) value (#{id},#{username},#{password})")
    int addUsers(User user);

    // Modify the user with annotations
    @Update("update users set username=#{username},password=#{password}  where id=#{id};")
    int updateUser2(User user);

    // Delete a user with annotations
    @Delete("delete from firend_mq.users where id=#{ids}")
    int deleteUser2(@Param("ids") int id);
Copy the code

Note:

We must bind the interface to the core configuration file mybatis-config.xml

About the @param () annotation

  • Arguments of basic type or String type need to be added
  • Reference types do not need to be added
  • If there is only one basic type, it can be ignored, but it is recommended to add both
  • What we refer to in SQL is the property name we set in @param ()