MyBatis3 传递参数种类分析

熊孩纸 阅读:184 2021-03-31 16:56:51 评论:0

第一种:单个参数[基本类型]:

# Mapper 接口定义 
T selectByPrimaryKey(String sid); 
 
# Mapper 映射文件 
 <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap"> 
    select  
    <include refid="Base_Column_List" /> 
    from *** 
    where sid = #{sid,jdbcType=VARCHAR} 
  </select>

第二种:多个参数

# Mapper 接口定义 
public List<T> getList(String sid, String code); 
 
# Mapper 映射文件 
<select id="getList" resultType="T"> 
  select * from ** where sid = #{0} and code= #{1}   
</select>   

第三种:Map参数

# Mapper 接口定义 
List<T> selectAll(Map<String,Object> parame); 
 
# Mapper 映射文件 
<select id="selectAll" parameterType="map" resultMap="BaseResultMap"> 
  	select  
    <include refid="Base_Column_List" /> 
    from cm 
    where 1 = 1 
       <if test="id != null"> 
            and  cm.id = #{id,jdbcType=DECIMAL} 
        </if> 
        <if test="carDeptName != null"> 
            and  cm.car_dept_name = #{carDeptName,jdbcType=VARCHAR} 
        </if> 
        <if test="carMakerName != null"> 
            and  cm.car_maker_name = #{carMakerName,jdbcType=VARCHAR} 
        </if> 
        <if test="hotType != null" > 
           and  cm.hot_type = #{hotType,jdbcType=BIGINT} 
        </if> 
 
  </select>

第四种:类参数

 <update id="updateByPrimaryKeySelective" parameterType="com.system.model.User" > 
    update user 
    <set > 
      <if test="carDeptName != null" > 
        car_dept_name = #{carDeptName,jdbcType=VARCHAR}, 
      </if> 
      <if test="carMakerName != null" > 
        car_maker_name = #{carMakerName,jdbcType=VARCHAR}, 
      </if> 
      <if test="icon != null" > 
        icon = #{icon,jdbcType=VARCHAR}, 
      </if> 
      <if test="carMakerPy != null" > 
            car_maker_py = #{carMakerPy,jdbcType=VARCHAR}, 
      </if> 
      <if test="hotType != null" > 
            hot_type = #{hotType,jdbcType=BIGINT}, 
      </if> 
    </set> 
    where id = #{id,jdbcType=BIGINT} 
  </update>

第五种:List参数

  <delete id="batchdelete" parameterType="java.util.List"> 
  	delete from user_relation_role where SID in  
  	<foreach item="sid" collection="list" open="(" separator="," close=")"> 
    	#{sid} 
    </foreach> 
  </delete>

第七种:@Param参数

# Mapper 接口定义 
List<String> selectIdBySortTime(@Param(value="startdate")String startDate); 
 
# Mapper 映射文件 
 <select id="selectIdBySortTime" resultType="java.lang.String" parameterType="java.lang.String"> 
  	select distinct *** from *** where sorttime >= to_date(#{startdate,jdbcType=VARCHAR},'YYYY-MM-DD') and created_date=updated_date  
  	and keyvalue in (select  distinct companyname from *** where isupdate='0') 
   </select>

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号