[Spring jdbc] jdbcTemplate.queryXxx() The return value is null


Today, when using the following method jdbcTemplate.queryXxx()to query data from the database, some of the returned objects and collections are displayed as null, and I am very puzzled. Why are some null values, some are not, and why there are null values?

// 查询返回用户对象
@Override
public User selectObject(String id) {
    
    
    // sql 语句
    String sql = "select * from t_user where userId=?";
    User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
    return user;
}

// 查询信息返回集合
@Override
public List<User> findAll() {
    
    
    // sql语句
    String sql = "select * from t_user";
    List<User> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<User>(User.class));
    return list;
}

The following figure shows the correct result, but the parts circled at the beginning are all null值.
Insert picture description here
Then return to toString()the place to view, we found in the User class to write a little userIDof setter()方法, after the corrections userIdshows, but userStatusstill null值.

I was puzzled. I searched it online and found an article. It feels a bit reasonable~

The spring jdbctemplate entity attribute mapping value is null: https://blog.csdn.net/iteye_14756/article/details/81800913?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_utm_term-4&spm=1001.2101.3001.4242

The article said that there was a problem during the conversion of this source code: the main problem was the underscore in the field. Although mine is not, I vaguely feel something!

/**
 1. Convert a name in camelCase to an underscored name in lower case.
 2. Any upper case letters are converted to lower case with a preceding underscore.
 3. @param name the string containing original name
 4. @return the converted name
 */
private String underscoreName(String name) {
    
    
    StringBuffer result = new StringBuffer();
    if (name != null && name.length() > 0) {
    
    
        //把第一个字母小写了
        result.append(name.substring(0, 1).toLowerCase());
        for (int i = 1; i < name.length(); i++) {
    
    
            //获取第二个字母
            String s = name.substring(i, i + 1);
            //如果第二个字母是大写的
            if (s.equals(s.toUpperCase())) {
    
    
                //给他添加下划线
                result.append("_");
                //然后加上第三个小写字母
                result.append(s.toLowerCase());
            } else {
    
    
                //或者直接返回
                result.append(s);
            }
        }
    }
    return result.toString();
}

After checking some information, I found that when doing this kind of mapping similar to the database entity class, the attribute name of the entity class must be consistent with the field name in the database! So iReplace the database field names with the entity class attribute names in the User class to be consistent, And finally solved the null value.
Insert picture description here
Summary of solutions:

  1. There is no setter method in the bean class
  2. The fields in the bean class do not correspond to the fields in the table in the database

Guess you like

Origin blog.csdn.net/qq_45797116/article/details/114379307