Return Value Type MyBatis introduced in resultType

A return data type generally

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

mapper interfaces:

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

SQL mapping file:

 
  1. <!--
  2. 指定 resultType 返回值类型时 String 类型的,
  3. string 在这里是一个别名,代表的是 java.lang.String
  4.  
  5. 对于引用数据类型,都是将大写字母转小写,比如 HashMap 对应的别名是 'hashmap'
  6. 基本数据类型考虑到重复的问题,会在其前面加上 '_',比如 byte 对应的别名是 '_byte'
  7. -->
  8. <select id="getEmpNameById" resultType="string">
  9. select username from t_employee where id = #{id}
  10. </select>

Second, the return type JavaBean

Such as access to information in the database according to a field, the package information query result into a JavaBean type of data.

mapper interfaces:

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

SQL mapping file:

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

Third, the return Listtype

Sometimes the data we want to query more than one, such as: Fuzzy query, full-table query, this time may be more than the returned data is a data for processing multiple data can be stored in Listthe collection.

mapper interfaces:

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

SQL mapping file:

 
  1. <!--
  2. 注意这里的 resultType 返回值类型是集合内存储数据的类型,不是 'list'
  3. -->
  4. <select id="getAllEmps" resultType="employee">
  5. select * from t_employee
  6. </select>

Fourth, the return Maptype

MyBatis also supports data package into a query Map.

1. If the result of the query is one, we can query data in a {表字段名, 对应的值}manner to deposit Mapin.

mapper interfaces:

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

SQL mapping file:

 
  1. <!--
  2. 注意这里的 resultType 返回值类型是 'map'
  3. -->
  4. <select id="getEmpAsMapById" resultType="map">
  5. select * from t_employee where id = #{id}
  6. </select>

Below the results of data queries posted for your reference:



2. If the result of the query is a plurality of data, we can also query data in a {表中某一字段名, JavaBean}way to package into Map.

mapper interfaces:

 
  1. // 查询所有员工的信息,把数据库中的 'id' 字段作为 key,对应的 value 封装成 Employee 对象
  2. // @MapKey 中的值表示用数据库中的哪个字段名作 key
  3. @MapKey("id")
  4. Map<Integer, Employee> getAllEmpsAsMap();

SQL mapping file:

 
  1. <!--
  2. 注意 resultType 返回值类型,不再是 'map',而是 Map 的 value 对应的 JavaBean 类型
  3. -->
  4. <select id="getAllEmpsAsMap" resultType="employee">
  5. select * from t_employee
  6. </select>

Here is the result of a query (only interception part):

MyBatis allows the results of the query into a package Map, such a mechanism is excellent.

Fifth, expand

Extension.  Returns the result of the above forms are based on the query ( select), in fact, change operations for additions and deletions may also return a certain type of data, for example Boolean, Integerand the like.

Published 244 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_18671415/article/details/105303781