mybatis 中 #{}带来的困惑

先看 xml 结构



<mapper namespace="cn.zdfy.blogsystem.dao.UserMapper">
    <select id="queryUserByName" resultType="User" parameterType="String">
        SELECT * FROM user WHERE username =#{name}
    select>
mapper>

先看一下 http://localhost:9290/test?test=“root” 访问,打断点看一下
mybatis 中 #{}带来的困惑_第1张图片
发现 字符串变成了 “”test”” 导致sql执行时 查不到数据 看sql 日志

2018-04-02 20:28:50.235  WARN 2132 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=2m14s869ms809µs814ns).
2018-04-02 20:28:50.236 DEBUG 2132 --- [io-9290-exec-10] c.z.b.dao.UserMapper.queryUserByName     : ==>  Preparing: SELECT * FROM user WHERE username =? 
2018-04-02 20:28:50.236 DEBUG 2132 --- [io-9290-exec-10] c.z.b.dao.UserMapper.queryUserByName     : ==> Parameters: "root"(String)
2018-04-02 20:28:50.237 DEBUG 2132 --- [io-9290-exec-10] c.z.b.dao.UserMapper.queryUserByName     : <==      Total: 0

再看 http://localhost:9290/test?test=root 访问 断点一下
mybatis 中 #{}带来的困惑_第2张图片
再看一下 sql日志

2018-04-02 20:30:42.955  WARN 2132 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=1m22s717ms242µs766ns).
2018-04-02 20:30:42.955 DEBUG 2132 --- [nio-9290-exec-2] c.z.b.dao.UserMapper.queryUserByName     : ==>  Preparing: SELECT * FROM user WHERE username =? 
2018-04-02 20:30:42.956 DEBUG 2132 --- [nio-9290-exec-2] c.z.b.dao.UserMapper.queryUserByName     : ==> Parameters: root(String)
2018-04-02 20:30:42.957 DEBUG 2132 --- [nio-9290-exec-2] c.z.b.dao.UserMapper.queryUserByName     : <==      Total: 1

总结 #{} 由于防注入 会自动加上 “”,导致字符串有多了引号,不注意还真能浪费半天时间。

你可能感兴趣的:(mybatis,疑难杂症)