MyBatis select标签

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

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

    接口:

List<User> queryUserByMap(Map<String,Object> paramentMap);

service:

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

XML:

<select id="queryUserByMap" parameterType="map" resultType="com.bob.analyst.model.User" >
     select 
    <include refid="Base_Column_List" />
    from tbl_user
    where user_name like concat('%',#{userName},'%')
    and   mobile like concat('%',#{mobile},'%')
  </select>

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

接口:
   List<User> queryUserByAnnotation(@Param("userName") String username,@Param("mobile") String mobile); 
 XML:
    <select id="queryUserByAnnotation"resultType="com.bob.analyst.model.User" >
     select 
    <include refid="Base_Column_List" />
    from tbl_user
    where user_name like concat('%',#{userName},'%')
    and   mobile like concat('%',#{mobile},'%')
  </select>

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

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

  接口:
    List<User> queryUserByBean(User user);
  XML:
    <select id="queryUserByBean" parameterType="com.bob.analyst.model.User" resultType="com.bob.analyst.model.User" >
     select 
    <include refid="Base_Column_List" />
    from tbl_user
    where user_name like concat('%',#{userName},'%')
    and   mobile like concat('%',#{mobile},'%')
  </select>

(4) 混合使用
接口:

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

XML:

    <select id="queryUserByPage" resultType="com.bob.analyst.model.User" >
     select 
    <include refid="Base_Column_List" />
    from tbl_user
    where user_name like concat('%',#{user.userName},'%')
    and   mobile like concat('%',#{user.mobile},'%')
    limit #{page.start}, #{page.limit}
  </select>

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

<resultMap id="BaseResultMap" type="com.bob.analyst.model.User">
    <id column="id" property="id" />
    <result column="user_name" property="userName" />
    <result column="mobile" property="mobile" />
    <result column="password" property="password" />
</resultMap>

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

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

<resultMap id="BaseResultMap" type="com.bob.analyst.model.User">
    <constructor><!-- 构造方法 -->
      <idArg/>
      <arg/>
    </constructor>
    <id/><!-- 主键 -->
    <result/><!-- 配置POJO到SQL列名的映射关系 -->
    <association property=""></association><!-- 一对一级联 -->
    <collection property=""></collection><!-- 一对多级联 -->
    <discriminator javaType=""><!-- 鉴别器 -->
      <case value=""></case><!-- 用于区分 -->
    </discriminator>
  </resultMap>

(6) 分页参数 RowBounds
接口:

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

service:

 RowBounds rowBounds=new RowBounds(0,20);

XML:

<select id="queryUserByRowBounds" resultType="com.bob.analyst.model.User" >
     select 
    <include refid="Base_Column_List" />
    from tbl_user
    where user_name like concat('%',#{user.userName},'%')
    and   mobile like concat('%',#{user.mobile},'%')
  </select>

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

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值