MyBatis其二

108 阅读2分钟

动态sql

  • where标签:

    ①当where代码块中的条件都不成立的时候, where代码块不生效

    ②当where代码块中的条件至少有一个成立的时候 在代码块之前加入一个where关键字 当代码块以and|or开头的时候,它会剔除第一个and|or

  • if标签:相当于java的条件判断,成立,就拼接sql语句

SELECT * FROM USER
        <where>
            <if test="id!=null" >
               and id = #{id}
            </if>
            <if test="username!=null" >
                and username = #{username}
            </if>
            <if test="sex!=null" >
                and sex = #{sex}
            </if>
            <if test="address!=null" >
                and address = #{address}
            </if>
        </where>
  • set标签:用于更新操作的标签。

    ① 在set代码块之前添加一个SET关键字

    ② 去掉set代码块之间的最后一个,

    ③ set代码块中至少要有一个条件成立

UPDATE `user` SET
<if test="username!=null">
    `username`=#{username},
</if>
<if test="sex!=null">
    `sex`=#{sex},
</if>
<if test="address!=null">
    `address`=#{address}
</if>
WHERE `id`=#{id};
  • foreach标签:循环遍历标签。适用于多个参数或者的关系。

    属性:

      collection:参数容器类型,(list-集合,array-数组,属性名-实体)。
      open:开始的 SQL 语句。
      close:结束的 SQL 语句。
      item:参数变量名。
      separator:分隔符。
    
SELECT * FROM USER WHERE id in
<foreach collection="list" item="thisId" open="(" close=")" separator=",">
    #{thisId}
</foreach>
  • sql片段抽取:将一些重复性的 SQL 语句进行抽取,以达到复用的效果。

    sql:抽取 SQL 语句标签。

    include:引入 SQL 片段标签。

<sql id="allFields">
    u.id uid,username,sex,address,r.id rid,role_name,role_desc
</sql>
 SELECT <include refid="allFields"></include>
        FROM role r LEFT JOIN user_role ur ON r.id=ur.rid
        LEFT JOIN `user` u ON ur.uid=u.id

多表查询

对一查询

  • resultMap:配置字段和对象属性的映射关系标签。

       id 属性:唯一标识
    
       type 属性:实体对象类型
    
  • id:配置主键映射关系标签。

  • result:配置非主键映射关系标签。

       column 属性:表中字段名称
    
       property 属性: 实体对象变量名称
    
  • association:配置被包含对象的映射关系标签。

       property 属性:被包含对象的变量名
    
       javaType 属性:被包含对象的数据类型
    
<!--自定义结果集映射-->
<resultMap id="TblAccountResult" type="com.eponine.domain.TblAccount">
    <id column="aid" property="aid"></id>
    <result column="money" property="money"></result>
    <result column="address" property="address"></result>
    <result column="aname" property="aname"></result>
    <result column="uid" property="uid"></result>
    <association property="user" javaType="com.eponine.domain.TblUser">
        <id column="id" property="id"></id>
        <result column="username" property="username"></result>
        <result column="password" property="password"></result>
        <result column="sex" property="sex"></result>
        <result column="email" property="email"></result>
        <result column="telephone" property="telephone"></result>
    </association>
</resultMap>
<!--对一-->
<select id="selectAll" resultMap="TblAccountResult">
    SELECT * FROM tblaccount a LEFT JOIN tbluser u ON u.id=a.uid
</select>

对多查询

  • resultMap:配置字段和对象属性的映射关系标签。

       id 属性:唯一标识
    
       type 属性:实体对象类型
    
  • id:配置主键映射关系标签。

  • result:配置非主键映射关系标签。

       column 属性:表中字段名称
    
       property 属性: 实体对象变量名称
    
  • collection:配置被包含集合对象的映射关系标签。

       property 属性:被包含集合对象的变量名
    
       ofType 属性:集合中保存的对象数据类型
    
<!--resultMap:自定义结果集映射-->
<resultMap id="TblUserResult" type="com.eponine.domain.TblUser">
    <!--主键映射使用id标签,其他字段使用result标签
        colum:数据库查询出来的字段名
        property:实体类中的成员变量
        collection:一对多的时候关联字段使用,表示一个集合
        association:多对一的时候关联字段使用,表示一个对象
    -->
    <id column="id" property="id"></id>
    <result column="username" property="username"></result>
    <result column="password" property="password"></result>
    <result column="sex" property="sex"></result>
    <result column="email" property="email"></result>
    <result column="telephone" property="telephone"></result>
    <collection property="accounts" ofType="com.eponine.domain.TblAccount">
        <id column="aid" property="aid"></id>
        <result column="money" property="money"></result>
        <result column="address" property="address"></result>
        <result column="aname" property="aname"></result>
        <result column="uid" property="uid"></result>
    </collection>
</resultMap>
<!--使用自己定义的结果集映射-->
<select id="selectAll" resultMap="TblUserResult">
    SELECT id,username,password,sex,email,telephone,aid,money,address,aname,uid
    FROM tbluser u LEFT JOIN tblaccount a ON u.id=a.uid
</select>

嵌套查询

<!--嵌套查询映射-->
<resultMap id="TbluserResultNested" type="com.eponine.domain.TblUser">
    <id column="id" property="id"></id>
    <result column="username" property="username"></result>
    <result column="password" property="password"></result>
    <result column="sex" property="sex"></result>
    <result column="email" property="email"></result>
    <result column="telephone" property="telephone"></result>
    <collection column="id" property="accounts"
                select="com.eponine.dao.TblAccountMapper.selectAccountById"
    ></collection>
</resultMap>
<!--嵌套查询(二段查询)-->
<select id="selectAllNested" resultMap="TbluserResultNested">
    select id,username,password,sex,email,telephone from tbluser
</select>
<!--根据id查-->
<select id="selectAccountById" parameterType="java.lang.Integer"
        resultType="com.eponine.domain.TblAccount">
    SELECT * FROM tblaccount WHERE uid = #{value}
</select>