当前位置:网站首页>Complete the sqlsession interface and implementation classes

Complete the sqlsession interface and implementation classes

2022-06-22 18:38:00 Procedural ape Xiaobing

Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 23 God , Click to see the event details

In the last course , According to the analysis of the previous design ideas , The next task to complete is to create SqlSession Interfaces and implementation classes DefaultSqlSession. Of course for SqlSession Creation of interfaces and implementation classes , You have completed the creation of , However, there are no methods to add, delete, modify and query the database in the interface and implementation classes .

In this course , The task is to write these method definitions . In the custom persistence layer framework , Just finish selectList and selectOne These two methods are written and implemented , It will not write and implement all the ways of adding, deleting, modifying and querying . These two methods have covered many cases . such as ,selectOne It already contains both the return result to be encapsulated and the parameters to be set .

Whether you want to complete the modification or delete , As long as you can put selectOne Realize it , Then complete the modification and deletion . I believe you can achieve it . It's only done here selectList and selectOne Write and implement .

stay SqlSession Define methods in interfaces

In this SqlSession Two methods are defined in the interface , One is to query all selectList() Method , The other is based on conditions selectOne() Method .

Query all selectList() The return value of List aggregate , meanwhile List How a collection's generics are defined . for example , Query all operations of the user module , Whether to encapsulate the results of all query processing into one user (User) object , Then put these users (User) Object encapsulation into List Set to return .

If we implement the user module ,List The stereotype in the collection should be the user (User). But it is also possible to complete the commodity module . If it is a commodity module , We need to package each record in the query product table into a product (Product ) To return .

This means that when customizing the persistence layer framework ,SqlSession Interface selectList A generic type of cannot be written as a concrete entity , therefore , Put this selectList() A collection generic definition of the return value of a method E, As long as the same type can be guaranteed .

that , This selectList() Method requires no arguments , The answer is yes . among , A parameter is a string type statementId. The user side calls again SqlSession Interface selectList() Method time , You need to pass on a statementId. This statementId This corresponds to the mapping configuration file namedspace.id, because namespace.id yes SQL The unique identifier of the statement . Only pass it on statementId Only then do I know which one to implement finally SQL sentence , To be able to pass this statementId stay Configuration In the entity , according to Map Collection K The value is encapsulated in advance MapperStatement. Because I've been right XML The file is parsed , The inside of SQL sentence 、 Parameter type 、 The returned result type and other information are all encapsulated in MapperStatement. Want to take out the corresponding MapperStatement, You must pass a message to the user statementId.

Except for this statementId Outside the parameters of the , Do you need any other parameters , If you query all , Only statementId that will do , But if you use fuzzy queries , And the returned results are also multiple records , So here we need another parameter , It's a Object Variable parameters of type .

Again , In defining selectOne() Methods and definitions selectList() The method is the same , The return value type is an entity type .

public interface SqlSession {
    //  Query all 
    public <E> List<E> selectList(String statementId, Object... params);
    // Query... According to the conditions 
    public <T> T  selectOne(String statementId, Object... params);
}

stay SqlSession The interface is defined selectList() Methods and selectOne() After method , In the test class on the user side , adopt slqSession. You can see these two methods , As shown in the figure below .

  image.png

complete SqlSession In the implementation class selectOne Method

 

stay selectList and selectOne In the method , To complete JDBC Code writing , But this is not a good way to achieve , We are thinking of JDBC Then go to the next layer , That is, this was created in the previous thought analysis Executor Interface and its implementation class SimpleExecuto. hold JDBC Code into this interface and implementation class query() In the method .

And in the selectList() and selectOne() Method wants to interact with the database , Underlying execution JDBC Code , Only need selectList() and selectOne() Method to call this SimpleExecutor Inside query() The method can , And pass some parameters it needs .

In the realization of selectList() and selectOne() Methods these two methods are , What we mainly achieve is selectList() Method .selectOne When this method is executed , call selectList() The method can . Calling selectList() When the method is used , Get the corresponding result set , And judge the obtained result set . If the length of the result set is 1, Returns the record of the current result set , If the length of the result set is not 1, Throw a runtime exception .

  @Override
  public <T> T selectOne(String statementId, Object... params) {


      List<Object> objects = selectList(statementId, params);
      if (objects.size()==1){
          return (T)objects.get(0);
      }else {
          throw new RuntimeException(" The query result is empty or too many results are returned ");
      }


  }

establish Executor Interfaces and implementation classes SimpleExecutor

In this selectList() In the method , About to finish SimpleExecutor Class query() Method call . at present ,SimpleExecutor Classes and Executor The interface has not yet , Based on the principle of opening and closing , The first Executor Interface and its implementation class SimpleExecutor To create , The specific implementation will be completed in the later course .

establish Executor Interface and define query() Method . The specific code is as follows :

public interface Executor {
    public <E> List<E> query(Configuration configuration, MapperStatement mapperStatement,Object... params);
}

 

SimpleExecutor Class implements the Executor Interface , And rewritten Executor Interface query() Method . The specific code is as follows :

public class SimpleExecutor implements Executor{
    @Override
    public <E> List<E> query(Configuration configuration, MapperStatement mapperStatement, Object... params) {
        return null;
    }
}

complete SqlSession In the implementation class selectList Method

hold Executor Interface and implementation classes SimpleExecutor After creating , that , We can do that selectList() Method to complete query() call .

@Override
public <E> List<E> selectList(String statementId, Object... params) {
  
    SimpleExecutor simpleExecutor=new SimpleExecutor();


    MapperStatement mapperStatement=configuration.getMapperStatementMap().get(statementId);
    
    List<Object> list = simpleExecutor.query(configuration, mapperStatement, params);


    return (List<E>) list;
}

Calling query() When the method is used , You need to pass three parameters :

The first parameter is configuration. This parameter is used in production defaultSqlSession when , Pass down configuration.

The second parameter is mapperStatement. This mapperStatement Where is the package ? Packaged in Configuration Medium Map Collection , adopt statementId obtain Map Collection mapperStatement.

The third parameter is params Variable parameter .

The final will be List Set to return .

This course is completed SqlSession Interface method definition and completion DefaultSqlSession Class method implementation . In the next course , About to finish SimpleExecutor Class query() Method .

Summary

This session , stay SqlSession Two methods are defined in the interface , One is selectList() and selectOne() Method , And in SqlSession Implementation class of DefaultSqlSession Class implements two methods in . At the same time, based on the principle of opening and closing Executor Interface and implementation classes SimpleExecutor class , And define... In the interface query() Method .

原网站

版权声明
本文为[Procedural ape Xiaobing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221706356754.html