MyBatis - development using annotations

Posted by newman on Sat, 18 Dec 2021 03:56:38 +0100

**MyBatis - development using annotations**

8, Using annotation development

1. Interface oriented programming

  • In real development, interface oriented programming is often chosen;
  • Root cause: decoupling, scalability, improved reuse. In layered development, the upper layer does not care about the specific implementation. Everyone abides by common standards, making the development easier and more standardized.
Understanding of interfaces
  • From a deeper understanding, the interface should be the separation of definition (specification and constraint) and Implementation (the principle of separation of name and reality);
  • The interface itself reflects the system designer's abstract understanding of the system;
  • There shall be two types of interfaces:
    • The first type is the abstraction of an individual, which can correspond to an abstract class;
    • The second is the abstraction of an aspect of an individual, that is, the formation of an abstract interface;
  • An individual may have multiple Abstract surfaces, and there are differences between abstract and abstract;
Three oriented differences:
  • Object oriented means that when we consider a problem, we take the object as the unit and consider its attributes and methods;
  • Process oriented means that when we consider a problem, we consider its implementation in a specific process (transaction process);
  • Interface design and non interface design are aimed at reuse technology, which is not a problem with object-oriented (process), but more reflected in the overall architecture of the system;

2. Using annotation development

  • Annotations are implemented directly on the interface
    @Select("select * from user")
    List<User> getUserByAnnotation();
  • The interface needs to be bound in the core configuration file
    <mappers>
        <mapper class="com.lengzher.dao.UserMapper"/>
    </mappers>
  • test
    @Test
    public void getUserByAnnotation(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserByAnnotation();
        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }
  • Essence: reflection mechanism implementation
  • Bottom layer: dynamic proxy

3. Annotation implementation CRUD

  • Set auto commit transactions
    //Set to true to automatically commit transactions
	public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession(true);
    }
  • CRUD
    • Interface
    @Select("select * from mybatis.user")
        List<User> getUserByAnnotation();
    
        //Method has multiple parameters. All parameters must be preceded by @ Param ("field name") annotation
        @Select("select * from user where id = #{id}")
        User getUserById(@Param("id")int userId);
    
        @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
        int updateUser(User user);
    
        @Insert("insert into user (id,name,pwd)values(#{id},#{name},#{password})")
        int AddUser(User user);
    
        @Delete("delete from user where id =#{id}")
        int delUser(@Param("id")int id);
    
    • test
        @Test
        public void Test(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //Query user by id
            User userById = mapper.getUserById(2);
            System.out.println(userById);
    
            //Modify user
            int linia = mapper.updateUser(new User(2, "Linia", "1232"));
    
            //Add user
            int add = mapper.AddUser(new User(8,"lengzher","123112"));
    
            //delete user
            int del = mapper.delUser(9);
    
            sqlSession.close();
    
        }
    
    • Tips:

      • Be sure to bind the interface registration to our core configuration file!!
    • About @ Param() annotation

      • Parameters of basic type or String type need to be added
      • The reference type does not need to be added
      • If you need a basic type, you can ignore it, but it is recommended to add it!
      • We refer to the attribute name set in @ Param() in SQL!!!!
    • #The difference between {} and ${}

      • #{} can largely prevent sql injection

Topics: Java Spring