MyBatis-常用

MyBatis 设置Sql完整打印
将改类创建在项目下

点击查看代码
package org.apache.ibatis.logging.jdbc;

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.reflection.ArrayUtil;

import java.lang.reflect.Method;
import java.sql.Array;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

public class BaseJdbcLogger {
    protected static final Set<String> SET_METHODS = (Set) Arrays.stream(PreparedStatement.class.getDeclaredMethods()).filter((method) -> {
        return method.getName().startsWith("set");
    }).filter((method) -> {
        return method.getParameterCount() > 1;
    }).map(Method::getName).collect(Collectors.toSet());
    protected static final Set<String> EXECUTE_METHODS = new HashSet();
    private final Map<Object, Object> columnMap = new HashMap();
    private final List<Object> columnNames = new ArrayList();
    private final List<Object> columnValues = new ArrayList();
    protected final Log statementLog;
    protected final int queryStack;

    public BaseJdbcLogger(Log log, int queryStack) {
        this.statementLog = log;
        if (queryStack == 0) {
            this.queryStack = 1;
        } else {
            this.queryStack = queryStack;
        }

    }

    protected void setColumn(Object key, Object value) {
        this.columnMap.put(key, value);
        this.columnNames.add(key);
        this.columnValues.add(value);
    }

    protected Object getColumn(Object key) {
        return this.columnMap.get(key);
    }

    protected String getParameterValueString() {
        List<Object> typeList = new ArrayList(this.columnValues.size());
        Iterator var2 = this.columnValues.iterator();

        while(var2.hasNext()) {
            Object value = var2.next();
            if (value == null) {
                typeList.add("null");
            } else {
                typeList.add(this.objectValueString(value) + "(" + value.getClass().getSimpleName() + ")");
            }
        }

        String parameters = typeList.toString();
        return parameters.substring(1, parameters.length() - 1);
    }

    protected String objectValueString(Object value) {
        if (value instanceof Array) {
            try {
                return ArrayUtil.toString(((Array)value).getArray());
            } catch (SQLException var3) {
                return value.toString();
            }
        } else {
            return value.toString();
        }
    }

    protected String getColumnString() {
        return this.columnNames.toString();
    }

    protected void clearColumnInfo() {
        this.columnMap.clear();
        this.columnNames.clear();
        this.columnValues.clear();
    }

    protected String removeBreakingWhitespace(String original) {
        StringTokenizer whitespaceStripper = new StringTokenizer(original);
        StringBuilder builder = new StringBuilder();

        while(whitespaceStripper.hasMoreTokens()) {
            builder.append(whitespaceStripper.nextToken());
            builder.append(" ");
        }

        return builder.toString();
    }

    protected boolean isDebugEnabled() {
        return this.statementLog.isDebugEnabled();
    }

    protected boolean isTraceEnabled() {
        return this.statementLog.isTraceEnabled();
    }

    private static String sql = "";
    private static String origin = "";


    protected synchronized void debug(String text, boolean input) {
        text = text.trim();
        if (statementLog.isDebugEnabled()) {
            if (text.startsWith("Preparing:")) {
                sql = text.substring(text.indexOf(":") + 1);
                statementLog.debug(prefix(input) + text);
                return;
            }
            if (text.startsWith("Parameters:")) {
                String temp = text.substring(text.indexOf(":") + 1);
                if (!"".equals(temp)) {
                    String[] split = temp.split(",");
                    if (split != null & split.length > 0) {
                        for (String string2 : split) {
                            String s = string2.trim();
                            String parameters = s.substring(0, s.indexOf("(")) ;
                            String type = s.substring(s.indexOf("(")+1, s.indexOf(")")) ;
                            if("String".equals(type)||"Timestamp".equals(type)){
                                s = "'"+parameters + "'";
                            }else {
                                s=parameters;
                            }
                            sql = sql.replaceFirst("\\?", s);
                        }
                    }
                }
                text = text +"\n"+"=============================================================================="+
                             "\n"+"org.originSQL.mybatis2mysqlLogChange:  " + "\n"+sql+"\n"+
                              "=============================================================================="+
                        "\n";
                sql = "";
            }
            statementLog.debug(prefix(input) + text);
        }
    }

        protected void trace(String text, boolean input) {
        if (this.statementLog.isTraceEnabled()) {
            this.statementLog.trace(this.prefix(input) + text);
        }

    }

    private String prefix(boolean isInput) {
        char[] buffer = new char[this.queryStack * 2 + 2];
        Arrays.fill(buffer, '=');
        buffer[this.queryStack * 2 + 1] = ' ';
        if (isInput) {
            buffer[this.queryStack * 2] = '>';
        } else {
            buffer[0] = '<';
        }

        return new String(buffer);
    }

    static {
        EXECUTE_METHODS.add("execute");
        EXECUTE_METHODS.add("executeUpdate");
        EXECUTE_METHODS.add("executeQuery");
        EXECUTE_METHODS.add("addBatch");
    }
}

MyBatis

查询日期范围 大于小于

 <select id="deductionByImport" resultType="com.tianque.nt.jsgrid.domain.deduction.DeductionEntity">
        select * from  dedutionImpoty where userId=#{userId}
         and createdate &gt;= to_date(#{startTime},'yyyy/mm/dd hh24:mi:ss')
         and createdate &lt; to_date(#{endTime},'yyyy/mm/dd hh24:mi:ss')
 </select>

查询当月范围,参数yyyyMM

select count(id) from serviceRecords
            where datastate != 2
            and orgCode like  #{orgCode} || '%'
            and createdate <![CDATA[>=]]>  trunc(to_date(#{dataMonth},'yyyyMM'),'MM')
            and createdate <![CDATA[<]]>  trunc(add_months(to_date(#{dataMonth},'yyyyMM'),'1'),'MM')
posted @ 2022-05-30 17:20  csj425  阅读(19)  评论(0)    收藏  举报