组合( Composite )模式就是把对象组合成树形结构,以表示“部分-总体”的层次结构,用户能够像处理一个简单对象同样来处理一个复杂对象,从而使得调用者无需了解复杂元素的内部结构。前端
组合模式中的角色有:java
抽象组件(容器):定义了树形结构中全部类的公共行为,例如add(),remove()等方法。
树叶:最终实现类,没有子类。
树枝:有子类的管理类,并经过管理方法调用其管理的子类的相关操做。
调用者:经过容器接口操做整个树形结构。
具体组合模式的例子能够参考 设计模式整理node
如今咱们来讲一下SqlNode是什么,来看这么一段配置文件正则表达式
<select id="findByGameTypeCount" resultType="java.lang.Long"> select count(*)
from betdetails a inner join UserBetOrder b on a.orderId = b.id <where> <if test="gameType != null and gameType > 0"> a.gameType = #{gameType} and </if> <if test="currDrawno != null"> b.currentDrawno = #{currDrawno} and </if> <if test="orderId != null and orderId > 0"> a.orderId = #{orderId} and </if> <if test="status != null and status >= 0"> a.status = #{status} and </if> <if test="userId != null and userId > 0"> b.userId = #{userId} and </if> <if test="start != null"> a.createTime >= #{start} and </if> <if test="end != null"> a.createTime <= #{end} and </if> 1 = 1 </where></select>
<insert id="insertBetdetailsByBatch" parameterType="java.util.List"> insert into betdetails(id,orderId,actorIndex,createTime,ballIndex,ballValue,betAmount,rate1,rate2,rate3,gameType,status,betResult,awardAmount,ballName) values <foreach collection="list" item="item" index="index" separator=","> (#{item.id},#{item.orderId},#{item.actorIndex},#{item.createTime},#{item.ballIndex},#{item.ballValue},#{item.betAmount},#{item.rate1},#{item.rate2},#{item.rate3},#{item.gameType},#{item.status},#{item.betResult},#{item.awardAmount},#{item.ballName}) </foreach></insert>
这其中的<if><where><foreach>节点就是SqlNode节点,SqlNode是一个接口,表明着组合模式中的容器。只要是有SqlNode,那就表明着必定是一个动态的SQL,里面就有可能会有参数#{}sql
public interface SqlNode {
//SqlNode接口中定义的惟一方法,该方法会根据用户传入的实参,解析该SqlNode所记录的动态SQL节点,并调用DynamicContext.appendSql()方法将解析后的SQL片断追加到
//DynamicContext.sqlBuilder中保存
//当SQL节点下的全部SqlNode完成解析后,就能够从DynamicContext中获取一条动态生成的完整的SQL语句 boolean apply(DynamicContext context);}
咱们先来看一下DynamicContext是什么,它的核心字段以下express
private final ContextMap bindings; //参考上下文
//在SqlNode解析动态SQL时,会将解析后的SQL语句片断添加到该属性中保存,最终拼凑出一条完成的SQL语句private final StringBuilder sqlBuilder = new StringBuilder();
ContextMap是一个内部类,继承于HashMap,重写了get方法后端
static class ContextMap extends HashMap<String, Object> { private static final long serialVersionUID = 2977601501966151582L; //将用户传入的参数封装成MetaObject对象(类实例中检查类的属性是否包含getter,setter方法) private MetaObject parameterMetaObject; public ContextMap(MetaObject parameterMetaObject) { this.parameterMetaObject = parameterMetaObject; } @Override public Object get(Object key) {设计模式
String strKey = (String) key; //若是ContextMap中已经包含了该key,则直接返回 if (super.containsKey(strKey)) { return super.get(strKey); } //若是不包含该key,从parameterMetaObject中查找对应属性 if (parameterMetaObject != null) { // issue #61 do not modify the context when reading return parameterMetaObject.getValue(strKey); } return null; }
}
public void appendSql(String sql) { sqlBuilder.append(sql); sqlBuilder.append(" ");}
SqlNode的实现类以下缓存
其中MixedSqlNode是树枝,TextSqlNode是树叶....app
咱们先来看一下TextSqlNode,TextSqlNode表示的是包含${}占位符的动态SQL节点。它的接口实现方法以下
@Overridepublic boolean apply(DynamicContext context) {
//将动态SQL(带${}占位符的SQL)解析成完成SQL语句的解析器,即将${}占位符替换成实际的变量值
GenericTokenParser parser = createParser(new BindingTokenParser(context, injectionFilter));
//将解析后的SQL片断添加到DynamicContext中 context.appendSql(parser.parse(text)); return true;}
BindingTokenParser是TextNode中定义的内部类,继承了TokenHandler接口,它的主要做用是根据DynamicContext.bindings集合中的信息解析SQL语句节点中的${}占位符。
private DynamicContext context;
private Pattern injectionFilter; //须要匹配的正则表达式
@Overridepublic String handleToken(String content) {
//获取用户提供的实参
Object parameter = context.getBindings().get("_parameter");
//若是实参为null if (parameter == null) {
//将参考上下文的value key设为null context.getBindings().put("value", null); //若是实参是一个经常使用数据类型的类(Integer.class,String.class,Byte.class等等) } else if (SimpleTypeRegistry.isSimpleType(parameter.getClass())) { //将参考上下文的value key设为该实参 context.getBindings().put("value", parameter); }
//经过OGNL解析参考上下文的值
Object value = OgnlCache.getValue(content, context.getBindings()); String srtValue = (value == null ? "" : String.valueOf(value)); // issue #274 return "" instead of "null"
//检测合法性 checkInjection(srtValue); return srtValue;}
private void checkInjection(String value) { if (injectionFilter != null && !injectionFilter.matcher(value).matches()) { throw new ScriptingException("Invalid input. Please conform to regex" + injectionFilter.pattern()); }
}
在OgnlCache中,对原生的OGNL进行了封装。OGNL表达式的解析过程是比较耗时的,为了提升效率,OgnlCache中使用了expressionCashe字段(ConcurrentHashMap<String,Object>类型)对解析后的OGNL表达式进行缓存。为了说明OGNL,咱们先来看一个例子
@Data@ToStringpublic class User { private int id; private String name;}
public class OGNLDemo { public void testOgnl1() throws OgnlException {
OgnlContext context = new OgnlContext(); context.put("cn","China"); String value = (String) context.get("cn"); System.out.println(value); User user = new User(); user.setId(100); user.setName("Jack"); context.put("user",user); Object u = context.get("user"); System.out.println(u); Object ognl = Ognl.parseExpression("#user.id"); Object value1 = Ognl.getValue(ognl,context,context.getRoot()); System.out.println(value1); User user1 = new User(); user1.setId(200); user1.setName("Mark"); context.setRoot(user1); Object ognl1 = Ognl.parseExpression("id"); Object value2 = Ognl.getValue(ognl1,context,context.getRoot()); System.out.println(value2); Object ognl2 = Ognl.parseExpression("@@floor(10.9)"); Object value3 = Ognl.getValue(ognl2, context, context.getRoot()); System.out.println(value3); } public static void main(String[] args) throws OgnlException { OGNLDemo demo = new OGNLDemo(); demo.testOgnl1(); }
}
运行结果:
China
User(id=100, name=Jack)
100
200
10.0
private static final Map<String, Object> expressionCache = new ConcurrentHashMap<String, Object>();
public static Object getValue(String expression, Object root) { try {
//建立OgnlContext对象 Map<Object, OgnlClassResolver> context = Ognl.createDefaultContext(root, new OgnlClassResolver()); //使用OGNL执行expression表达式 return Ognl.getValue(parseExpression(expression), context, root); } catch (OgnlException e) { throw new BuilderException("Error evaluating expression '" + expression + "'. Cause: " + e, e); }
}
private static Object parseExpression(String expression) throws OgnlException {
//查找缓存
Object node = expressionCache.get(expression); if (node == null) {
//解析表达式 node = Ognl.parseExpression(expression); //将表达式的解析结果添加到缓存中 expressionCache.put(expression, node); } return node;}
StaticTextSqlNode很简单,就是直接返回SQL语句
public class StaticTextSqlNode implements SqlNode { private final String text; public StaticTextSqlNode(String text) { this.text = text; } @Override public boolean apply(DynamicContext context) {
context.appendSql(text); return true; }
}
IfSqlNode是解析<if>节点,字段含义以下
//用于解析<if>节点的test表达式的值
private final ExpressionEvaluator evaluator;
//记录<if>节点中test表达式private final String test;
//记录了<if>节点的子节点private final SqlNode contents;
接口方法以下
@Overridepublic boolean apply(DynamicContext context) {
//检测test属性中记录的表达式 if (evaluator.evaluateBoolean(test, context.getBindings())) {
//若是test表达式为true,则执行子节点的apply()方法 contents.apply(context); return true; //返回test表达式的结果为true } return false; //返回test表达式的结果为false}
在ExpressionEvaluator中
public boolean evaluateBoolean(String expression, Object parameterObject) {
//用OGNL解析expression表达式
Object value = OgnlCache.getValue(expression, parameterObject);
//处理Boolean类型 if (value instanceof Boolean) { return (Boolean) value; }
//处理数字类型 if (value instanceof Number) { return new BigDecimal(String.valueOf(value)).compareTo(BigDecimal.ZERO) != 0; } return value != null;}
TrimSqlNode会根据子节点的解析结果,添加或删除响应的前缀或后缀,好比有这么一段配置
<insert id="insertNotNullBetdetails" parameterType="com.cloud.model.game.Betdetails"> insert into betdetails <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null">id,</if> <if test="orderId != null">orderId,</if> <if test="actorIndex != null">actorIndex,</if> <if test="ballIndex != null">ballIndex,</if> <if test="ballValue != null">ballValue,</if> <if test="betAmount != null">betAmount,</if> <if test="createTime != null">createTime,</if> <if test="rate1 != null">rate1,</if> <if test="rate2 != null">rate2,</if> <if test="rate3 != null">rate3,</if> <if test="gameType != null">gameType,</if> <if test="status != null">status,</if> <if test="betResult != null">betResult,</if> <if test="awardAmount != null">awardAmount,</if> <if test="ballName != null">ballName,</if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null">#{id},</if> <if test="orderId != null">#{orderId},</if> <if test="actorIndex != null">#{actorIndex},</if> <if test="createTime != null">#{createTime},</if> <if test="ballIndex != null">#{ballIndex},</if> <if test="ballValue != null">#{ballValue},</if> <if test="betAmount != null">#{betAmount},</if> <if test="rate1 != null">#{rate1},</if> <if test="rate2 != null">#{rate2},</if> <if test="rate3 != null">#{rate3},</if> <if test="gameType != null">#{gameType},</if> <if test="status != null">#{status},</if> <if test="betResult != null">#{betResult},</if> <if test="awardAmount != null">#{awardAmount},</if> <if test="ballName != null">#{ballName},</if> </trim></insert>
TrimSqlNode中字段含义以下
private final SqlNode contents; //该<trim>节点的子节点private final String prefix; //记录了前缀字符串(为<trim>节点包裹的SQL语句添加的前缀)private final String suffix; //记录了后缀字符串(为<trim>节点包裹的SQL语句添加的后缀)
//若是<trim>节点包裹的SQL语句是空语句,删除指定的前缀,如whereprivate final List<String> prefixesToOverride;
//若是<trim>节点包裹的SQL语句是空语句,删除指定的后缀,如逗号private final List<String> suffixesToOverride;
它的接口方法以下
@Overridepublic boolean apply(DynamicContext context) {
//建立FilteredDynamicContext对象,FilteredDynamicContext是TrimSqlNode的内部类,继承于DynamicContext
FilteredDynamicContext filteredDynamicContext = new FilteredDynamicContext(context);
//调用子节点的apply()方法进行解析,注意收集SQL语句的是filteredDynamicContext
boolean result = contents.apply(filteredDynamicContext);
//处理前缀和后缀 filteredDynamicContext.applyAll(); return result;}
FilteredDynamicContext的字段属性含义以下
private DynamicContext delegate; //底层封装的DynamicContext对象private boolean prefixApplied; //是否已经处理过前缀private boolean suffixApplied; //是否已经处理事后缀private StringBuilder sqlBuffer; //用于记录子节点解析后的结果
FilteredDynamicContext的applyAll()方法
public void applyAll() {
//获取子节点解析后的结果,并所有转化为大写 sqlBuffer = new StringBuilder(sqlBuffer.toString().trim()); String trimmedUppercaseSql = sqlBuffer.toString().toUpperCase(Locale.ENGLISH); if (trimmedUppercaseSql.length() > 0) {
//处理前缀 applyPrefix(sqlBuffer, trimmedUppercaseSql); //处理后缀 applySuffix(sqlBuffer, trimmedUppercaseSql); }
//将解析后的结果SQL片断添加到DynamicContext的StringBuilder中 delegate.appendSql(sqlBuffer.toString());}
private void applyPrefix(StringBuilder sql, String trimmedUppercaseSql) { if (!prefixApplied) { //若是尚未处理过前缀 prefixApplied = true; //更新为已处理 if (prefixesToOverride != null) { //若是须要删除的前缀列表不为null
//遍历该前缀列表 for (String toRemove : prefixesToOverride) { //若是<trim>子节点收集上来的SQL语句以该前缀开头 if (trimmedUppercaseSql.startsWith(toRemove)) { //从<trim>子节点收集上来的StringBuilder中删除该前端 sql.delete(0, toRemove.trim().length()); break; } } } //若是有前缀字符串(好比说"("),将前缀字符串插入StringBuilder最前端 if (prefix != null) { sql.insert(0, " "); sql.insert(0, prefix); }
}
}
private void applySuffix(StringBuilder sql, String trimmedUppercaseSql) { if (!suffixApplied) { //若是尚未处理事后缀 suffixApplied = true; //更新为已处理后缀 if (suffixesToOverride != null) { //若是须要处理的后缀列表不为null
//遍历该后缀列表 for (String toRemove : suffixesToOverride) { //若是从<trim>子节点收集上来的SQL语句以该后缀结尾 if (trimmedUppercaseSql.endsWith(toRemove) || trimmedUppercaseSql.endsWith(toRemove.trim())) { //获取该后缀的起始位置 int start = sql.length() - toRemove.trim().length(); //获取该后缀的终止位置 int end = sql.length(); //从<trim>子节点收集上来的StringBuilder中删除该后端 sql.delete(start, end); break; } } } //若是有后缀字符串(好比说")"),将前缀字符串拼接上StringBuilder最后端 if (suffix != null) { sql.append(" "); sql.append(suffix); }
}
}
WhereSqlNode和SetSqlNode都继承于TrimSqlNode,他们只是在TrimSqlNode的属性中指定了固定的标记。
public class WhereSqlNode extends TrimSqlNode { private static List<String> prefixList = Arrays.asList("AND ","OR ","ANDn", "ORn", "ANDr", "ORr", "ANDt", "ORt"); public WhereSqlNode(Configuration configuration, SqlNode contents) { super(configuration, contents, "WHERE", prefixList, null, null); }
}
public class SetSqlNode extends TrimSqlNode { private static List<String> suffixList = Arrays.asList(","); public SetSqlNode(Configuration configuration,SqlNode contents) { super(configuration, contents, "SET", null, null, suffixList); }
}
ForEachSqlNode,在动态SQL语句中,一般须要对一个集合进行迭代,Mybatis提供了<foreach>标签实现该功能。在使用<foreach>标签迭代集合时,不只可使用集合的元素和索引值,还能够在循环开始以前或结束以后添加指定的字符串,也容许在迭代过程当中添加指定的分隔符。配置样例以下
<insert id="insertBetdetailsByBatch" parameterType="java.util.List"> insert into betdetails(id,orderId,actorIndex,createTime,ballIndex,ballValue,betAmount,rate1,rate2,rate3,gameType,status,betResult,awardAmount,ballName) values <foreach collection="list" item="item" index="index" separator=","> (#{item.id},#{item.orderId},#{item.actorIndex},#{item.createTime},#{item.ballIndex},#{item.ballValue},#{item.betAmount},#{item.rate1},#{item.rate2},#{item.rate3},#{item.gameType},#{item.status},#{item.betResult},#{item.awardAmount},#{item.ballName}) </foreach></insert>
ForEachSqlNode中各个字段含义以下:
private final ExpressionEvaluator evaluator;private final String collectionExpression;private final SqlNode contents;private final String open;private final String close;private final String separator;private final String item;private final String index;private final Configuration configuration;