MyBatis内SQL语句报错
问题描述:
<insert id="insertSelectiveAndTreturnId" parameterType="com.binhai.fix.bean.FixBean"
useGeneratedKeys="true" keyProperty="id">
insert into t_fix
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="describe != null" >
describe,
</if>
<if test="subDate != null" >
subDate,
</if>
<if test="finDate != null" >
finDate,
</if>
<if test="phone != null" >
phone,
</if>
<if test="shifoushanchu != null" >
shifoushanchu,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="describe != null" >
#{describe,jdbcType=VARCHAR},
</if>
<if test="subDate != null" >
#{subDate,jdbcType=VARCHAR},
</if>
<if test="finDate != null" >
#{finDate,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
#{phone,jdbcType=INTEGER},
</if>
<if test="shifoushanchu != null" >
#{shifoushanchu,jdbcType=INTEGER},
</if>
</trim>
</insert>
报错:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'describe,
phone )
values ( '111',
' at line 2
### The error may involve com.binhai.fix.dao.FixBeanDao.insertSelectiveAndTreturnId-Inline
### The error occurred while setting parameters
### SQL: insert into t_fix ( describe, phone ) values ( ?, ? )
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'describe,
phone )
values ( '111',
' at line 2
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'describe,
phone )
values ( '111',
' at line 2
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
我看了看了好几遍真是没看出来什么错误,请大神指点一二 谢谢!头发都秃了
答
你的有一个参数名叫 describe , 这个在MYSQL中和 EXPLAIN 是同义词 ,建议你换一个参数名 , 或者这样使用
insert into t_fix ( `describe` , phone) values....
答
phone的类型是什么?如果是int的话,判断!=null是不对的,如果没问题,那就将insert into t_fix ( describe, phone ) values ( ?, ? ) 这个sql在navicat里面执行一下,看看报错不
答
同楼上 使用了关键字,可以使用特殊方式使程序运行,不过不建议是字段使用关键字!另外请看异常 信息 明明白白啊