Home >Java >javaTutorial >What should I use to connect the return value of mybatis?

What should I use to connect the return value of mybatis?

(*-*)浩
(*-*)浩Original
2019-05-17 10:56:473694browse

What should I use to connect the return value of mybatis? It depends on what data is returned. Common ones are:

What should I use to connect the return value of mybatis?

1. Return the general data type

For example To obtain a field value in the database based on the id attribute.

mapper interface:

// 根据 id 获得数据库中的 username 字段的值
String getEmpNameById(Integer id);

SQL mapping file:

 <!-- 
        指定 resultType 返回值类型时 String 类型的,
        string 在这里是一个别名,代表的是 java.lang.String 

        对于引用数据类型,都是将大写字母转小写,比如 HashMap 对应的别名是 &#39;hashmap&#39;
        基本数据类型考虑到重复的问题,会在其前面加上 &#39;_&#39;,比如 byte 对应的别名是 &#39;_byte&#39;
    -->
    <select id="getEmpNameById" resultType="string">
        select username from t_employee where id = #{id}
    </select>

2. Return JavaBean type

For example, obtain based on a certain field The information in the database encapsulates the query result information into data of a certain JavaBean type.

mapper interface:

 // 根据 id 查询信息,并把信息封装成 Employee 对象
    Employee getEmpById(Integer id);

SQL mapping file:

 <!-- 
        通过 resultType 指定查询的结果是 Employee 类型的数据  
        只需要指定 resultType 的类型,MyBatis 会自动将查询的结果映射成 JavaBean 中的属性
    -->
    <select id="getEmpById" resultType="employee">
        select * from t_employee where id = #{id}
    </select>

3. Return List type

Sometimes we need to query There is more than one piece of data, such as fuzzy query, full table query, etc. At this time, the data returned may be more than one piece of data. For processing of multiple data, it can be stored in the List collection.

mapper interface:

 // 假如是全表查询数据,将查询的数据封装成 Employee 类型的集合
List<Employee> getAllEmps();

SQL mapping file:

<!--
        注意这里的 resultType 返回值类型是集合内存储数据的类型,不是 &#39;list&#39;
    -->
    <select id="getAllEmps" resultType="employee">
        select * from t_employee
    </select>

4. Return Map type

MyBatis also supports encapsulating the queried data into a Map.

1. If the query result is one, we can store the query data into the Map in the form of {table field name, corresponding value}.

mapper interface:

 //  根据 id 查询信息,并把结果信息封装成 Map 
Map<String, Object> getEmpAsMapById(Integer id);

SQL mapping file:

<!-- 
        注意这里的 resultType 返回值类型是 &#39;map&#39;
     -->
    <select id="getEmpAsMapById" resultType="map">
        select * from t_employee where id = #{id}
    </select>

The above is the detailed content of What should I use to connect the return value of mybatis?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn