Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2014 abel533@gmail.com * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.linju.framework.pager.parser.impl; import com.linju.framework.pager.Constant; import com.linju.framework.pager.Dialect; import com.linju.framework.pager.Page; import com.linju.framework.pager.parser.Parser; import com.linju.framework.pager.parser.SqlParser; import com.linju.framework.pager.sqlsource.PageProviderSqlSource; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.ParameterMapping; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.SystemMetaObject; import org.apache.ibatis.session.Configuration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author liuzh */ public abstract class AbstractParser implements Parser, Constant { //?SQL public static final SqlParser sqlParser = new SqlParser(); public static Parser newParser(Dialect dialect) { Parser parser = null; switch (dialect) { case mysql: case mariadb: case sqlite: parser = new MysqlParser(); break; default: break; } return parser; } @Override public boolean isSupportedMappedStatementCache() { return true; } public String getCountSql(final String sql) { return sqlParser.getSmartCountSql(sql); } public abstract String getPageSql(String sql); public List<ParameterMapping> getPageParameterMapping(Configuration configuration, BoundSql boundSql) { List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(); if (boundSql.getParameterMappings() != null) { newParameterMappings.addAll(boundSql.getParameterMappings()); } newParameterMappings .add(new ParameterMapping.Builder(configuration, PAGEPARAMETER_FIRST, Integer.class).build()); newParameterMappings .add(new ParameterMapping.Builder(configuration, PAGEPARAMETER_SECOND, Integer.class).build()); return newParameterMappings; } public Map setPageParameter(MappedStatement ms, Object parameterObject, BoundSql boundSql, Page page) { Map paramMap = null; if (parameterObject == null) { paramMap = new HashMap(); } else if (parameterObject instanceof Map) { paramMap = (Map) parameterObject; } else { paramMap = new HashMap(); //?sql??ParameterMapping?getter //TypeHandlerRegistry??? boolean hasTypeHandler = ms.getConfiguration().getTypeHandlerRegistry() .hasTypeHandler(parameterObject.getClass()); MetaObject metaObject = SystemMetaObject.forObject(parameterObject); //??MyProviderSqlSource? if (ms.getSqlSource() instanceof PageProviderSqlSource) { paramMap.put(PROVIDER_OBJECT, parameterObject); } if (!hasTypeHandler) { for (String name : metaObject.getGetterNames()) { paramMap.put(name, metaObject.getValue(name)); } } //???? if (boundSql.getParameterMappings() != null && boundSql.getParameterMappings().size() > 0) { for (ParameterMapping parameterMapping : boundSql.getParameterMappings()) { String name = parameterMapping.getProperty(); if (!name.equals(PAGEPARAMETER_FIRST) && !name.equals(PAGEPARAMETER_SECOND) && paramMap.get(name) == null) { if (hasTypeHandler || parameterMapping.getJavaType().equals(parameterObject.getClass())) { paramMap.put(name, parameterObject); break; } } } } } //? paramMap.put(ORIGINAL_PARAMETER_OBJECT, parameterObject); return paramMap; } }