mybatis日常语法有哪些

mybatis日常语法有哪些,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

mybatis中的#和$的区别? 

#方式能够很大程度防止sql注入。参数有'';
$方式无法防止Sql注入。无''原样赋值;一般用于传入数据库对象,例如传入表名.字段名,sql语句等

<![CDATA[ ]]>关键标签

标明是纯文本的,没有这个的话 <  >  & 字符是不能直接存入XML的,需要转义,而用这个标记则不需要转义而将这些符号存入XML文档。

可以避免未预料的特殊符号导致XML解析出错。

sql语句块

<sqlid="selectBasic"> 
  select * from t 
</sql>
<select id="selectLikeName" parameterType="string" resultType="User" > 
  <include refid="selectBasic"/>
  where name like #{name}
</select>

动态sql标签

<!-- if标签 -->
<if test=""></if>

<!-- choose 与或非标签-->
<choose>
<when test="dateType == 'WEEK' "></when>
<otherwise>
  //TODO something
</otherwise>
</choose>

<!-- in 常用foreach 循环; item 节点对象 -->
<foreach item="item" collection="status" separator="," open="(" close=")" index="index">  
  #{item, jdbcType=TINYINT}
</foreach> 

<!-- 主键自增,同时赋值给传入po类的主键id值 -->
<insert id="insertRole" parameterType="role" useGeneratedKeys="true" keyProperty="id">
   insert into t_role(role_name,note) values (#{roleName},#{note})
</insert>

<!-- 便捷的trim标签 trim代替where/set标签-->
<!-- 使用 if/trim 代替 where(判断参数) - 将 User 类不为空的属性作为 where 条件 -->   
select * from a    
<trim prefix="WHERE" prefixOverrides="AND|OR">  
        <if test="username !=null ">  
            u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%')  
        </if>   
    </trim>    
<!-- if/trim代替set(判断参数) - 将 User 类不为空的属性更新 -->   
update a 
<trim prefix="SET" suffixOverrides=","></trim>

看完上述内容,你们掌握mybatis日常语法有哪些的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!