About mybatis return results map

ResultMap

Basic use

The return value is used for the case of a custom entity classes

Map data type of entity class

Uniquely identifies the resultMap: id

column: field names of the database table

property: the entity class attribute name

resultType can return gives the type of the return value, such as String, int, Map, and the like, which will return List is defined as the return type Map, then they are automatically mybatis map in a List, it may also be a resultType objects


<? xml Version = "1.0" encoding = "UTF-8"?>
<DOCTYPE Mapper!
the PUBLIC "- // mybatis.org//DTD Mapper 3.0 // EN"
"http://mybatis.org/dtd/mybatis mapper.dtd--3 ">
<- namespace:! the current namespace database table mapping file, only can not be repeated ->
<Mapper namespace =" com.hao947.sql.mapper.PersonMapper ">
  <-! type: type id mapping data entity class: uniquely identifies the resultMap ->
  <resultMap of the type = "the Person" id = "BaseResultMap">
    <- column:! field name property of the database table: the name of the entity class attributes - ->
    <ID = column "the person_id" Property = "the personId" />
    <Result column = "name" Property = "name" />
    <Result column = "Gender" Property = "Gender" />
    <result column="person_addr" property="personAddr" />
    <result column="birthday" property="birthday" />
  </resultMap>
  <- id:! sql this unique identification of
     the parameterType: data type of the input parameters of
     the resultType: return value data type
     # {}: If a parameter is passed ID # {} for receiving the contents of an arbitrary parameter, if multiple parameters have certain rules, using the pre-compiled form of the SELECT
    * from the p-the WHERE p.id = the Person, highly secure -?>
 
  <-! SQL statement returns the value type using resultMap ->
  <the SELECT = ID "selectPersonById" the parameterType = "java.lang.Integer"
    The resultMap = "BaseResultMap">
    SELECT * WHERE P p.person_id from Person ID = # {}
  </ SELECT>
  <- The resultMap:! return value is suitable from where the entity classes defined
  resultType: suitable return data type is non-defined value, i.e., the type provided jdk ->
  <SELECT ID = "selectPersonCount" the resultType = "java.lang.Integer">
    SELECT COUNT (*) from
    Person
  </ SELECT>
 
  <select id="selectPersonByIdWithMap" parameterType="java.lang.Integer"
    resultType="java.util.Map">
    select * from person p where p.person_id= #{id}
    </select>
 
</mapper>

resultType is a mapping result set can be simply written resultType = "map" or resultType = "hashmap", wherein the "map" and "hashmap" are able to identify the alias mybatis written as "java.util.HashMap" course no problem, at the end java code, is so written:

List<Map<String,Object>> list =sqlSession.selectList("User.test"); 

      for(Map<String,Object> map :list){  

           System.out.println (map.get ( "the above mentioned id")); // by map.get ( "key"), you can get the results you need.

      } 

 

Guess you like

Origin www.cnblogs.com/ysySelf/p/11223971.html