SoFunction
Updated on 2025-05-19

MyBatis query database operation custom field name method

Using MyBatisMapperWhen performing database operations, you can use itSELECTStatementField AS xxThis syntax.

The following is a detailed introduction to how to use it in different scenarios.

Mapper based on XML mapped files

When using XML mapping files to defineMapperWhen using SQL statements, it can be usedField AS xxgrammar.

Sample code

Mapper interface

import ;
import ;

public interface UserMapper {
    List<Map<String, Object>> selectUsersWithAlias();
}

Mapper XML file (

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-////DTD Mapper 3.0//EN"
        "/dtd/">
<mapper namespace="">
    <select  resultType="">
        SELECT user_id AS id, user_name AS name
        FROM users
    </select>
</mapper>

Code call example

import ;
import ;
import ;
import ;

import ;
import ;
import ;

public class Main {
    public static void main(String[] args) throws Exception {
        String resource = "";
        InputStream inputStream = (resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        try (SqlSession session = ()) {
            UserMapper userMapper = ();
            List<Map<String, Object>> users = ();
            for (Map<String, Object> user : users) {
                ("ID: " + ("id") + ", Name: " + ("name"));
            }
        }
    }
}

Code explanation

  • existFiledselectIn a statement, useuser_id AS idanduser_name AS nameThe fields were renamed.
  • resultType=""It means that the query result willMapreturn the form, where the key is the field name (usingAS) The value is the field value.

Annotation-based Mapper

If using annotations to defineMapper, can also be used in SQL annotationsField AS xxgrammar.

Sample code

import ;
import ;
import ;

public interface UserMapper {
    @Select("SELECT user_id AS id, user_name AS name FROM users")
    List<Map<String, Object>> selectUsersWithAlias();
}

Code explanation

  • exist@SelectIn annotated SQL statements, useASThe keyword renames the field.
  • The method return type isList<Map<String, Object>>, the field name in the query result will beASThe alias afterMapmiddle.

Map to entity class

When mapping the query result to the entity class,ASThe alias after that must be consistent with the attribute name of the entity class, so that MyBatis can be mapped correctly.

Sample code

Entity Class

public class User {
    private Integer id;
    private String name;

    // Getters and Setters
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
         = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
         = name;
    }
}

Mapper interface

import ;

public interface UserMapper {
    List<User> selectUsersWithAlias();
}

Mapper XML file (

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-////DTD Mapper 3.0//EN"
        "/dtd/">
<mapper namespace="">
    <select  resultType="">
        SELECT user_id AS id, user_name AS name
        FROM users
    </select>
</mapper>

Code explanation

  • existselectUsed in a statementASThe keyword willuser_idRename toiduser_nameRename toname,andUserThe attribute names of the entity class are the same.
  • resultType=""Indicates that the query results will be mapped toUserEntity class.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.