MyBatis mapper.xml用法

假设有Product、Product Img表

public class Product {
    Long id;
    String name;
}

public class ProductImg {
    Long id;
    Long productId;
    String url;
}
  1. 基本<resultMap>
    <resultMap id="BaseResultMap" type="com.zyd.shiro.persistence.beans.Product">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
    </resultMap>
  2. Product中List<ProductImg>
    public class Product {
        Long id;
        String name;
        List<ProductImg> imgs;
    }
    <resultMap id="resultMapper1" type="com.zyd.shiro.persistence.beans.Product">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
    
        <collection property="imgs" column="img_id" javaType="ArrayList" ofType="java.lang.com.zyd.shiro.persistence.beans.ProductImg">
            <result property="id" jdbcType="BIGINT" column="img_id"/>
            <result property="url" jdbcType="VARCHAR" column="img_url"/>
        </collection>
    </resultMap>
    
    <!--对应的SQL-->
    <select id="xxx" resultMap="resultMapper1">
        select p.*,
               pi.id  img_id,
               pi.url img_url
        from product p
                 left join product_img pi on p.id = pi.product_id
    </select>
  3. Product中List<String>

    public class Product {
        Long id;
        String name;
        List<String> imgs;
    }
    <resultMap id="resultMapper1" type="com.zyd.shiro.persistence.beans.Product">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
    
        <collection property="imgs" ofType="java.lang.String" javaType="ArrayList">
            <result column="img_url" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>
    
    <!--对应的SQL-->
    <select id="xxx" resultMap="resultMapper1">
        select p.*,
               pi.id  img_id,
               pi.url img_url
        from product p
                 left join product_img pi on p.id = pi.product_id
    </select>
  4. insert动态sql

    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.zyd.shiro.persistence.beans.Product" useGeneratedKeys="true">
        insert into product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">
                `name`,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
  5. <if>中数组/集合的判断

    #集合
    <if test="arr != null and arr.size() > 0">
    #数组
    <if test="col != null and col .length > 0">
  6. 使用<foreach>
    
    select * from product where name in
    <foreach collection="arr" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
  7. 使用<trim>
     
    <if test="statusList != null and statusList.size() > 0">
        <trim prefix="(" suffix=")" prefixOverrides="and|or">
            <if test="statusList.contains('Indemand')">
                is_indemand = 1
            </if>
            <if test="statusList.contains('正在众筹')">
                or close_date > now()
            </if>
            <if test="statusList.contains('已结束')">
                or now() > close_date and is_indemand = 0
            </if>
        </trim>
    </if>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值