musenh
musenh
采纳率50%
2020-08-18 21:50

mybatis insert语句报错,查了好多博客都不是,第一次做网页,有没有人知道咋解决啊?不胜感激

50

是向数据库中插入一条数据,包的错误说在''附近有错误,但我的sql语句没有使用单引号,纠结好几天了,跪求解答!
这个是数据库的表

create table org_user(
user_id int primary key auto_increment,
regin_name nvarchar(20),
passwd varchar(12),
email varchar(20),
question nvarchar(20),
answer nvarchar(20),
name nvarchar(20),
gender nchar(1),
birthday date);

然后根据这个表建立一个对象,这个是对象的代码
public class RegUser {

private Integer userID;
private String resName;
private String passwd;
private String email;
private String question;
private String answer;
private String name;
private Date brith;
private char gender;



public RegUser() {
    super();
}

@Override
public String toString() {
    return "RegUser [userID=" + userID + ", resName=" + resName + ", passwd=" + passwd + ", email=" + email
            + ", question=" + question + ", answer=" + answer + ", name=" + name + ", brith=" + brith + ", gender="
            + gender + "]";
}

public RegUser(Integer userID, String resName, String passwd, String email, String question, String answer,
        String name, Date brith, char gender) {
    super();
    this.userID = userID;
    this.resName = resName;
    this.passwd = passwd;
    this.email = email;
    this.question = question;
    this.answer = answer;
    this.name = name;
    this.brith = brith;
    this.gender = gender;
}
public Integer getUserID() {
    return userID;
}
public void setUserID(Integer userID) {
    this.userID = userID;
}
public String getResName() {
    return resName;
}
public void setResName(String resName) {
    this.resName = resName;
}
public String getPasswd() {
    return passwd;
}
public void setPasswd(String passwd) {
    this.passwd = passwd;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getQuestion() {
    return question;
}
public void setQuestion(String question) {
    this.question = question;
}
public String getAnswer() {
    return answer;
}
public void setAnswer(String answer) {
    this.answer = answer;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Date getBrith() {
    return brith;
}
public void setBrith(Date brith) {
    this.brith = brith;
}
public char getGender() {
    return gender;
}
public void setGender(char gender) {
    this.gender = gender;
}

}

下面是在mapper中的配置

<resultMap id="RegUserMap" type="RegUser">
    <id column="user_id" property="userID"/>
    <result column="regin_name" property="resName"/>
    <result column="passwd" property="passwd"/>
    <result column="email" property="email"/>
    <result column="question" property="question"/>
    <result column="answer" property="answer"/>
    <result column="name" property="name"/>
    <result column="gender" property="gender"/>
    <result column="birthday" property="brith"/>

</resultMap>

<insert id="add" parameterType="com.musen.blog.bean.RegUser">
    insert into org_user(regin_name,passwd,email,question,answer,name,gender,birthday)
    values(#(resName),#(passwd),#(email),#(question),#(answer),#(name),#(gender),#(brith))
</insert>

然后报的错误是
Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException:

Error updating database. Cause: java.sql.SQLSyntaxErrorException: (conn=55) 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 2

The error may exist in file [E:\crazy11\blog\target\classes\com\musen\blog\bean\RegUserMapper.xml]

The error may involve com.musen.blog.bean.RegUserMapper.add-Inline

The error occurred while setting parameters

SQL: insert into org_user(regin_name,passwd,email,question,answer,name,gender,birthday) values(#(resName),#(passwd),#(email),#(question),#(answer),#(name),#(gender),#(brith))

Cause: java.sql.SQLSyntaxErrorException: (conn=55) 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 2

; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: (conn=55) 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 2


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 8月前

    你的符号没有用对
    应该是 {},而不是()
    另外看看字段的类型,和数据库的,是否一致,字段名是否一致。
    可以参考
    https://blog.csdn.net/weixin_43254907/article/details/90233804

    点赞 评论 复制链接分享
  • it_zhangwei 路漫漫兮其修远兮 8月前

    #(resName) 换成#{resName},把#(字段)换成#{字段}就行了

    点赞 评论 复制链接分享
  • elvishehai thomas.he 8月前

    insert into org_user(regin_name,passwd,email,question,answer,name,gender,birthday)
    values(#{resName},#{passwd},#{email},#{question},#{answer},#{name},#{gender},#{brith})
    这样写才对
    对了,建议用工具去生成,不需要自己去写这些东西的,你可以百度下mybatis-generator-gui,默认会给你生成curd的方法以及xml里面对应关系都会生成

    点赞 评论 复制链接分享
  • qq_42024009 风丶飞 8月前

    The error occurred while setting parameters这个是错误提示,表示属性或参数设置错误
    上面几位已经提到了参数需求要#{}或者${}(不怎么推荐这个,有sql注入的风险.有些特殊场景可以使用)

    点赞 评论 复制链接分享
  • qq1049545450 小小的人儿居然已存在 8月前


    insert into org_user(regin_name,passwd,email,question,answer,name,gender,birthday)
    values(#{resName},#{passwd},#{email},#{question},#{answer},#{name},#{gender},#{brith})

    我太难了

    点赞 评论 复制链接分享
  • luo_mu_hpu 豆北儿 8月前

    报错说的是你的RegUserMapper.xml出问题了,你的sql最后转换出来是
    insert into org_user(regin_name,passwd,email,question,answer,name,gender,birthday) values(#(resName),#(passwd),#(email),#(question),#(answer),#(name),#(gender),#(brith))
    这个样子,数据识别不了,把你的#()改成 #{}

    点赞 评论 复制链接分享