When Mybatis operation data: java.sql.SQLSyntaxErrorException: Unknown column 'XXX' in 'field list'

This error is more important, and very common, it is explained individually:

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

First look at the internal procedures:

dao.addUser ( "ll111", "LL11"); // add users l main function test
// Add User Success 1 Failure 0 
    public  int addUser (String userPhoneNumber, the userPassword String) throws IOException {
         // user's phone number, name (the default phone number), password, 
        the User the User = new new the User (userPhoneNumber, userPhoneNumber, the 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>

 

 

 Cause of error : in itself, three fields are of type String, but due to the usage of error, the results appear on the map resulting in the use of Mybatis, that is, the original name and phone number you want to insert as a string, but actually as a xml field name. If it is okay integer, String can be resolved into a string and an integer, but as alphanumeric and compiler can not be identified.

In essence, it is wrong to use the Mybatis not familiar with, understand the mechanism of its implementation period. The same problem also occurs in other statements, so pay attention to the type of data transmission in the realization! ! !

Solution : As also shown in FIG. Xml codes, password variables live single quotes, can be expressed as a string, so in fact, it should read:

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

 

 

Guess you like

Origin www.cnblogs.com/Comet-Fei/p/11946206.html