zoukankan      html  css  js  c++  java
  • mybatismapper.xml传参_动态sql

    一、mapper.xml传参

      1、根据getter和setter;

      2、根据Map的key;

      3、@Param("")注解;

    二、动态sql

    <if test="value!=null and value!=''">
        判断成功,sql语句存在;
        判断失败,sql语句不存在;
    </if>
    <choose>
        <when test="value!=null and value!=''">
            判断成功,执行该句
        </when>
        <otherwise>
            否则,执行该句
        </otherwise>
    </choose>
    <where>
        <if test="value!=null and value!=''">
            and/or要加上,开头的and/or会自动取消
        </if>
    </where>
    <set>
        <if test="value!=null">
            ,要加上,最后的,会自动取消
        </if>
    </set>
    <trim prefix="where" prefixOverrides="and | or" suffix=")" suffixOverrides=",">
        语句前加where,开头是and或or会取消,最后加),最后是,会自动取消
    </trim>
    <foreach collection="list" item="item" open="(" close=")" separator=",">
        #{item},一般用于in语句或添加多行数据
    </foreach>
    <sql id="field">
        id,name,age
    </sql>
    <select id="daoMethod">
        SELECT <include refid="field"></include>
        FROM tableName
    </select>
    #{}和${}区别:${}是做字符串拼接,#{}是?占位,#{}比${}安全

    三、级联一对多

    <resultMap type="com.zhihuibao580.beans.Clargeclass" id="largeClass">
        <id column="lid" property="id"/>
        <result column="lname" property="name"></result>
        <collection property="objSubclass" ofType="com.zhihuibao580.beans.Subclass">
            <id column="sid" property="id"/>
            <result column="sname" property="name"></result>
            <result column="slargeClass" property="largeClass"></result>
            <result column="sicon" property="icon"></result>
        </collection>
    </resultMap>
    <select id="selectCommodityClass" resultMap="largeClass">
        select l.id lid,l.name lname,
        s.id sid,s.name sname,s.largeClass slargeClass,s.icon sicon 
        from c_largeclass l,subclass s 
        where l.id=s.largeClass
    </select>

    四、级联一对一

    <resultMap type="com.zhihuibao580.beans.Subclass" id="mySubclass">
        <id column="sid" property="id"/>
        <result column="sname" property="name"/>
        <result column="slargeClass" property="largeClass"/>
        <result column="sicon" property="icon"/>
        <association property="clargeclass" javaType="com.zhihuibao580.beans.Clargeclass">
            <id column="lid" property="id"/>
            <result column="lname" property="name"/>
        </association>
    </resultMap>
    <select id="adminSelectSubclass" resultMap="mySubclass">
        select s.id sid,s.name sname,s.largeClass slargeClass,s.icon sicon,
        l.id lid,l.name lname
        from c_largeclass l,subclass s
        where s.largeClass=l.id
    </select>

    五、递归查询

    <select id="findByArticleId" resultMap="commentResultMap">
        SELECT * FROM dcy_comment WHERE parent_id = -1
        AND article_id = #{articleId}
        ORDER BY create_date DESC
    </select>
    <select id="findByParentId" resultMap="commentResultMap">
        SELECT * FROM dcy_comment WHERE parent_id = #{id}
    </select>
    <resultMap id="commentResultMap" type="com.dingcaiyan.blog.entities.Comment">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="user_id" property="userId"/>
        <result column="nick_name" property="nickName"/>
        <result column="user_image" property="userImage"/>
        <result column="article_id" property="articleId"/>
        <result column="content" property="content"/>
        <result column="create_date" property="createDate"/>
        <collection property="children" javaType="list" ofType="com.dingcaiyan.blog.entities.Comment" column="id"
                    select="findByParentId"/>
    </resultMap>
  • 相关阅读:
    python3----练习......
    python3----练习题(....)
    python3----requests
    python3----练习题(爬取电影天堂资源,大学排名,淘宝商品比价)
    python3----练习题(图片转字符画)
    python3----练习题(....)
    ConfigParser 读写配置文件
    数据驱动ddt+excel数据读取
    数据驱动ddt
    expected_conditions判断页面元素
  • 原文地址:https://www.cnblogs.com/linding/p/12616249.html
Copyright © 2011-2022 走看看