当前位置:网站首页>Mybaits: interface proxy implementation Dao

Mybaits: interface proxy implementation Dao

2022-06-22 17:16:00 Liu Chu, Ge Nian

1. Interface proxy implementation Dao

1.1 Agent development mode introduction

​ use Mybatis The agent development method of DAO Layer development , This way is the mainstream for us to enter the enterprise .

Mapper Interface development methods only need programmers to write Mapper Interface ( amount to Dao Interface ), from Mybatis The framework creates the dynamic proxy object of the interface according to the definition of the interface , The method body of the proxy object is the same as the above Dao Interface implementation class methods .

Mapper Interface development needs to follow the following specifications :

1) Mapper.xml In the document namespace And mapper The full limit name of the interface is the same

2) Mapper Interface method names and Mapper.xml Each of the statement Of id identical

3) Mapper Input parameter types and mapper.xml Each of the sql Of parameterType Same type of

4) Mapper Output parameter types and mapper.xml Each of the sql Of resultType Same type of

summary :

The way of interface development : Programmers just need to define interfaces , You can operate the database , How to create specific objects ?

1. Programmers are responsible for defining interfaces

2. Operating the database ,mybatis The framework is based on the interface , Generate proxy objects through dynamic proxy , Responsible for database management crud operation

1.2. To write StudentMapper Interface

 Insert picture description here
For example, the following code example :

Mapper.class

/*  Persistence layer interface  */
public interface StudentMapper {
    
    // Query all 
    public abstract List<Student> selectAll();
}

Mapper.xml

<!-- mapper: The core root tag  namespace attribute : The name space  -->
<mapper namespace="com.yyl.mapper.StudentMapper">

    <sql id="select" >SELECT * FROM student</sql>

    <!-- select: The label of the query function  id attribute : Unique identification  resultType attribute : Specify the result mapping object type  parameterType attribute : Specifies the parameter mapping object type  -->
    <select id="selectAll" resultType="student">
        <include refid="select"/>
    </select>
</mapper>

1.3 Test agent mode

    public List<Student> selectAll() {
    
        List<Student> list = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try{
    
            //1. Load core profile 
            is = Resources.getResourceAsStream("MyBatisConfig.xml");

            //2. obtain SqlSession Factory object 
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //3. Get... Through factory objects SqlSession object 
            sqlSession = sqlSessionFactory.openSession(true);

            //4. obtain StudentMapper Implementation class object of interface 
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();

            //5. Call methods by implementing class objects , Receive results 
            list = mapper.selectAll();

        } catch (Exception e) {
    

        } finally {
    
            //6. Release resources 
            if(sqlSession != null) {
    
                sqlSession.close();
            }
            if(is != null) {
    
                try {
    
                    is.close();
                } catch (IOException e) {
    
                    e.printStackTrace();
                }
            }
        }

        //7. Return results 
        return list;
    }

The operation results are as follows :
 Insert picture description here

1.4 Source code analysis

  • Analyze how dynamic proxy objects are generated ?

    Through dynamic agent development mode , We only write one interface , Don't write implementation classes , We go through getMapper() Method finally gets org.apache.ibatis.binding.MapperProxy Proxy object , And then perform the function , And the proxy object is MyBatis Used JDK Dynamic proxy technology , Help us generate proxy implementation class objects . So that you can perform the relevant persistence operations .

  • How analytical methods are performed ?

    When the dynamic proxy implementation class object executes the method, it finally calls mapperMethod.execute() Method , In this method, through switch The statement is new according to the operation type 、 modify 、 Delete 、 Query operation , The last step is back to MyBatis The most original SqlSession Method to perform the addition, deletion, modification and query .

1.5 A summary of knowledge

Interface proxy allows us to write only interfaces , The implementation class object is created by MyBatis Generate .

Implementation rules :

  1. The namespace in the mapping configuration file must be the same as Dao The full class name of the layer interface is the same .
  2. Add, delete, and query tags in the mapping configuration file id Property must be the same as Dao Layer interface has the same method name .
  3. Add, delete, and query tags in the mapping configuration file parameterType Property must be the same as Dao The parameters of the layer interface method are the same .
  4. Add, delete, and query tags in the mapping configuration file resultType Property must be the same as Dao The return value of the layer interface method is the same .
  5. Get the dynamic proxy object SqlSession In the functional class getMapper() Method .
原网站

版权声明
本文为[Liu Chu, Ge Nian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221538403302.html