esolve
V2EX  ›  问与答

mybatis 里如何连接字符串?

  •  
  •   esolve · Sep 26, 2017 · 9672 views
    This topic created in 3327 days ago, the information mentioned may be changed or developed.

    像是下面的 table_name='user_'#{groupId}

    连接是错误的

    譬如如果 id 是 132,应该是变成了'user_'132,而不是期望的'user_132'

     <select id="tableExists" parameterType="String" resultType="Integer">
        SELECT COUNT(*)
        FROM information_schema.tables
        WHERE table_schema='api_db'
        AND table_name='user_'#{groupId}
     </select>
    
    5 replies  •  2017-09-26 16:10:16 +08:00
    Aegwynn
    Aegwynn
    Sep 26, 2017
    1
    oracle:'user_'||#{groupId}
    mysql:concat('user_',#{groupId})

    顺便吐槽 lz,你这什么 jb 用法,拼接放到上层语言不行么?
    LichMscy
    LichMscy
    Sep 26, 2017
    2
    一楼正解

    传 parameter 的时候直接拼上'user_',减少在 SQL 中拼接,不然就用 concat,建议掌握下 mybatis 的原理
    novaeye
    novaeye
    Sep 26, 2017
    3
    "#"符号换成"$"符号.
    参考: http://www.jianshu.com/p/b3b4a4fb8a54
    hcymk2
    hcymk2
    Sep 26, 2017
    4
    用 getMetaData().getTables() 来判断表是否存在吧。
    esolve
    esolve
    OP
    Sep 26, 2017
    5
    @Aegwynn 分表了啊,如果在 java 层的话,那就要在业务层拼接了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2898 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 12:12 · PVG 20:12 · LAX 04:12 · JFK 07:12
    ♥ Do have faith in what you're doing.