V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zhady009
V2EX  ›  Java

mybatis 中如何处理插入语句中的美元符号 等特殊字符?

  •  
  •   zhady009 · 2019-05-21 19:50:48 +08:00 via iPhone · 3667 次点击
    这是一个创建于 2014 天前的主题,其中的信息可能已经有所发展或是发生改变。
    CDATA 试了几种写法发现都不管用
    11 条回复    2019-05-22 10:41:02 +08:00
    gejun123456
        1
    gejun123456  
       2019-05-21 21:48:32 +08:00
    sql 看看,可以试试 bind 标签
    <bind name="hello" value="$">
    select * from where id = #{hello}
    Belmode
        2
    Belmode  
       2019-05-21 22:15:18 +08:00
    html 符号实体 $
    ```
    &#36;
    ```
    zhady009
        3
    zhady009  
    OP
       2019-05-21 22:19:45 +08:00 via iPhone
    @gejun123456 就是用你插件生成的 insert 买了一年
    zhady009
        4
    zhady009  
    OP
       2019-05-21 22:24:58 +08:00 via iPhone
    @gejun123456 就比如一个 username 字段 value 是 x$x
    gejun123456
        5
    gejun123456  
       2019-05-21 22:56:43 +08:00
    @zhady009 #4 我试了下没发现问题。
    java 代码:
    User record = new User();
    record.setUserName("x$x");
    mapper.insertSelective(record);

    xml:
    <insert id="insertSelective" keyColumn="user_id" keyProperty="userId" parameterType="com.codehelper.domain.User" useGeneratedKeys="true">
    <[email protected]>
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="userName != null">
    user_name,
    </if>
    <if test="birth != null">
    birth,
    </if>
    <if test="salary != null">
    salary,
    </if>
    <if test="ssssssss != null">
    ssssssss,
    </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
    <if test="userName != null">
    #{userName},
    </if>
    <if test="birth != null">
    #{birth},
    </if>
    <if test="salary != null">
    #{salary},
    </if>
    <if test="ssssssss != null">
    #{ssssssss},
    </if>
    </trim>

    插入没问题,另外你这个是报啥错哈
    zhady009
        6
    zhady009  
    OP
       2019-05-22 09:56:11 +08:00
    @gejun123456 mybtis 版本 3.4.6
    java:
    AdPlanRtReport adPlanRtReport = new AdPlanRtReport();
    adPlanRtReport.setUserId(0L);
    adPlanRtReport.setPlanId(0L);
    adPlanRtReport.setPlanName("212$");
    adPlanRtReportMapper.insertSelective(adPlanRtReport);
    ...省略
    xml:
    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="xxx.AdPlanRtReport" useGeneratedKeys="true">
    <[email protected]>
    insert into ad_plan_rt_report
    <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="userId != null">
    user_id,
    </if>
    <if test="planId != null">
    plan_id,
    </if>
    <if test="planName != null">
    plan_name,
    </if>
    ...省略
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
    <if test="userId != null">
    #{userId,jdbcType=BIGINT},
    </if>
    <if test="planId != null">
    #{planId,jdbcType=BIGINT},
    </if>
    <if test="planName != null">
    #{planName,jdbcType=VARCHAR},
    </if>
    ...省略
    </trim>
    </insert>

    报错信息:
    Error updating database. Cause: java.lang.IllegalArgumentException: Illegal group reference
    ### The error may involve xxx.mapper.AdPlanRtReportMapper.insertSelective-Inline
    ### The error occurred while setting parameters
    ### SQL: insert into ad_plan_rt_report ( user_id, plan_id, plan_name, cpm, roi, impression, click, ctr, spend, cpc, order_num, gmv, scene_type, ad_unit_num, mall_fav_num, max_cost, account_status, `status`, operate_status, is_deleted, report_date, report_time ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
    ### Cause: java.lang.IllegalArgumentException: Illegal group reference
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
    at com.sun.proxy.$Proxy35.insert(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:278)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:58)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
    at com.sun.proxy.$Proxy58.insertSelective(Unknown Source)
    zhady009
        7
    zhady009  
    OP
       2019-05-22 10:05:29 +08:00
    数据是写进去了..不太理解
    gejun123456
        8
    gejun123456  
       2019-05-22 10:15:48 +08:00
    @zhady009 #7 用了啥 mybatis 插件么,我这边不会出现这个问题的
    gejun123456
        9
    gejun123456  
       2019-05-22 10:16:39 +08:00
    @zhady009 #7 看看 有没有插件用了 replaceAll 啥的 https://jerval.iteye.com/blog/2164227
    gejun123456
        10
    gejun123456  
       2019-05-22 10:18:14 +08:00   ❤️ 1
    https://github.com/abel533/Mapper/issues/30

    可以参考这个帖子,也是用了一个 mybatis 的插件 报的错
    zhady009
        11
    zhady009  
    OP
       2019-05-22 10:41:02 +08:00
    @gejun123456 感谢找到原因了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3623 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:50 · PVG 12:50 · LAX 20:50 · JFK 23:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.