小言_互联网的博客

spring Mybatis 传入 动态表名

390人阅读  评论(0)

mybatis 报错:

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
  at line 1
### The error may involve defaultParameterMap
### The error occurred while setting parameters

在网上查了一下,:需要用到${tablenamme} 不能够用#{tablename};

网上找了一下原因:

#{}接受了参数,但是这个参数是 表名,所以导致这个参数一直错误。因为#{}会给参数添加  “” 变成字符串。后来改成 ${} 接受参数。这样可以执行了。

 

值得注意的是请尽量避免使用  ${} 因为此种方法可能会导致SQL注入。

 

但发现如果用${tablename},并不好用;

后面改为传入map 问题解决;

    Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("tablename", tablename);
        condition.put("username", username);
        condition.put("position", position);

mapper.updatePosition( condition);

xml

insert into ${tablename} (position,username,loadtime) values(#{position},#{username},now())


转载:https://blog.csdn.net/q610098308/article/details/102093746
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场