Using MyBatisMapper
When performing database operations, you can use itSELECT
StatementField AS xx
This 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 defineMapper
When using SQL statements, it can be usedField AS xx
grammar.
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
- exist
Filed
select
In a statement, useuser_id AS id
anduser_name AS name
The fields were renamed. -
resultType=""
It means that the query result willMap
return 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 xx
grammar.
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
@Select
In annotated SQL statements, useAS
The keyword renames the field. - The method return type is
List<Map<String, Object>>
, the field name in the query result will beAS
The alias afterMap
middle.
Map to entity class
When mapping the query result to the entity class,AS
The 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
- exist
select
Used in a statementAS
The keyword willuser_id
Rename toid
,user_name
Rename toname
,andUser
The attribute names of the entity class are the same. -
resultType=""
Indicates that the query results will be mapped toUser
Entity class.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.