mybatis bind _parameter等标签

1、UserMapper.xml 有一个 selectByUser 方法,这个方法用到了 like 查询条件,部分代码如下 。

<if test=” userNarne != null and userNarne ! = ””>
and user name like concat ( ’ 毛 ’, #{userNarne},’ 毡 ’ )
</if>

使用 concat 函数连接字符串

2、bind 标签可以使用 OGNL 表达式创建一个变量井将其绑定到上下文中

在 MySQL 中,这个函数支持多个参数,但在 Oracle 中只 
支持两个参数。由于不 同数据库之间的语法差异 ,如果更换数据库,有些 SQL 语句可能就需要 
重写。针对这种情况,可以使用 bind 标签来避免由于更换数据库带来的一些麻烦。将上面的 
方法改为 bind 方式后,代码如下。

<if test=userNarne != null and userNarne !=””>
<bind narne= "userNarneLikevalue = ”’草’+ userNarne + ’每’” /〉
and user name like #{userNarneLike}
</if>

bind 标签的两个属性都是必选项, name 为绑定到上下文的变量名, value 为 OGNL 表 
达式。创建一个 bind 标签的变量后 , 就可以在下面直接使用

3、使用 bind 拼接字符串不仅可 
以避免因更换数据库而修改 SQL,也能预防 SQL 注入。

如下以下的方式:

<if test=" deptname != null and deptname != '' ">
            	<bind name="pattern" value="'%' + _parameter.deptname + '%'" />
                and d.dept_name like #{pattern}
 </if> 
此处使用使用代表传入对象的deptaname绑定了pattern这个参数,可以有效的防止sql注入。

4、_parameter 参数取任意名称可以注入
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
 select      <include refid="Base_Column_List" />
 from base.tb_user
   <if test="_parameter != null">
      where id = #{id,jdbcType=INTEGER}  # where id=#{a,jdbcType=INTEGER}
   </if>
</select>
以上参数 id名称可能会变化但是写 _parameter就可以防止注入不到的情况。

5、 _parameter 两个对象注入时情况
List<User> select(User user,Page page) 时
if test一定要<if test='_parameter.get("0").name != null'>
(通过parameter.get(0)得到第一个参数即user);
where语句where name = #{0.name,jdbcType=CHAR}
(通过0.name确保第一个参数user的name属性值)
不用0,1也可以取名List<User> select(@param(user)User user,@param(page)Page page)

6、@Param注解的使用 类似于将参数重命名了一次

接口层代码以及xml代码如下:

List<String> selectIdBySortTime(@Param(value="startdate")String startDate);
<select id="selectIdBySortTime" resultType="java.lang.String" parameterType="java.lang.String">  
    select distinct ajlcid from ebd_fh_ajlc where sorttime >= 
        to_date(#{startdate,jdbcType=VARCHAR},'YYYY-MM-DD') and created_date=updated_date   
        and keyvalue in (select  distinct companyname from ebd_fh_company_list where isupdate='0')  
  </select>
别名和 mapping文件中的#{}中名称一致即可
List<String> selectIds(@Param(value="dateVo")DateVo dateVo);  
<select id="selectIds" resultType="java.lang.String" parameterType="com.api.entity.DateVo">  
    select distinct ajlcid from ebd_fh_ajlc where sorttime >= 
to_date(#{dateVo.startDate,jdbcType=VARCHAR},'YYYY-MM-DD') and created_date=updated_date   
    and keyvalue in (select  distinct companyname from ebd_fh_company_list where isupdate='0')  
 </select>  

7、 Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2] 遇到如下的错误的原因是:
在MyBatis3.4.4版以后如果需要注入多个参数不能直接使用#{0},#{1}要使用  #{arg0} #{arg0}
select u.user_name  from t_base_role   r  , t_base_userrole t , t_base_user  u  WHERE  r.role_name=#{arg0} 
	        AND  t.role_id=r.id 
	        AND  t.user_id=u.id
	        AND  u.dept_id=(select m.dept_id from t_base_user m where m.id=#{arg1});




猜你喜欢

转载自blog.csdn.net/zhangkang65/article/details/79637185