当前位置:网站首页>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 .
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 .
边栏推荐
- 2022年G2电站锅炉司炉题库及在线模拟考试
- Excuse me, when cdc2.0 reads mysql, there should be no table lock. An error was just reported, access D
- Live streaming: dongyuhui is so popular. Is there anyone watching liuzhenhong?
- JSP learning (I) -- overview of JSP
- Grafana 9 is officially released, which is easier to use and more cool!
- Five practical tips for power Bi (complimentary books at the end of the article)
- 第四届青年生命科学论坛 | 第一轮通知
- 【 outils】 utilisation de PIP et de conda
- The Fourth Youth Life Science Forum | first round notice
- SOA面向服务的架构
猜你喜欢

JSP learning (3) -- JSP implicit object

sqlserver保存时遇到这个页面怎么回事啊

国产手机干翻苹果?原来是靠百元机和猛降价实现的

【win11】注册表修改fix 右键没有新建

Missing value handling

JSP learning (I) -- overview of JSP
![[applet project development -- Jingdong Mall] configuration tabbar & window style for uni app development](/img/cd/bdf26a02a43c63f374861e8431787c.png)
[applet project development -- Jingdong Mall] configuration tabbar & window style for uni app development

Pytorch——报错解决:“torch/optim/adamw.py” beta1, UnboundLocalError: local variable ‘beta1‘

Binary tree practice the second bullet

A course for New Oriental transformation bilingual live broadcast to bring goods to the project manager
随机推荐
Short video live broadcast source code, use of EditText input box
数组模拟栈
阿里云过户找不到账号安全组ID问题
Game NFT Market: opensea's most easily cut cake
Golang实现基于Redis的可靠延迟队列
Filebeat collects log data and transfers it to redis. Different es indexes are created based on log fields through logstash
Jenkins中node节点添加之SSH方式2
Gridhome, a must-have static site generator for beginners
基于转换器 (MMC) 技术和电压源转换器 (VSC) 的高压直流 (HVDC) 模型(Matlab&Simulink实现)
腾讯云国际版云服务器欠费说明
Huawei cloud "digital intelligence" operation and maintenance
传统图像--LBP特征
< JVM part I: memory and garbage collection part > 08 object instantiation and direct memory
Filebeat收集日志数据传输到Redis,通过Logstash来根据日志字段创建不同的ES索引
List的同步类比较
JSP learning (I) -- overview of JSP
Azkaban startup error 2022/06/20 21:39:27.726 +0800 error [stdouterrredirect] [azkaban] exception in thread "m
Jenkins installation and upgrade
【工具】pip和conda的相關使用
Does CDC 2.2.1 monitoring sqlserver not support monitoring multiple databases?