All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zhangzlyuyx.easy.mybatis.mapper.JoinExampleProvider Maven / Gradle / Ivy

package com.zhangzlyuyx.easy.mybatis.mapper;

import java.util.Set;

import org.apache.ibatis.mapping.MappedStatement;

import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

/**
 * JoinExampleProvider
 * @author zhangzlyuyx
 *
 */
public class JoinExampleProvider extends MapperTemplate {
	
	public static final String method_selectByJoinExample = "selectByJoinExample";
	
	public static final String method_selectByJoinExampleAndRowBounds = "selectByJoinExampleAndRowBounds";
	
	public static final String method_selectMapByJoinExample = "selectMapByJoinExample";
	
	public static final String method_selectMapByJoinExampleAndRowBounds = "selectMapByJoinExampleAndRowBounds";
	
	public static final String method_selectCountByJoinExample = "selectCountByJoinExample";

	public JoinExampleProvider(Class mapperClass, MapperHelper mapperHelper) {
		super(mapperClass, mapperHelper);
	}
    
    /**
     * 根据JoinExample查询
     *
     * @param ms
     * @return
     */
    public String selectByJoinExample(MappedStatement ms) {
        Class entityClass = getEntityClass(ms);
        //将返回值修改为实体类型
        setResultType(ms, entityClass);
        StringBuilder sql = new StringBuilder("SELECT ");
        if (isCheckExampleEntityClass()) {
            sql.append(SqlHelper.exampleCheck(entityClass));
        }
        sql.append("distinct");
        //支持查询指定列
        sql.append(exampleSelectColumns(entityClass));
        sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
        sql.append(exampeJoinOn());
        sql.append(exampleWhereClause(entityClass));
        sql.append(exampleGroupBy(entityClass));
        sql.append(exampleOrderBy(entityClass));
        sql.append(SqlHelper.exampleForUpdate());
        return sql.toString();
    }
    
    /**
     * 根据JoinExample查询
     * @param ms
     * @return
     */
    public String selectByJoinExampleAndRowBounds(MappedStatement ms) {
    	return this.selectByJoinExample(ms);
    }
    
    /**
     * 根据JoinExample查询
     *
     * @param ms
     * @return
     */
    public String selectMapByJoinExample(MappedStatement ms) {
        Class entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder("SELECT ");
        if (isCheckExampleEntityClass()) {
            sql.append(SqlHelper.exampleCheck(entityClass));
        }
        sql.append("distinct");
        //支持查询指定列
        sql.append(exampleSelectColumns(entityClass));
        sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
        sql.append(exampeJoinOn());
        sql.append(exampleWhereClause(entityClass));
        sql.append(exampleGroupBy(entityClass));
        sql.append(exampleOrderBy(entityClass));
        sql.append(SqlHelper.exampleForUpdate());
        return sql.toString();
    }
    
    /**
     * 根据JoinExample查询
     * @param ms
     * @return
     */
    public String selectMapByJoinExampleAndRowBounds(MappedStatement ms) {
    	return this.selectMapByJoinExample(ms);
    }
    
    /**
     * 根据Example查询总数
     *
     * @param ms
     * @return
     */
    public String selectCountByJoinExample(MappedStatement ms) {
        Class entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder("SELECT ");
        if (isCheckExampleEntityClass()) {
            sql.append(SqlHelper.exampleCheck(entityClass));
        }
        sql.append(SqlHelper.exampleCountColumn(entityClass));
        sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
        sql.append(exampeJoinOn());
        sql.append(exampleWhereClause(entityClass));
        sql.append(exampleGroupBy(entityClass));
        sql.append(SqlHelper.exampleForUpdate());
        return sql.toString();
    }
    
    /**
     * example支持查询指定列时
     *
     * @return
     */
    private String exampleSelectColumns(Class entityClass) {
    	String tableName = SqlHelper.getDynamicTableName(entityClass, tableName(entityClass));
        StringBuilder sql = new StringBuilder();
        sql.append("");
        sql.append("  ");
        sql.append("    ");
        sql.append("      " + tableName + "." + "${selectColumn}");
        sql.append("    ");
        sql.append("  ");
        sql.append("  ");
        sql.append("    " + getAllColumns(entityClass));
        sql.append("  ");
        sql.append("");
        return sql.toString();
    }
    
    /**
     * 获取所有查询列,如id,name,code...
     *
     * @param entityClass
     * @return
     */
    private String getAllColumns(Class entityClass) {
    	String tableName = SqlHelper.getDynamicTableName(entityClass, tableName(entityClass));
        Set columnSet = EntityHelper.getColumns(entityClass);
        StringBuilder sql = new StringBuilder();
        for (EntityColumn entityColumn : columnSet) {
            sql.append("" + tableName + "." + entityColumn.getColumn()).append(",");
        }
        return sql.substring(0, sql.length() - 1);
    }
    
    /**
     * 获取 where 语句 xml
     *
     * @return
     */
    private String exampleWhereClause(Class entityClass) {
    	String tableName = SqlHelper.getDynamicTableName(entityClass, tableName(entityClass));
        return "" +
                "\n" +
                " ${@tk.mybatis.mapper.util.OGNL@andNotLogicDelete(_parameter)}" +
                " \n" +
                "  \n" +
                "    \n" +
                "      ${@tk.mybatis.mapper.util.OGNL@andOr(criteria)}" +
                "      \n" +
                "        \n" +
                "          \n" +
                "            \n" +
                "              ${@tk.mybatis.mapper.util.OGNL@andOr(criterion)} ${criterion.condition}\n" +
                "            \n" +
                "            \n" +
                "              ${@tk.mybatis.mapper.util.OGNL@andOr(criterion)} " + "" + tableName + "." + "${criterion.condition} #{criterion.value}\n" +
                "            \n" +
                "            \n" +
                "              ${@tk.mybatis.mapper.util.OGNL@andOr(criterion)} " + "" + tableName + "." + "${criterion.condition} #{criterion.value} and #{criterion.secondValue}\n" +
                "            \n" +
                "            \n" +
                "              ${@tk.mybatis.mapper.util.OGNL@andOr(criterion)} " + "" + tableName + "." + "${criterion.condition}\n" +
                "              \n" +
                "                #{listItem}\n" +
                "              \n" +
                "            \n" +
                "          \n" +
                "        \n" +
                "      \n" +
                "    \n" +
                "  \n" +
                " \n" +
                "" +
                "";
    }
    
    /**
     * 获取 order by xml
     * @param entityClass
     * @return
     */
    private String exampleOrderBy(Class entityClass) {
    	String tableName = SqlHelper.getDynamicTableName(entityClass, tableName(entityClass));
        StringBuilder sql = new StringBuilder();
        sql.append("");
        sql.append("  ORDER BY " + "" + tableName + "." + "${_parameter.orderByClause}");
        sql.append("");
        String orderByClause = EntityHelper.getOrderByClause(entityClass);
        if (orderByClause != null && orderByClause.length() > 0) {
            sql.append("");
            sql.append("  ORDER BY " + "" + tableName + "." + orderByClause);
            sql.append("");
        }
        return sql.toString();
    }
    
    /**
     * 获取 join on xml
     * @return
     */
    private String exampeJoinOn() {
    	StringBuilder sql = new StringBuilder();
    	//join
        sql.append("");
        sql.append("  ");
        sql.append("    ${joinPart.joinConnect} ${joinPart.joinTable} ON ${joinPart.joinOnClause} ");
        sql.append("  ");
        sql.append("");
        return sql.toString();
    }
    
    /**
     * 获取 group by 语句
     * @param entityClass
     * @return
     */
    private String exampleGroupBy(Class entityClass) {
    	String tableName = SqlHelper.getDynamicTableName(entityClass, tableName(entityClass));
    	StringBuilder sql = new StringBuilder();
    	//group by
        sql.append("");
        sql.append("  GROUP BY " + "" + tableName + "." + "${_parameter.groupByClause}");
        sql.append("");
        return sql.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy