@Param注解在mybatis中的使用以及傳入參數的幾種方式(轉)


第一種:

Dao層的方法

<span style="font-size:12px;">Public User selectUser(String name,String password);</span>  

 

對應的Mapper.xm

  

<select id="selectUser" resultMap="BaseResultMap">  

    select  *  from user_user_t   where user_name = #{0} and user_password=#{1}  

  </select>  

 


第二種:

 

該方法采用Map傳多參數

Dao層的方法

 

<span style="font-size:12px;">Public User selectUser(Map paramMap);</span>  

 

對應的Mapper.xml

 <select id=" selectUser" resultMap="BaseResultMap">  
  select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_password=#{userPassword,jdbcType=VARCHAR}
 </select>

 

  

Service層調用

 

 

public User xxxSelectUser(){  

Map paramMap=new hashMap();  

paramMap.put(“userName”,”對應具體的參數值”);  
paramMap.put(“userPassword”,”對應具體的參數值”);  
User user=xxx. selectUser(paramMap);} 

 

 

個人認為此方法不夠直觀,見到接口方法不能直接的知道要傳的參數是什么。

 

第三種:

Dao層的方法

Public User selectUser(@param(“userName”)Stringname,@param(“userpassword”)String password);

 

對應的Mapper.xml

<select id=" selectUser" resultMap="BaseResultMap">  

   select  *  from user_user_t   where user_name = #{userName,jdbcType=VARCHAR} and user_password=#{userPassword,jdbcType=VARCHAR} 

</select>

 

第四種:

mapper.java:

public List<User> getUserByParam(User use);

 

 

對應Mapper.xml:

<select id="getUserByParam" resultType="com.ray.bean.User" parameterType="com.ray.bean.User" > 
    select * from t_pub_user t 
     <where>
        <if test="{user_name}!=null">
             t.user_name like CONCAT('%',#{user_name},'%')
        </if>
        <if test="{user_password}!=null">
            and t.user_password like CONCAT('%',#{user_password},'%')
        </if>
    </where>
     limit #{1},#{2} 
</select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM