码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis中传入参数的几种方式

时间:2017-08-19 10:54:05      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:param   blank   ring   mybatis   技术分享   shm   tle   char   password   

第一种:

Dao层的方法

Public User selectUser(String name,String password);

 

对应的Mapper.xm

  

技术分享
<select id="selectUser" resultMap="BaseResultMap">  

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

  </select>  
技术分享

 


第二种:

 

该方法采用Map传多参数

Dao层的方法

 

Public User selectUser(Map paramMap);

 

对应的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>
技术分享

 

mybatis中传入参数的几种方式

标签:param   blank   ring   mybatis   技术分享   shm   tle   char   password   

原文地址:http://www.cnblogs.com/flynnsky/p/7395343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!