1. Develop with annotations

1.1 Interface Oriented Programming

  • You’ve all learned about object-oriented programming before, you’ve all learned about interfaces, but in real development, a lot of the time, you choose to program to interfaces
  • Root reason: Decoupling, extensibility, improved reuse. In layered development, the upper layer doesn’t care about the specific implementation, and everyone follows the common standard, which makes development easier and more standardized
  • In an object-oriented system, the various functions of the system are accomplished by the cooperation of many different objects. In this case, how each object implements itself internally is less important to the system designer;
  • And the cooperation between each object becomes the key of the system design. Small to the communication between different classes, large to the interaction between the modules, at the beginning of the system design is to focus on consideration, which is the main work content of system design. Programming to an interface means programming with this idea in mind.

Understanding of interfaces

  • Interfaces, at a deeper level, should be a separation of definitions (specifications, constraints) and implementations (the principle of separation of names and facts).
  • The interface itself reflects the system designer’s abstract understanding of the system.
  • There should be two types of interfaces:
  • The first – class is an abstraction of an individual, which corresponds to an abstract class.
  • The second kind is the abstraction of a certain aspect of an individual, that is, to form an abstract surface (interface);
  • An individual may have more than one surface of abstraction. There is a difference between an abstract body and an abstract surface.

Three faces to the difference

  • Object-oriented means that when we think about a problem, we think about an object in terms of its properties and its methods
  • Process-oriented means that when we think about a problem, we think about its implementation in terms of a specific process (transaction process)
  • Interface design and non-interface design are for reuse technology, and object-oriented (process) is not a problem. More is reflected in the overall architecture of the system

1.2 Annotations Development

1. Annotations are implemented on interface methods

public interface UserDao{

    @Select("select * from user")
    List<User> getUsers();
}

2. MyBatis core configuration file mabatis-config.xml

<! > <mappers> <mapper class=" com.jialidun.dao.userdao "/> </mappers>

3. The test

@Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); // UserMapper Mapper = SQLSession.getMapper (UserDap.class); List<User< userList = mapper.getUsers(); for(User user : userList){ System.out.println(user); } sqlSession.close(); }

Essence: Reflection mechanism to achieve the underlying: dynamic proxy pattern!

Execute the process