学习Mybatis xml 常用关键语法

1.<where>和<if>

<!-- 根据条件查询用户 -->
<select id="queryUserByWhere" parameterType="user" resultType="user">
    SELECT id, username, birthday, sex, address FROM `user`
    WHERE 1=1
    <if test="sex != null and sex != ''">
        AND sex = #{sex}
    </if>
    <if test="username != null and username != ''">
        AND username LIKE
        '%${username}%'
    </if>
</select>

if 内部判断的是传递过来的实体参数中的字段,判断参数字段内容,如果为ture 执行if 内部命令

 

2.<include refid> 个人认为为标签索引

    <sql id="Base_Column_List" >
            collegeID, collegeName
    </sql> 
     
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
           select 
            <include refid="Base_Column_List" />
            from t_notification_template
           where id = #{id,jdbcType=BIGINT}
     </select>

内部select查询中可以使用索引标签

好处是将公共部分提出来,方便后续开发使用。

4.<foreach>   参数为list时,可使用

<select id="countByUserList" resultType="_int" parameterType="list">
select count(*) from users
  <where>
    id in
    <foreach item="item" collection="list" separator="," open="(" close=")" index="">
      #{item.id, jdbcType=NUMERIC}
    </foreach>
  </where>
</select>

 

posted @ 2020-09-15 13:22  Ivin-yang  阅读(995)  评论(0编辑  收藏  举报