ibatis 之 java.util.Map作为parameterClass和resultClass

ibatis 之 java.util.Map作为parameterClass和resultClass1.Map作为parameterClass    映射文件:    <!useMaptypeasparameterClass><selectid="getProduct-Map"parameterClass="java.util.Map"resultMap="get-product-result"><!…

1.Map作为parameterClass

 

   映射文件:

    

<!--use Map type as parameterClass-->  
<select id="getProduct-Map" parameterClass="java.util.Map" resultMap="get-product-result">  
     <![CDATA[ 
          select * from t_product  where prd_id=#id# and prd_description=#description# 
     ]]>  
</select>

 

   DAO层:

  

  

/** 
  * java.util.Map作为parameterClass 
  */  
 public Product getProductMap(Map map) throws SQLException {  
    init();  
    Product product = (Product)sqlMapClient.queryForObject("getProduct-Map", map);  
    return product;  
} 

 

  Test类:

 

public void getProductMap() throws SQLException{  
     Map map = new HashMap();  
     map.put("id", new Integer(1));  
     map.put("description", "basketball");  
     Product product = productDao.getProductMap(map);  
     System.out.println(product);  
} 

 

  结果:

  id:1
  description:basketball
  price206.99

 

2.Map作为resultClass

 

   映射文件:

   

   

<resultMap id="get-product-map" class="java.util.HashMap">  
        <result property="id" column="prd_id"/>  
        <result property="description" column="prd_description"/>  
        <result property="price" column="prd_price"/>  
</resultMap>  
      
<!--START  use Map type as resultClass,MUST use java.util.HashMap instead java.util.Map-->  
<select id="getProdcut-MapResult" resultClass="java.util.HashMap">  
      <![CDATA[ 
         select * from t_product 
      ]]>  
</select>              
<select id="getProductUseMap-resultMap" resultMap="get-product-map">  
     <![CDATA[ 
         select * from t_product 
     ]]>  
</select>

 

 

  DAO层:

   

 

/** 
  * java.util.Map作为resultClass 
  */  
public List getProductMapResult() throws SQLException {  
     init();  
     List list = sqlMapClient.queryForList("getProdcut-MapResult");  
     return list;              
}  
public List getProductUseMapByResultMap() throws SQLException {  
    init();  
    List list = sqlMapClient.queryForList("getProductUseMap-resultMap");  
    return list;  
}

 

  Test类:

 

    

public void getProductMapResult() throws SQLException{
		
    Map map = null;
		
    List list = productDao.getProductMapResult();
		
    for(Iterator it=list.iterator(); it.hasNext();) {
			
        //List里存放的是java.util.Map类型
			
        Object obj = (Object)it.next();
			
        System.out.println(obj.getClass());
			
        System.out.println(obj);
		
    }
	
}
	
	
public void getProductUseMapByResultMap() throws SQLException {
		
     Map map = null;
		
     List list = productDao.getProductUseMapByResultMap();
		
     for(Iterator it=list.iterator(); it.hasNext();) {
			
         //List里存放的是java.util.Map类型
			
         Object obj = (Object)it.next();
			
         System.out.println(obj.getClass());
			
         System.out.println(obj);
		
      }
	
} 

 

 

结果:

class java.util.HashMap
{prd_id=1, prd_price=206.99, prd_description=basketball}
class java.util.HashMap
{prd_id=2, prd_price=106.99, prd_description=football}
class java.util.HashMap
{price=206.99, description=basketball, id=1}
class java.util.HashMap
{price=106.99, description=football, id=2}

 

 

注意: Map作为resultClass时,必须指定具体的实现类,比如java.util.HashMap,否则会报错

Caused by: java.lang.RuntimeException: JavaBeansDataExchange could not instantiate result class.  Cause: java.lang.InstantiationException: java.util.Map

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/38076.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注