mybatis多个参数 判断非空

来源:互联网 发布:自考网络辅导 编辑:程序博客网 时间:2024/05/16 07:06
这个标题放在百度上估计也只能搜到在下的这篇小贴子.其他贴子都是把问题拆开的a.只传一个参数判断非空的情况

  1. <if test="type!=null and type!=''">  
  1.     AND type = #{type}  
  1. </if>  
b.传多个参数不判断非空的情况

AO层的函数方法 

Public User selectUser(String name,String area);

对应的Mapper.xml  

<select id="selectUser" resultMap="BaseResultMap"> select  *  from user_user_t   where user_name = #{0} and user_area=#{1}</select>
这两个方法拿过来一点辙也没有,又不能把它们生生的拼在一块
终于让小菜发现了第三个方法使用@Param("参数")
public List<Map> statistics( @Param("startTime")String startTime,@Param("endTime")String endTime,Integer pageIndex,Integer pageSize);
<select id="statistics"  resultType="java.util.Map">  SELECT COUNT(*) AS 'total_cnt',SUM(consume_change) AS 'change_cnt',COUNT(DISTINCT(uid)) AS 'user_cnt',DATE_FORMAT(`consume_time`,'%Y-%m-%d') 'data_time'    FROM kpl_matchs_vote.guess_consume    WHERE consume_status=0 AND consume_type='bet'    <if test="startTime != null" >      AND consume_time >= #{startTime,jdbcType=TIMESTAMP}    </if>    <if test="endTime != null" >      AND #{endTime,jdbcType=TIMESTAMP} >= consume_time    </if>    GROUP BY DATE_FORMAT(`consume_time`,'%Y-%m-%d') limit #{2},#{3}</select>
这样子有多参数都可以判断
这里面用到了 MyBatis 3提供的映射注解.最初设计的时候mybatis是一个XML驱动的框架.MyBatis3构建在强大而全面的java配置API上.这个配置 API 是基于 XML 的 MyBatis 配置的 基础,也是新的基于注解配置的基础。注解提供了一种简单的方式来实现简单映射语句,而 不会引入大量的开销。
注解非常多可以参考这里(http://www.mybatis.org/mybatis-3/zh/java-api.html)
我们只看@Param它的目标是注解在参数上,如果你的映射器的方法需要多个参数, 这个注解可以被应用于映射器的方法 参数来给每个参数一个名字。否则,多 参数将会以它们的顺序位置来被命名 (不包括任何 RowBounds 参数) 比如。 #{param1} , #{param2} 等 , 这 是 默 认 的 。 使 用 @Param(“person”),参数应该被命名为 #{person}。

0 0
原创粉丝点击