|
| 1 | +package com.vonchange.jdbc.mybatis; |
| 2 | + |
| 3 | +import com.vonchange.common.util.StringPool; |
| 4 | +import com.vonchange.jdbc.model.SqlParam; |
| 5 | +import com.vonchange.jdbc.util.JdbcUtil; |
| 6 | +import com.vonchange.jdbc.util.MybatisTpl; |
| 7 | +import com.vonchange.mybatis.dialect.Dialect; |
| 8 | +import org.springframework.jdbc.core.JdbcOperations; |
| 9 | +import org.springframework.jdbc.core.PreparedStatementCreator; |
| 10 | +import org.springframework.jdbc.core.PreparedStatementCreatorFactory; |
| 11 | +import org.springframework.jdbc.core.SqlParameter; |
| 12 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
| 13 | +import org.springframework.jdbc.core.namedparam.NamedParameterUtils; |
| 14 | +import org.springframework.jdbc.core.namedparam.ParsedSql; |
| 15 | +import org.springframework.jdbc.core.namedparam.SqlParameterSource; |
| 16 | +import org.springframework.jdbc.support.JdbcUtils; |
| 17 | +import org.springframework.lang.Nullable; |
| 18 | + |
| 19 | +import javax.sql.DataSource; |
| 20 | +import java.sql.DatabaseMetaData; |
| 21 | +import java.sql.SQLException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Locale; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.function.Consumer; |
| 29 | + |
| 30 | +public abstract class MybatisJdbcTemplate extends NamedParameterJdbcTemplate { |
| 31 | + public MybatisJdbcTemplate(DataSource dataSource) { |
| 32 | + super(dataSource); |
| 33 | + DatabaseMetaData metaData = null; |
| 34 | + try { |
| 35 | + metaData = dataSource.getConnection().getMetaData(); |
| 36 | + String name = metaData.getDatabaseProductName().toLowerCase(Locale.ENGLISH); |
| 37 | + //h2 |
| 38 | + System.out.println("XXXX::"+name); |
| 39 | + } catch (SQLException e) { |
| 40 | + throw new RuntimeException(e); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public MybatisJdbcTemplate(JdbcOperations classicJdbcTemplate) { |
| 45 | + super(classicJdbcTemplate); |
| 46 | + } |
| 47 | + |
| 48 | + protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource, |
| 49 | + @Nullable Consumer<PreparedStatementCreatorFactory> customizer) { |
| 50 | + Map<String,Object> param =toMap(paramSource); |
| 51 | + SqlParam sqlParam = genSqlParam(sql,param); |
| 52 | + if(null==sqlParam){ |
| 53 | + return getPreparedStatementCreatorOriginal(sql,paramSource,customizer); |
| 54 | + } |
| 55 | + List<SqlParameter> declaredParameters = buildSqlParameterList(sqlParam.getParams()); |
| 56 | + PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlParam.getSql(),declaredParameters); |
| 57 | + if (customizer != null) { |
| 58 | + customizer.accept(pscf); |
| 59 | + } |
| 60 | + Object[] paramObj=sqlParam.getParams().toArray(); |
| 61 | + return pscf.newPreparedStatementCreator(paramObj); |
| 62 | + } |
| 63 | + protected PreparedStatementCreator getPreparedStatementCreatorOriginal(String sql, SqlParameterSource paramSource, |
| 64 | + @Nullable Consumer<PreparedStatementCreatorFactory> customizer) { |
| 65 | + |
| 66 | + ParsedSql parsedSql = getParsedSql(sql); |
| 67 | + PreparedStatementCreatorFactory pscf = getPreparedStatementCreatorFactory(parsedSql, paramSource); |
| 68 | + if (customizer != null) { |
| 69 | + customizer.accept(pscf); |
| 70 | + } |
| 71 | + Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null); |
| 72 | + return pscf.newPreparedStatementCreator(params); |
| 73 | + } |
| 74 | + protected abstract Dialect dialect(); |
| 75 | + private SqlParam genSqlParam(String sql,Map<String,Object> param){ |
| 76 | + if(!sql.contains(StringPool.SPACE)){ |
| 77 | + return MybatisTpl.generate("sql."+sql,param,dialect()); |
| 78 | + } |
| 79 | + if(sql.contains("[@")){//#{ spel 也有的 ||sql.contains("#{" |
| 80 | + return MybatisTpl.generate(sql,sql,param,dialect()); |
| 81 | + } |
| 82 | + if(sql.contains("#{")){ |
| 83 | + if(!sql.contains(":#{")){ |
| 84 | + return MybatisTpl.generate(sql,sql,param,dialect()); |
| 85 | + } |
| 86 | + } |
| 87 | + return null; |
| 88 | + } |
| 89 | + public static List<SqlParameter> buildSqlParameterList(Collection<?> param) { |
| 90 | + List<SqlParameter> params = new ArrayList<>(param.size()); |
| 91 | + for (Object value : param) { |
| 92 | + if(null==value){ |
| 93 | + params.add(new SqlParameter(JdbcUtils.TYPE_UNKNOWN)); |
| 94 | + continue; |
| 95 | + } |
| 96 | + params.add(new SqlParameter(JdbcUtil.sqlTypeFor(value.getClass()))); |
| 97 | + } |
| 98 | + return params; |
| 99 | + } |
| 100 | + public static List<SqlParameter> buildSqlParameterListX(SqlParameterSource paramSource) { |
| 101 | + String[] paramNames = paramSource.getParameterNames(); |
| 102 | + if (paramNames == null) { |
| 103 | + return new ArrayList<>(); |
| 104 | + } |
| 105 | + List<SqlParameter> params = new ArrayList<>(paramNames.length); |
| 106 | + for (String paramName : paramNames) { |
| 107 | + params.add(new SqlParameter( |
| 108 | + paramName, paramSource.getSqlType(paramName), paramSource.getTypeName(paramName))); |
| 109 | + } |
| 110 | + return params; |
| 111 | + } |
| 112 | + |
| 113 | + private Map<String,Object> toMap( SqlParameterSource paramSource){ |
| 114 | + String[] parameterNames = paramSource.getParameterNames(); |
| 115 | + Map<String,Object> map = new HashMap<>(); |
| 116 | + if(null==parameterNames){ |
| 117 | + return map; |
| 118 | + } |
| 119 | + for (String paramName : parameterNames) { |
| 120 | + map.put(paramName,paramSource.getValue(paramName)); |
| 121 | + } |
| 122 | + return map; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +} |
0 commit comments