zoukankan      html  css  js  c++  java
  • @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>
  • 相关阅读:
    Python单元测试之unittest基础
    linux--硬链接和软链接
    12-8 istio核心功能实战-----可观察性(程访问遥测插件)
    12-7 istio核心功能实战-----可观察性(网络可视化)
    12-6 istio核心功能实战-----可观察性(分布式追踪)
    12-3 部署面向生产的istio-----核心组件
    12 ServiceMesh代表作istio-----12-1 ServiceMes、Istio架构原理
    11-7 Grafana看板和邮件报警
    11-6 监控落地
    11-4 部署前奏-Helm&Operator
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/6100123.html
Copyright © 2011-2022 走看看