Home > Java > javaTutorial > body text

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

(*-*)浩
Release: 2019-05-29 11:02:02
Original
3671 people have browsed it

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);
Copy after login

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>
Copy after login

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);
Copy after login

SQL mapping file:

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

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();
Copy after login

SQL mapping file:

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

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);
Copy after login

SQL mapping file:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template