mybatis 传递一个参数,多个参数的应用

1、传递单个参数


注意:当有test条件判断时候,dao层参数必须用注解,否则会报错,

public List<Caidan> getcaidan(@Param(value="czyid") String czyid);//参数用注解

xml文件配置

    <select id="getcaidan" parameterType="java.lang.String" resultType="com.dyaqjy.bean.Caidan">  //   parameterType这个属性可以不写
   <choose>
   <when test="czyid=='000_admin'">//用注解cayid也可以写成param1
              SELECT * FROM s_caidan where upid='root'
   </when>
   <otherwise>
         SELECT s_caidan.id,s_caidan.title,s_caidan.upid,s_caidan.url,
       s_caidan.icon,s_quanxian.czyid
       FROM s_caidan RIGHT OUTER JOIN s_quanxian ON s_quanxian.caidanid = s_caidan.id
       WHERE s_caidan.upid = 'root' AND s_quanxian.czyid = #{czyid}//用注解cayid也可以写成param1
       ORDER BY s_caidan.id ASC
   </otherwise>
    </choose>
                     
    </select>


没有参数判断的话,参数可以用注解也可以不用注解,下面几种情况都可以使用


public List<XXBean> getXXBeanList(String id);  

<select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">

  select t.* from tableName t where t.id= #{id}  

</select>  

其中方法名和ID一致,#{}中的参数名与方法中的参数名一直, 我这里采用的是XXXBean是采用的短名字,

select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。





public List<XXBean> getXXBeanList(String id);  

<select id="getXXXBeanList"  resultType="XXBean"> //一个参数的时候,可以不写参数类型属性,直接用#{index},从零开始的

  select t.* from tableName t where t.id= #{0}  

</select>  


public List<XXBean> getXXBeanList(String id);  
<select id="getXXXBeanList"  resultType="XXBean"> //一个参数额时候,可以不写参数类型属性,直接用#{id}
  select t.* from tableName t where t.id= #{id}  
</select>  
④
也可以用最上面的注解,用法都一样,此处不写了




2、传递多个参数


public List<XXXBean> getXXXBeanList(String xxId, String xxCode);  

<select id="getXXXBeanList" resultType="XXBean">

  select t.* from tableName where id = #{0} and name = #{1}  

</select>  

由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始


3、用注解传递多个参数


public AddrInfo getAddrInfo(@Param("corpId")int corpId, @Param("addrId")int addrId);
 
xml配置这样写:
 
<select id="getAddrInfo"  resultMap="com.xxx.xxx.AddrInfo">
       SELECT * FROM addr__info 
    where addr_id=#{addrId} and corp_id=#{corpId}
</select>
 
以前在<select>语句中要带parameterType的,现在可以不要这样写。


4、把所有参数放到一个实体类中,类似于第二种方法

public abstract int  appliInformationcount(Apply app);

xml配置这样写:


        <select id="appliInformationcount" parameterType="com.dyaqjy.bean.Apply" resultType="int" >  
  
//  parameterType参数写上类的全路径
     
             select    count(*)
            FROM s_apply
            LEFT OUTER JOIN p_area ON s_apply.areaid = p_area.id
            LEFT OUTER JOIN p_illegal ON s_apply.illegalid = p_illegal.id
            LEFT OUTER JOIN p_term ON s_apply.termid = p_term.id
            LEFT OUTER JOIN p_unit ON p_unit.id = s_apply.unitid
            LEFT OUTER JOIN s_user ON s_user.wxuserid = s_apply.userid   
            where   s_apply.termid = #{termid}  //通过实体传进来的参数,可以直接使用#{termid}
          <if test="username!=null and username!=''">  //注意test中的参数直接写变量名,不用加#{},否则不管满不满足条件都为真    
                   and   s_user.username =  #{username}
         </if>       
          <if test="ucardid!=null and ucardid!=''">         
                    and   s_user.ucardid = #{ucardid}  
            </if>
   
        </select>
5、传递两个或两个以上的实体参数

//dao层的方法参数要用注解
public abstract List<Apply> appliInformationSearcher(@Param(value="app")Apply app,@Param(value="pagi")Pagination pagi);
        <select id="appliInformationSearcher"   resultType="com.dyaqjy.bean.Apply" >                
            select
            s_apply.id,
            s_apply.userid,
            s_user.username,
            s_apply.unitid,
            p_unit.name unitname,
            s_apply.termid,
            p_term.tname termname,
             s_apply.areaid,
            p_area.name areaname,
             s_apply.illegalid,
            p_illegal.illegalname,            
            s_apply.illegaltype      
             FROM s_apply
             LEFT OUTER JOIN p_area ON s_apply.areaid = p_area.id
             LEFT OUTER JOIN p_illegal ON s_apply.illegalid = p_illegal.id
             LEFT OUTER JOIN p_term ON s_apply.termid = p_term.id
             LEFT OUTER JOIN p_unit ON p_unit.id = s_apply.unitid
             LEFT OUTER JOIN s_user ON s_user.wxuserid = s_apply.userid
             where s_apply.termid = #{param1.termid}             
           <if test="param1.username!=null and param1.username!=''"> //param1代表第一参数,param1.username就是dao方法中第一个参数类中的字段     
                   and   s_user.username =  #{param1.username}
         </if>       
          <if test="param1.ucardid!=null and param1.ucardid!=''">         
                    and   s_user.ucardid = #{param1.ucardid}  
            </if>
            order by s_apply.id desc
            limit  #{param2.startRow,jdbcType=INTEGER},#{param2.rows,jdbcType=INTEGER}  
        </select>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值