Instructions for using resultType value in xml file of mybatis Mapper

  • 2021-11-24 01:29:28
  • OfStack

The resultType value in the xml file of the directory Mapper (1) returns the value of one general data type (2) when the return type is javaBean (3) when the return type is List (4) returns the number of types Map structure (5) says 1 about the method of passing in multiple parameters in mapper layer in mybatis. resultType analysis summary of mybatis learning 1, object type 2, List type 3, Map type

resultType value in xml file of Mapper

Returns a value of 1 general data type

For example, the value of one field in the table is obtained according to id or field condition query


User Sel(int id); // According to id Query 

SQL mapping file


<select id="Sel" resultType="java.lang.String"> // Notice that this one writes the full name of the class  
    select username from user_test where id = #{id}
</select>  

You need to define the alias of resultType if you need abbreviation

The basic type of java does not require an alias:

别名 映射的类型
_byte byte
_long long
_short short
_int int
_boolean boolean
integer Integer
string String
date Date
boolean Boolean

When the return type is javaBean


<select id="Sel" resultType="com.tx.springboottestdemo.entity.User">
    select * from user_test where id = #{id}
</select>
<typeAliases>  
<!--  Definition for a single alias  type: Path of type  alias: Alias  -->  
<typeAlias type="cn.itcast.mybatis.po.User" alias="user"/>  

③ When the return is List type

Sometimes we need fuzzy query or full table query, and the returned data is multiple pieces, so we can save multiple pieces of data to list.

mapper interface


List<User> getUsers();

SQL mapping file:


<select id="getUsers" resultType="com.tx.entity.User">
select * from user
</select>

Note here that the return is List type but resultType is still javaBean. Some people may wonder why this is not a collection type. In fact, looking at the essence through phenomena is JavaBean.

④ Return type number Map structure

When we return 1 piece of data in the query, we can encapsulate {field name, field value} into Map structure.


Map<String, Object> findUserByName(Integer id);

SQL mapping file:


<select id="findUserByName" resultType="string">
select userName from user where id=#{id};
</select>

⑤ Let's talk about the method of passing in multiple parameters in mapper layer in mybatis

1. Actually, it can be regarded as multiple parameters


public List<User> findUser( String name1,  String name2);

Corresponding SQL mapping file:


<select id="findUser3"  resultType="com.tx.springboottestdemo.entity.User">
    select * from user_test where userName = #{0} and realName = #{1}
</select> 

Among them, # {0}, # {1} are indexed according to the order of mybatis by default, but errors will be reported in springboot 2.1 (integrated mybatis framework). I think it is OK on the Internet. I try a lot and it seems that I can't. error is posted below:

~~org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]~~

2. It can be seen as annotated


public List<User> findUser( @Param("name1") String name1, @Param("name2") String name2);

Corresponding SQL file:


<select id="Sel" resultType="java.lang.String"> // Notice that this one writes the full name of the class  
    select username from user_test where id = #{id}
</select>  
0

3. Parameters can be encapsulated in Map

Sometimes, our business data query does not define the corresponding POJO, so we encapsulate the parameters.


public List<User> findUser1(Map<String, Object> map);

Corresponding SQL file:


<select id="findUser1" parameterType="java.util.Map" resultType="com.tx.springboottestdemo.entity.User">
    select * from user_test where userName = #{username} and realName = #{realname}
</select>

resultType Analysis of mybatis Learning

resultType is the return value type defined in sql mapping file, and the return value includes basic type, object type, List type, Map type and so on. Now summarize 1 and explain again

Summarize

resultType:

1. Basic type: resultType= basic type

2. List type: resultType = the type of element in List

3. Map type:

Single record: resultType = map

Multiple records: resultType = type of value in Map

1. Object type

For the object type resultType, just write the full class name of the object directly

Example:

hotelMapper interface


<select id="Sel" resultType="java.lang.String"> // Notice that this one writes the full name of the class  
    select username from user_test where id = #{id}
</select>  
3

HotelMapper.xml


<select id="Sel" resultType="java.lang.String"> // Notice that this one writes the full name of the class  
    select username from user_test where id = #{id}
</select>  
4

Test class:


<select id="Sel" resultType="java.lang.String"> // Notice that this one writes the full name of the class  
    select username from user_test where id = #{id}
</select>  
5

2. List type

The return value is of type List, and resultType is the type of object in List, such as List < Hotel > , resultType is Hotel

Example:

hotelMapper interface


<select id="Sel" resultType="java.lang.String"> // Notice that this one writes the full name of the class  
    select username from user_test where id = #{id}
</select>  
6

hotelMapper.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.pjf.mybatis.dao.HotelMapper">
    <!--  The return value is List , resultType For List The full class name of the element in the  -->
    <select id="getHotel" resultType="com.pjf.mybatis.po.Hotel">
        select * from hotel
        where
        price>#{price}
    </select>
</mapper>

Test class:


package com.pjf.mybatis;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.pjf.mybatis.dao.HotelMapper;
import com.pjf.mybatis.po.Hotel;
public class TestHotel {
    public SqlSessionFactory sqlSessionFactory() throws IOException {
        // mybatis Configuration file for 
        String resource = "mybatis_config.xml";
        //  Use the class loader to load mybatis Which also loads the associated mapping file TestHotel.class.getClassLoader()
        InputStream is = Resources.getResourceAsStream(resource);
        //  Build sqlSession Our factory 
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return sessionFactory;
    }
    //  Check 
    @Test
    public void getHotel() throws IOException {
        SqlSessionFactory sessionFactory = sqlSessionFactory();
        SqlSession session = sessionFactory.openSession();
        HotelMapper hotelMapper = session.getMapper(HotelMapper.class);
        System.out.println(hotelMapper.getClass());
        //  The return value is List
        List<Hotel> list = hotelMapper.getHotel(1000);
        for (Hotel hotel : list) {
            System.out.println(hotel);
        }
        session.close();
    }
}

3. Map type

a, which returns the map of a single record, key is the attribute, and the value is the attribute value. resultType is map

hotelMapper interface


<select id="Sel" resultType="java.lang.String"> // Notice that this one writes the full name of the class  
    select username from user_test where id = #{id}
</select>  
9

hotelMapper.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.pjf.mybatis.dao.HotelMapper">
    <!--  The return value is map , resultType For map -->
    <select id="getHotel" resultType="map">
        select * from hotel
        where
        id=#{id}
    </select>
</mapper>

Test class: Hotels that return id=1001


package com.pjf.mybatis;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.pjf.mybatis.dao.HotelMapper;
import com.pjf.mybatis.po.Hotel;
public class TestHotel {
    public SqlSessionFactory sqlSessionFactory() throws IOException {
        // mybatis Configuration file for 
        String resource = "mybatis_config.xml";
        //  Use the class loader to load mybatis Which also loads the associated mapping file TestHotel.class.getClassLoader()
        InputStream is = Resources.getResourceAsStream(resource);
        //  Build sqlSession Our factory 
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return sessionFactory;
    }
    //  Check 
    @Test
    public void getHotel() throws IOException {
        SqlSessionFactory sessionFactory = sqlSessionFactory();
        SqlSession session = sessionFactory.openSession();
        HotelMapper hotelMapper = session.getMapper(HotelMapper.class);
        System.out.println(hotelMapper.getClass());
        //  The return value is map
        Map<String, Object> map = hotelMapper.getHotel(1001);
        System.out.println(map);
        session.close();
    }
}

b, which returns the map of multiple records. key is an arbitrary 1 attribute, and the value is an object type. E.g. Map < String,Hotel > , resultType is Hotel

When map for multiple records is returned, key is an arbitrary 1 attribute with an object type value, but key requires that one attribute in the object be named key by @ MapKey ("hotelName")

Example:

hotelMapper interface


package com.pjf.mybatis.dao;
import java.util.Map;
import org.apache.ibatis.annotations.MapKey;
import com.pjf.mybatis.po.Hotel;
public interface HotelMapper {
    //  The return value is Map , key Need to pass @MapKey(" Attribute name ") To specify javaBean In 1 Attribute named key , value For the object 
    @MapKey("hotelName")
    public Map<String, Hotel> getHotel(Integer i);
}

hotelMapper. xml file


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.pjf.mybatis.dao.HotelMapper">
    <!--  The return value is map , resultType Is the full class name of the object  -->
    <select id="getHotel" resultType="com.pjf.mybatis.po.Hotel">
        select * from hotel
        where
        price>#{price}
    </select>
</mapper>

Test class: Return price > More than 1000 hotels


package com.pjf.mybatis;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.pjf.mybatis.dao.HotelMapper;
import com.pjf.mybatis.po.Hotel;
public class TestHotel {
    public SqlSessionFactory sqlSessionFactory() throws IOException {
        // mybatis Configuration file for 
        String resource = "mybatis_config.xml";
        //  Use the class loader to load mybatis Which also loads the associated mapping file TestHotel.class.getClassLoader()
        InputStream is = Resources.getResourceAsStream(resource);
        //  Build sqlSession Our factory 
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return sessionFactory;
    }
    //  Check 
    @Test
    public void getHotel() throws IOException {
        SqlSessionFactory sessionFactory = sqlSessionFactory();
        SqlSession session = sessionFactory.openSession();
        HotelMapper hotelMapper = session.getMapper(HotelMapper.class);
        System.out.println(hotelMapper.getClass());
        //  The return value is map
        Map<String, Hotel> map = hotelMapper.getHotel(1000);
        System.out.println(map);
        session.close();
    }
}

Related articles: