MyBatis 常用 select标签使用学习

常用元素有:id、parameterType、resultType、resultMap,设置缓存用到flushCache、useCache

    id:配合Mapper的全限定名,联合成为一个唯一的标识,用户标识这条SQL。 parameterType:表示这条SQL接受的参数类型,可以是MyBatis系统定义或者自定义的别名,比如int、string、float等,也可以是全限定名,比如com.xx.xx.xx.pojo.User。
    resultType:表示这条SQL返回的结果类型,与parameterType一样,可以是系统定义的别名,也可以是类的全限定名。
    resultMap:它是映射器的引用,将执行强大的映射功能。我们可以使用resultType和resultMap中的一个,resultMap能提供自定义映射的机会
    flushCache:它的作用是在调用SQL后,是否要求MyBatis清空之前的查询本地缓存和二级缓存,默认 false。
    useCache:启动二级缓存的开关,是否要求MyBatis将此结果缓存 默认true。

    传递多个参数
    (1) 使用map接口传递参数–不推荐

    接口:

List queryUserByMap(Map paramentMap);


service:

   Map map=new HashMap();
   map.put("userName", "张三");
   map.put("mobile", "13800000000");


XML:

   

(2) 使用注解传递多个参数 —推荐
使用@Param(org.apache.ibatis.annotations.Param)

接口:


   List queryUserByAnnotation(@Param("userName") String username,@Param("mobile") String mobile);


 XML:


   

    

备注:这里不需要使用parameterType来表明参数类型,让MyBatis自动检索。

(3) 通过Java Bean传递多个参数

  接口:


    List queryUserByBean(User user);


  XML:


   


(4) 混合使用
接口:

   List queryUserByPage(@Param("user") User user,@Param("page") PageParam pageParam);

    

XML:

   

   

(5) 使用resultMap作为映射结果集


   
   
   
   

   

resultMap 的 id 代表它的标识,type代表使用哪个类作为其映射类,可以使用别名或者全限定名.

id:代表resultMap的主键,而result代表其属性
property:代表POJO的属性名称,而column代表SQL的列名。


   
     
     
   

   
   
   
   
   
     
   

 

   

(6) 分页参数 RowBounds
接口:

  List queryUserByRowBounds(@Param("userName") String username,@Param("mobile") String mobile,RowBounds rowBounds);  


service:

 RowBounds rowBounds=new RowBounds(0,20);


XML:

   

备注:它是MyBatis的一个附加参数,MyBatis会自动识别它。

你可能感兴趣的:(MyBatis 常用 select标签使用学习)