com.zzy.mybatis.mapper.MapperProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.zzy.mybatis.mapper.MapperProvider.java

Source

/*
 * 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.zzy.mybatis.mapper;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.scripting.xmltags.IfSqlNode;
import org.apache.ibatis.scripting.xmltags.MixedSqlNode;
import org.apache.ibatis.scripting.xmltags.SetSqlNode;
import org.apache.ibatis.scripting.xmltags.SqlNode;
import org.apache.ibatis.scripting.xmltags.StaticTextSqlNode;
import org.apache.ibatis.scripting.xmltags.TrimSqlNode;
import org.apache.ibatis.scripting.xmltags.VarDeclSqlNode;
import org.apache.ibatis.scripting.xmltags.WhereSqlNode;

import com.zzy.mybatis.mapperhelper.EntityHelper;
import com.zzy.mybatis.mapperhelper.MapperHelper;
import com.zzy.mybatis.mapperhelper.MapperTemplate;

/**
 * Mappper????
 *
 * @author liuzh
 */
public class MapperProvider extends MapperTemplate {

    public MapperProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    /**
     * 
     *
     * @param ms
     * @return
     */
    public SqlNode select(MappedStatement ms) {
        Class<?> entityClass = getSelectReturnType(ms);
        //
        setResultType(ms, entityClass);
        List<SqlNode> sqlNodes = new ArrayList<SqlNode>();
        //??sql:select column ... from table
        sqlNodes.add(new StaticTextSqlNode(
                "SELECT " + EntityHelper.getSelectColumns(entityClass) + " FROM " + tableName(entityClass)));
        //if<where>
        sqlNodes.add(new WhereSqlNode(ms.getConfiguration(), getAllIfColumnNode(entityClass)));
        return new MixedSqlNode(sqlNodes);
    }

    /**
     * ?
     *
     * @param ms
     */
    public void selectByPrimaryKey(MappedStatement ms) {
        final Class<?> entityClass = getSelectReturnType(ms);
        //?
        List<ParameterMapping> parameterMappings = getPrimaryKeyParameterMappings(ms);
        //sql
        String sql = new SQL() {
            {
                //select
                SELECT(EntityHelper.getSelectColumns(entityClass));
                //from
                FROM(tableName(entityClass));
                //where?=#{property}
                WHERE(EntityHelper.getPrimaryKeyWhere(entityClass));
            }
        }.toString();
        //??SqlSource
        StaticSqlSource sqlSource = new StaticSqlSource(ms.getConfiguration(), sql, parameterMappings);
        //?SqlSource
        setSqlSource(ms, sqlSource);
        //
        setResultType(ms, entityClass);
    }

    /**
     * 
     *
     * @param ms
     * @return
     */
    public SqlNode selectCount(MappedStatement ms) {
        Class<?> entityClass = getSelectReturnType(ms);
        List<SqlNode> sqlNodes = new ArrayList<SqlNode>();
        //select count(*) from table
        sqlNodes.add(new StaticTextSqlNode("SELECT COUNT(*) FROM " + tableName(entityClass)));
        //?where,if?
        sqlNodes.add(new WhereSqlNode(ms.getConfiguration(), getAllIfColumnNode(entityClass)));
        return new MixedSqlNode(sqlNodes);
    }

    /**
     * ?
     *
     * @param ms
     * @return
     */
    public SqlNode insert(MappedStatement ms) {
        Class<?> entityClass = getSelectReturnType(ms);
        List<SqlNode> sqlNodes = new ArrayList<SqlNode>();
        //insert into table
        sqlNodes.add(new StaticTextSqlNode("INSERT INTO " + tableName(entityClass)));
        //?
        List<EntityHelper.EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        //Identity?
        Boolean hasIdentityKey = false;
        //?
        for (EntityHelper.EntityColumn column : columnList) {
            //?sql????
            if (column.getSequenceName() != null && column.getSequenceName().length() > 0) {
            } else if (column.isIdentity()) {
                //Identity??selectKey
                //?Identity
                if (hasIdentityKey) {
                    throw new RuntimeException(ms.getId() + "" + entityClass.getCanonicalName()
                            + "?MySql,?!");
                }
                //?selectKey
                newSelectKeyMappedStatement(ms, column);
                hasIdentityKey = true;
                //?,,??,?
                //bind
                sqlNodes.add(new VarDeclSqlNode(column.getProperty() + "_cache", column.getProperty()));
            } else if (column.isUuid()) {
                //uuid?bind
                sqlNodes.add(new VarDeclSqlNode(column.getProperty() + "_bind", getUUID()));
            }
        }
        //?(??,??...)
        sqlNodes.add(new StaticTextSqlNode("(" + EntityHelper.getAllColumns(entityClass) + ")"));
        List<SqlNode> ifNodes = new ArrayList<SqlNode>();
        //?values(,...)
        for (EntityHelper.EntityColumn column : columnList) {
            //,property!=null
            //,,property_cache,??
            if (column.isIdentity()) {
                ifNodes.add(getIfCacheNotNull(column,
                        new StaticTextSqlNode("#{" + column.getProperty() + "_cache },")));
            } else {
                //?property
                ifNodes.add(getIfNotNull(column, new StaticTextSqlNode("#{" + column.getProperty() + "},")));
            }
            //null??null
            //?
            if (column.getSequenceName() != null && column.getSequenceName().length() > 0) {
                ifNodes.add(getIfIsNull(column, new StaticTextSqlNode(getSeqNextVal(column) + " ,")));
            } else if (column.isIdentity()) {
                ifNodes.add(getIfCacheIsNull(column, new StaticTextSqlNode("#{" + column.getProperty() + " },")));
            } else if (column.isUuid()) {
                ifNodes.add(getIfIsNull(column, new StaticTextSqlNode("#{" + column.getProperty() + "_bind },")));
            } else {
                //null?jdbcTypeoracle?VARCHAR??
                ifNodes.add(getIfIsNull(column,
                        new StaticTextSqlNode("#{" + column.getProperty() + ",jdbcType=VARCHAR},")));
            }
        }
        //values(#{property},#{property}...)
        sqlNodes.add(new TrimSqlNode(ms.getConfiguration(), new MixedSqlNode(ifNodes), "VALUES (", null, ")", ","));
        return new MixedSqlNode(sqlNodes);
    }

    /**
     * ??null
     *
     * @param ms
     * @return
     */
    public SqlNode insertSelective(MappedStatement ms) {
        Class<?> entityClass = getSelectReturnType(ms);
        List<SqlNode> sqlNodes = new ArrayList<SqlNode>();
        //insert into table
        sqlNodes.add(new StaticTextSqlNode("INSERT INTO " + tableName(entityClass)));
        //?
        List<EntityHelper.EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        List<SqlNode> ifNodes = new ArrayList<SqlNode>();
        //Identity?
        Boolean hasIdentityKey = false;
        //??????
        for (EntityHelper.EntityColumn column : columnList) {
            //?
            if (column.getSequenceName() != null && column.getSequenceName().length() > 0) {
                //
                ifNodes.add(new StaticTextSqlNode(column.getColumn() + ","));
            } else if (column.isIdentity()) {
                if (hasIdentityKey) {
                    throw new RuntimeException(ms.getId() + "" + entityClass.getCanonicalName()
                            + "?MySql,?!");
                }
                //selectKey-MS
                newSelectKeyMappedStatement(ms, column);
                hasIdentityKey = true;
                //
                ifNodes.add(new StaticTextSqlNode(column.getColumn() + ","));
                //?,,??,?
                sqlNodes.add(new VarDeclSqlNode(column.getProperty() + "_cache", column.getProperty()));
            } else if (column.isUuid()) {
                //UUIDbind
                sqlNodes.add(new VarDeclSqlNode(column.getProperty() + "_bind", getUUID()));
                ifNodes.add(new StaticTextSqlNode(column.getColumn() + ","));
            } else {
                ifNodes.add(getIfNotNull(column, new StaticTextSqlNode(column.getColumn() + ",")));
            }
        }
        //?sqlNodes
        sqlNodes.add(new TrimSqlNode(ms.getConfiguration(), new MixedSqlNode(ifNodes), "(", null, ")", ","));

        ifNodes = new ArrayList<SqlNode>();
        //?values(#{property},#{property}...)
        for (EntityHelper.EntityColumn column : columnList) {
            //??,
            //,,property_cache
            if (column.isIdentity()) {
                ifNodes.add(new IfSqlNode(new StaticTextSqlNode("#{" + column.getProperty() + "_cache },"),
                        column.getProperty() + "_cache != null "));
            } else {
                ifNodes.add(new IfSqlNode(new StaticTextSqlNode("#{" + column.getProperty() + "},"),
                        column.getProperty() + " != null "));
            }
            if (column.getSequenceName() != null && column.getSequenceName().length() > 0) {
                ifNodes.add(getIfIsNull(column, new StaticTextSqlNode(getSeqNextVal(column) + " ,")));
            } else if (column.isIdentity()) {
                ifNodes.add(getIfCacheIsNull(column, new StaticTextSqlNode("#{" + column.getProperty() + " },")));
            } else if (column.isUuid()) {
                ifNodes.add(getIfIsNull(column, new StaticTextSqlNode("#{" + column.getProperty() + "_bind },")));
            }
        }
        //values(#{property},#{property}...)
        sqlNodes.add(new TrimSqlNode(ms.getConfiguration(), new MixedSqlNode(ifNodes), "VALUES (", null, ")", ","));
        return new MixedSqlNode(sqlNodes);
    }

    /**
     * ?
     *
     * @param ms
     * @return
     */
    public SqlNode delete(MappedStatement ms) {
        Class<?> entityClass = getSelectReturnType(ms);
        List<SqlNode> sqlNodes = new ArrayList<SqlNode>();
        //delete from table
        sqlNodes.add(new StaticTextSqlNode("DELETE FROM " + tableName(entityClass)));
        //where/if?
        sqlNodes.add(new WhereSqlNode(ms.getConfiguration(), getAllIfColumnNode(entityClass)));
        return new MixedSqlNode(sqlNodes);
    }

    /**
     * 
     *
     * @param ms
     */
    public void deleteByPrimaryKey(MappedStatement ms) {
        final Class<?> entityClass = getSelectReturnType(ms);
        List<ParameterMapping> parameterMappings = getPrimaryKeyParameterMappings(ms);
        //sql
        String sql = new SQL() {
            {
                //delete from table
                DELETE_FROM(tableName(entityClass));
                //where =#{property} ?
                WHERE(EntityHelper.getPrimaryKeyWhere(entityClass));
            }
        }.toString();
        //??SqlSource
        StaticSqlSource sqlSource = new StaticSqlSource(ms.getConfiguration(), sql, parameterMappings);
        //?SqlSource
        setSqlSource(ms, sqlSource);
    }

    /**
     * 
     *
     * @param ms
     */
    public void updateByPrimaryKey(MappedStatement ms) {
        final Class<?> entityClass = getSelectReturnType(ms);
        //??set=?where=?
        //?set
        List<ParameterMapping> parameterMappings = getColumnParameterMappings(ms);
        //?where
        parameterMappings.addAll(getPrimaryKeyParameterMappings(ms));
        //sql
        String sql = new SQL() {
            {
                //update table
                UPDATE(tableName(entityClass));
                //?
                List<EntityHelper.EntityColumn> columnList = EntityHelper.getColumns(entityClass);
                //set column = ?
                for (EntityHelper.EntityColumn column : columnList) {
                    SET(column.getColumn() + " = ?");
                }
                //where =#{property} ?
                WHERE(EntityHelper.getPrimaryKeyWhere(entityClass));
            }
        }.toString();
        //??SqlSource
        StaticSqlSource sqlSource = new StaticSqlSource(ms.getConfiguration(), sql, parameterMappings);
        //?SqlSource
        setSqlSource(ms, sqlSource);
    }

    /**
     * ?null
     *
     * @param ms
     * @return
     */
    public SqlNode updateByPrimaryKeySelective(MappedStatement ms) {
        Class<?> entityClass = getSelectReturnType(ms);
        List<SqlNode> sqlNodes = new ArrayList<SqlNode>();
        //update table
        sqlNodes.add(new StaticTextSqlNode("UPDATE " + tableName(entityClass)));
        //?
        List<EntityHelper.EntityColumn> columnList = EntityHelper.getColumns(entityClass);
        List<SqlNode> ifNodes = new ArrayList<SqlNode>();
        //if property!=null and property!=''
        for (EntityHelper.EntityColumn column : columnList) {
            StaticTextSqlNode columnNode = new StaticTextSqlNode(
                    column.getColumn() + " = #{" + column.getProperty() + "}, ");
            ifNodes.add(getIfNotNull(column, columnNode));
        }
        sqlNodes.add(new SetSqlNode(ms.getConfiguration(), new MixedSqlNode(ifNodes)));
        //?
        columnList = EntityHelper.getPKColumns(entityClass);
        List<SqlNode> whereNodes = new ArrayList<SqlNode>();
        boolean first = true;
        //where =#{property} ?
        for (EntityHelper.EntityColumn column : columnList) {
            whereNodes.add(getColumnEqualsProperty(column, first));
            first = false;
        }
        sqlNodes.add(new WhereSqlNode(ms.getConfiguration(), new MixedSqlNode(whereNodes)));
        return new MixedSqlNode(sqlNodes);
    }
}