Mybatis中使用的设计模式

本文探讨了Mybatis中如何运用建造者模式解析配置文件,Builder角色和ConcreteBuilder角色分别负责不同类型的配置文件,Product角色对应解析结果,Director角色协调构建过程。此外,还分析了Mybatis的策略模式应用,StatementHandler采用策略模式,其中抽象策略类为Statement,具体策略类如PreparedStatement和SimpleStatement,Context角色即RoutingStatementHandler根据StatementType选择实现类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

建造者模式

这里讲得很清楚了
深入浅出设计模式——建造者模式(Builder Pattern)

在mybatis中,在解析配置文件的时候使用。
Builder角色是org.apache.ibatis.builder.BaseBuilder
ConcreteBuilder角色就是它的实现类,分别负责解析不同类型的配置文件,包括

org.apache.ibatis.builder.xml.XMLConfigBuilder
org.apache.ibatis.builder.xml.XMLMapperBuilder
org.apache.ibatis.builder.xml.XMLStatementBuilder
org.apache.ibatis.builder.SqlSourceBuilder

Product角色就是org.apache.ibatis.session.Configuration
Director角色就是org.mybatis.spring.SqlSessionFactoryBean


策略模式

详细介绍看这里
JAVA设计模式详解——策略模式

在mybatis中,StatementHandler就是使用策略模式。
抽象策略类(Strategy)角色是org.apache.ibatis.executor.statement.StatementHandler
具体策略类(ConcreteStrategy)角色是

org.apache.ibatis.executor.statement.CallableStatementHandler
org.apache.ibatis.executor.statement.PreparedStatementHandler
org.apache.ibatis.executor.statement.SimpleStatementHandler

环境类(Context)角色是org.apache.ibatis.executor.statement.RoutingStatementHandler

RoutingStatementHandler会根据StatementType选择对应的StatementHandler实现类


整理中。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值