欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mybatis知识点备忘录

程序员文章站 2024-03-17 15:42:58
...

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>