Mybatis操作数据时出现:java.sql.SQLSyntaxErrorException: Unknown column 'XXX' in 'field list'

这个错误比较重要,而且很常见,故单独进行说明:

Mybatis出现:Unknown column ‘xxx‘ in ‘field list‘

先来看一下程序的内部:

dao.addUser("ll111", "ll11");// 添加用户l main函数测试
//添加用户 成功1 失败0
    public int addUser(String userPhoneNumber, String userPassword) throws IOException {
        //用户的 手机号,姓名(默认手机号),密码
        User user = new User(userPhoneNumber, userPhoneNumber, userPassword);
        int result = mapper.addUser(user);

        System.out.println(result);

        sqlSession.commit();
        return result;
    }
<!--    添加用户-->
    <insert id="addUser" parameterType="main.pojo.User">
        INSERT INTO user(user_phone_number, user_name, user_password)
        VALUES (${userPhoneNumber}, ${userName}, ‘${userPassword}‘);
    </insert>

错误原因:本身来说,三个字段都属于String类型,但是在使用Mybatis时由于用法的错误,致使出现上图的结果,即本来手机号和名字想作为字符串插入,但是xml里却当成了字段名。如果是整数还好,String可以解析整数并且转化为字符串,但是像字母和数字混合,编译器就无法识别。

本质上来说,是Mybatis使用上的错误,不熟悉,理解其实现机理所致。同样的问题,也会出现在其他语句中,所以在实现时注意数据传输的类型!!!

解决方法:也如上图xml代码所示,password的变量使用单引号括住,即可表示为字符串,所以实际上,应该这样写:

<!--    添加用户-->
    <insert id="addUser" parameterType="main.pojo.User">
        INSERT INTO user(user_phone_number, user_name, user_password)
        VALUES (‘${userPhoneNumber}‘, ‘${userName}‘, ‘${userPassword}‘);
    </insert>

Mybatis操作数据时出现:java.sql.SQLSyntaxErrorException: Unknown column 'XXX' in 'field list'

原文地址:https://www.cnblogs.com/Comet-Fei/p/11946206.html

时间: 2024-10-12 20:39:59

Mybatis操作数据时出现:java.sql.SQLSyntaxErrorException: Unknown column 'XXX' in 'field list'的相关文章

实体类与数据库字段不匹配问题,java.sql.SQLSyntaxErrorException: Unknown column &#39;xxx&#39; in &#39;field list&#39;

控制台报错 ### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Unknown column 'user_name' in 'field list' ### The error may exist in panfeng/mapper/StudentMapper.java (best guess) ### The error may involve panfeng.mapper.StudentMapper.se

java.sql.SQLException: Unknown column &#39;user0_.user_name&#39; in &#39;field list&#39; 报错问题

报错信息: java.sql.SQLException: Unknown column 'user0_.user_name' in 'field list'Query is: select user0_.id as id1_0_, user0_.user_name as user_nam2_0_, user0_.password as password3_0_ from user user0_java thread: http-nio-8881-exec-1 Hibernate报错:说表里没有u

Python Flask 向MySQL表里插入一条记录,提示Unknown column &#39;XXX&#39; in &#39;field list

sql =' INSERT INTO `blog`.`user` (`id`, `username`) VALUES (%d, %s)' %(1,username) 我在给username赋值aaabbb后提示:"Unknown column 'aaaabbb' in 'field list'" 找了半天问题,原来是%s没加双引号. 改为如下 sql =' INSERT INTO `blog`.`user` (`id`, `username`) VALUES (%d, "%s

[mybatis] sql语句无错误,但是执行多条sql语句时,抛出java.sql.SQLSyntaxErrorException

错误内容 org.springframework.jdbc.BadSqlGrammarException: ### Error updating database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to u

mybatis的报错总结_____2Error updating database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the ri

在编写动态sql时 trim的属性写错了,如下图: 错误示范: (接口的xml配置文件中:) 1 <update id="modifyBill" parameterType="Bill"> 2 update smbms_bill 3 <trim suffix="set" prefixOverrides="," prefix="where id=#{id}"> 4 <if tes

sql语句正确,但是报错java.sql.SQLSyntaxErrorException ORA-00907: 缺失右括号

sql语句如下,在oracle中带入参数能正常执行,在java程序中则报错java.sql.SQLSyntaxErrorException ORA-00907: 缺失右括号 select re.*, tbp.taskid, tbp.processtype, tbp.processstatus, tbp.timeLimit, tbp.createtime    posttime, tbp.proContent    dealContent from biz_t_tasktroubleprocess

JDBC报错 消息 Could not get JDBC Connection; nested exception is java.sql.SQLException: Unknown system variable &#39;tx_isolation&#39;

今天用JDBCTemplate时,发现启动服务器之后报错: Could not get JDBC Connection; nested exception is java.sql.SQLException: Unknown system variable 'tx_isolation'. 网上说是因为MySQL版本过高的问题,我的MySQL版本是8.0,里面已经没有tx_isolation,变成了transaction_isolation,用了以下几种解决方法: (1)更改MySQL  无效,仍然

连接mysql时提示java.sql.SQLException: Access denied for user &#39;root&#39;@&#39;DESKTOP-N2B2D9A&#39; (using password: YES)

用root连接mysql时提示:访问被拒绝 检查一下mysql server是否开启,发现后台在运行着..  然后查了一下mysql的用户表,发现root只能运行使用本地ip(localhost或者127.0.0.1)进行连接 解决方法:新开一个权限GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'jeffw' WITH GRANT OPTION; 然后查询一下:select * from user; 新开的root的已经开启权限,

Hibernate卡住,然后报错java.sql.SQLException: Unknown system variable &#39;language&#39;异常

启动应用的时候会卡在: 08:22:58,221 DEBUG IntegratorServiceImpl:46 - Adding Integrator [org.hibernate.cfg.beanvalidation.BeanValidationIntegrator]. 08:22:58,226 DEBUG IntegratorServiceImpl:46 - Adding Integrator [org.hibernate.secure.spi.JaccIntegrator]. 08:22: