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

2019-05-21 19:50:48 +08:00
 zhady009
CDATA 试了几种写法发现都不管用
3665 次点击
所在节点    Java
11 条回复
gejun123456
2019-05-21 21:48:32 +08:00
sql 看看,可以试试 bind 标签
<bind name="hello" value="$">
select * from where id = #{hello}
Belmode
2019-05-21 22:15:18 +08:00
html 符号实体 $
```
&#36;
```
zhady009
2019-05-21 22:19:45 +08:00
@gejun123456 就是用你插件生成的 insert 买了一年
zhady009
2019-05-21 22:24:58 +08:00
@gejun123456 就比如一个 username 字段 value 是 x$x
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">
<!--@mbg.generated-->
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
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">
<!--@mbg.generated-->
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
2019-05-22 10:05:29 +08:00
数据是写进去了..不太理解
gejun123456
2019-05-22 10:15:48 +08:00
@zhady009 #7 用了啥 mybatis 插件么,我这边不会出现这个问题的
gejun123456
2019-05-22 10:16:39 +08:00
@zhady009 #7 看看 有没有插件用了 replaceAll 啥的 https://jerval.iteye.com/blog/2164227
gejun123456
2019-05-22 10:18:14 +08:00
https://github.com/abel533/Mapper/issues/30

可以参考这个帖子,也是用了一个 mybatis 的插件 报的错
zhady009
2019-05-22 10:41:02 +08:00
@gejun123456 感谢找到原因了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/566325

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX