Package output results

catalogue

After the execution of SQL statements in MyBatis, the processing of results

    1. resultType result type refers to the java object that the data is converted to after the sql statement is executed. The java type is arbitrary

    2. Alias the resultType of the returned result

    3. Save query results into Map

    4. resultMap: result map, specifying the correspondence between column names and attributes of java objects

After the execution of SQL statements in MyBatis, the processing of results

MyBatis executes sql statements and obtains java objects

     1. resultType result type refers to the java object that the data is converted to after the sql statement is executed. The java type is arbitrary

resultType the value of the result type

         1. Fully qualified name of type

         2. Alias of type, such as Java The alias of lang.integer is int, and the alias of int type is_ int
   

Treatment method

        1. mybatis executes sql statements, and then calls the parameterless construction method of the class to create objects.

        2. mybatis pays the specified column value of ResultSet to the property with the same name of the return value.

            <select id="selectMultiPosition" resultType="com.bjpowernode.domain.Student">
                select id,name, email,age from student
            </select>

Peer to peer jdbc

            ResultSet rs = executeQuery("select id,name, email,age from student" )
            while(rs.next()){
                Student  student = new Student();
                student.setId(rs.getInt("id"));
                student.setName(rs.getString("name"))
            }

        
Look at an example

Example 1

        /*
        The return value is a single object Student,
        resultType="org.example.domain.Student"
        */
        public Student selectStudentById(Integer id);


Corresponding mapper file

        <!--
        The default assignment rule of return value is, Assign the column value of the query result to the attribute of the corresponding object
        -->    
        <select id="selectStudentById" resultType="org.example.domain.Student" >
            select * from t_student where id=#{id}
        </select>

Example 2

        /*
        The return value is list, in which is Student,
        resultType="org.example.domain.Student"
        */
        List<Student> selectMultiParam(@Param("myName") String name, @Param("myAge") Integer age);

Corresponding mapper file

        <!--
        The return value of the method is List, however resultType The value of is Student,
        because List By one Student form,
        -->
        <select id="selectMultiParam" resultType="org.example.domain.Student">
            select * from t_student where name=#{myName} or age=#{myAge}
        </select>

Example 3

        /*
        The return value is a simple type
        resultType="int", This is the alias given by MyBatis,
        You can also use the fully qualified name of the type, such as resultType="java.lang.Integer"
        */
        Integer countStudent();

Corresponding mapper file

        <select id="countStudent" resultType="int">
            select count(*) from t_student
        </select>

    2. Alias the resultType of the returned result

In example 3 above, if MyBatis is used as an alias for a simple type, can a user-defined type use an alias

That is, can resultType="org.example.domain.Student" be replaced by resultType="Student class alias"

The answer is yes

The syntax for defining an alias is in the main configuration file

An example is given. The configuration in the main configuration file is as follows

        <!--
            1. Alias directly
            Can be an alias for a type, Easy to use
            type : Fully qualified name of custom type
            alias : alias(Short, Easy to remember)

            One<typeAliases>Contains multiple<typeAlias>label, Multiple aliases can be defined
        -->
        <!--
            2. The index class in the package is the alias of the fully qualified name, Case insensitive
            use<package>, name Is the package name
            be careful package Labels are also used in<typeAliases>Medium
        -->
        <typeAliases>
            <typeAlias type="org.example.domain.Student" alias="Student" />
            <package name="org.example.domain"/>
        </typeAliases>
        public Student selectStudentById1(Integer id);

The mapper file is as follows

        <select id="selectStudentById1" resultType="Student">
            select * from t_student where id=#{id}
        </select>

    3. Save query results into Map

    /*
        Query result return Map
        SQL query statement with name from ID_ student where id = #{id}
        The result is {name=lisi, id=2}. There are two key value pairs in this map
        If the statement is select id, name, email from t_student where id = #{id}
        The query result is {name=lisi, id=2, email=124@123.com }
        The rule is
            1. The column name is key and the column value is value
            2. The query result can only be one line of record, and multiple lines will report errors
    */
    Map<Object, Object> selectMapById(Integer id);

The corresponding mapper file is

    <!--return map-->
    <select id="selectMapById" resultType="java.util.HashMap">
        select id, name from t_student where id = #{id}
    </select>

    4. resultMap: result map, specifying the correspondence between column names and attributes of java objects

As mentioned earlier, the resultType is assigned to the attribute with the same name as the column name in the java object according to the column name of the query result

If my column name and attribute name are different now, I have to use this

For example, the attribute of Student is s_id, s_name

The query result column names are ID and name

At this time, you need to use resultMap to specify the corresponding relationship between the column name and the attribute of the java object
        
Give examples

        /*
            Use ResultMap to define the mapping relationship between column names and attributes of java objects
        */
        List<NewStu> selectAllStudents();

The attribute of NewStu is s_id, s_name, s_email, s_age        

The corresponding mapper is

        <!--
            use resultMap
            1. Define first resultMap(Mapping relationship)
            2. stay select Used in labels resultMap To reference the defined
        -->
        <!--
            definition resultMap
            id : Custom name, Is the identifier of this mapping relationship
            type : java Name of permission type
        -->
        <resultMap id="stuMap" type="org.example.domain.NewStu">
            <!--id As primary key, result Yes no primary key-->
            <id column="id" property="s_id"/>
            <result column="name" property="s_name"/>
            <result column="email" property="s_email"/>
            <result column="age" property="s_age"/>
        </resultMap>
        <!--
            Define the above resultMap after, When select Used in resultMap="stuMap"after
            The query results will be id Value payment NewStu Object s_id attribute...
        -->
        <select id="selectAllStudents" resultMap="stuMap">
            select * from t_student
        </select>

Note that ResultMap and ResultType are selected from the sql statement label

In addition to the fact that ResultMap can solve the inconsistency between the attribute name and column name of java objects, there is another method, which is the column alias in sql statements

        <!--
            And can be achieved by using column aliases resultMap Same effect
            But the disadvantage is that it cannot be reused
        -->
        <select id="selectAllStudents" resultType="org.example.domain.NewStu" >
            select id as s_id, name as s_name, email as s_email, age as s_age from t_student
        </select>

 

Keywords: Java Database MySQL Mybatis Interview

Added by paullb on Fri, 11 Feb 2022 07:45:04 +0200