Mybatis知识点备忘录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rhx_1989/article/details/89015710

1.一对一关联查询

<association column="id" property="userInfo" javaType="java.util.Map"
             select="selectUserInfoById"/>

2.一对多关联查询

<collection column="id" property="workList" ofType="java.util.Map"
            select="selectWorkListUserId"/>

3.多参数关联查询

<collection column="{tAge=fAge,tSex=fSex}" property="userList" ofType="java.util.Map"
            select="selectUserList"/>

4.传递数组参数

<if test="ids!=null and ids.length!=0">
     WHERE id IN
    <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
        #{id}
    </foreach>
</if>

5.传递List集合参数

WHERE id IN
<foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
    #{id}
</foreach>
<if test="names!=null and names.size!=0">
    WHERE name IN(
    <foreach collection="names" item="name" index="index" separator=",">
        #{name}
    </foreach>
    )
</if>

6.定义通用sql语句

<sql id="search">
      WHERE age &gt; #{age} AND sex=#{sex}
</sql>

<select id="selectUserList" resultType="java.util.Map" parameterType="java.util.Map">
      SELECT *  FROM t_user  <include refid="search" />
</select>

<select id="selectUserCount" resultType="java.util.Map" parameterType="java.lang.Integer">
      SELECT COUNT(*)  FROM t_user  <include refid="search" />
</select>

猜你喜欢

转载自blog.csdn.net/rhx_1989/article/details/89015710