设置SQL语句参数值:列索引1,参数值[名称],值类[java.lang.String],SQL类型未知

时间:2018-03-07 07:16:01

标签: java spring log4j spring-jdbc

我使用NamedParameterJdbcTemplate将参数传递给Query。但我得到跟踪细节作为一个警告和参数绑定没有显示。主要问题是它是显示执行sql但我想参数也只是它的一部分?(问号)来了。警告绑定发生后执行是好的但是在日志中看不到绑定。请帮我检查一下。

  

列索引1,参数值[mike],值类   [java.lang.String],SQL类型未知   列索引2,参数值   [1],值类[java.lang.Integer],SQL类型未知

存储库:

@Repository
public class DataRepository {
    private static Logger log = LoggerFactory.getLogger(DataRepository.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    public Data findDataObjet() throws Exception {

        Map<String, Object> parameters = new HashMap<>();
        parameters.put("id1", "mike");
        parameters.put("id2", new Long(1));

        String sqlString = "select * from table1 where id1 = ":id1" and id2 = :id2";

        Data extObj = jdbcTemplate.query(sqlString, parameters, (rs) -> {
            if (rs != null && rs.next()) {
                Data innerObj = new Data();
                innerObj.setName(rs.getString("name"));             
                return innerObj;
            } else {
                log.info("No records found:"+rs);
                return null;
            }
        });

        return extObj;

    }
}

application.properties:

spring.datasource.url=url
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

spring.datasource.type = com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=2

的logback-spring.xml:

<springProfile name="dev">
        <logger name="org.springframework.jdbc.core.JdbcTemplate">
            <level value="trace" />
        </logger>
        <logger name="org.springframework.jdbc.core.StatementCreatorUtils">
            <level value="trace" />
        </logger>
        <appender name="dailyRollingFileAppender"
            class="ch.qos.logback.core.rolling.RollingFileAppender">
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${logsPath}TSYS2DM%d{MMddyyyy}.log
                </FileNamePattern>
                <maxHistory>4</maxHistory>
            </rollingPolicy>

            <encoder>
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level
                    %logger{35}-%msg %n</Pattern>
            </encoder>

        </appender>
        <root level="INFO">
            <appender-ref ref="dailyRollingFileAppender" />
        </root>
    </springProfile>

记录跟踪:

2018-03-07 12:19:42.888 DEBUG 30224 --- [resources/input] o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL query
2018-03-07 12:19:42.889 DEBUG 30224 --- [resources/input] o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL statement [ select * from table1 where id1 = "?" and id2 = ?;]
2018-03-07 12:19:43.132 TRACE 30224 --- [resources/input] o.s.jdbc.core.StatementCreatorUtils      : Setting SQL statement parameter value: column index 1, parameter value [mike], value class [java.lang.String], SQL type unknown
2018-03-07 12:19:43.132 TRACE 30224 --- [resources/input] o.s.jdbc.core.StatementCreatorUtils      : Setting SQL statement parameter value: column index 2, parameter value [1], value class [java.lang.Integer], SQL type unknown
2018-03-07 12:19:43.439  INFO 30224 --- [resources/input] c.t.f.c.t.t.db.TRMAccountDataRepository  : found record with cnsmr_id:1234

1 个答案:

答案 0 :(得分:0)

使用Wrapper类SqlParameterValue,通知将消失:

    Map<String, Object> parameters = new HashMap<>();
    parameters.put("id1", new SqlParameterValue(Types.VARCHAR,"mike"));
    parameters.put("id2", new SqlParameterValue(Types.BIGINT,new Long(1)));

在日志文件中,您将看到SQL类型91 ...,而不是未知的SQL类型