Mybatis标签的使用

一、新增(insert)

 <insert id="saveCart" useGeneratedKeys="true" keyProperty="id">
        insert into mall_oms.oms_cart(
                user_id,
                sku_id,
                title,
                main_picture,
                price,
                quantity)
        values (#{userId},
                #{skuId},
                #{title},
                #{mainPicture},
                #{price},
                #{quantity})
 </insert>

        id="mapper层对应的名字"

        useGeneratedKeys="true"开启id键生成  keyProperty="id" 生成id键,执行此sql语句会返回一个id值。

        1.1多条记录插入

   Mapper映射

void saveProjectCoord(List<ProjectCoord> ProjecList);

 SQL语句 

 <insert id="saveProjectCoord">
		INSERT INTO map_coord(
		longitude,
		latitude,
		project_id
		) VALUES
		<foreach collection="ProjecList" separator="," item="list">
			(#{list.longitude},#{list.latitude},#{list.projectId})
		</foreach>
	</insert>

        注意:values后面的括号, collection中存放的是前端传来的值的名称 ProjecList

二、删除(delete)

  <delete id="removeAllCarts">
        delete
        from mall_oms.oms_cart
        where user_id = #{userId}
  </delete>

三、修改(update) 

  <update id="updateQuantityById" parameterType="xxxxxx">
        update mall_oms.oms_cart
        set quantity=#{quantity}
        where id = #{id}
  </update>

四、查询(select)

<select id="selectExistsCart" resultMap="BaseResultMap">
        select
        <include refid="SimpleQueryFields"/>
        from
        mall_oms.oms_cart
        where user_id=#{userId}
        and sku_id=#{skuId}
</select>

 reultMap可以替换为resultType

 <select id="selectExistsCart" resultType="cm.mall.pojo.order.vo.CartStandardVO">

        模糊查询 

<if test="wbsName != null and wbsName != ''">
	and a.wbs_name like concat( '%', #{wbsName},'%')
</if>

 五、reultMap设置Mybatis的关系映射

 <resultMap id="OrderListMapper" type="cn.tedu.mall.pojo.order.vo.OrderListVO">
        <id column="id" property="id"/>
        <result column="sn" property="sn"/>
        <result column="user_id" property="userId"/>
        <result column="contact_name" property="contactName"/>

        <!--当前实体类中有 集合 类型属性,要使用collection标签映射 -->
        <collection property="orderItems"
 ofType="cn.tedu.mall.pojo.order.vo.OrderItemListVO">
            <!--property:指定实体类中映射为集合类型的属性名
            Javatype:指定当前集合的类型,默认List类型
            ofType:指定当前集合泛型的类型
            column:列属性-->
            <id column="ooi_id" property="id"/>
            <result column="order_id" property="orderId"/>
            <result column="sku_id" property="skuId"/>
            <result column="title" property="title"/>
        </collection>

</resultMap>

public class OrderListVO implements Serializable {

private List<OrderItemListVO> orderItems;

}

        当前实体类中有 集合 类型属性,要使用collection标签映射 
 <collection property="orderItems"  javaType="listofType ="cn.tedu .mall.pojo.order .vo. OrderItemListVO">
            property:指定实体类中集合的属性名
            javatype:指定当前集合的类型,默认List类型
            ofType:指定当前集合泛型的类型
            column:列属性
            <id column="ooi_id" property="id"/>
            <result column="order_id" property="orderId"/>
</collection>

六、设置SQL语句片段

 <sql id="SimpleQueryFields">
        <if test="true">
            id,
            user_id,
            sku_id,
            title,
            main_picture,
            price,
            quantity
        </if>
 </sql>

        使用时用  <include refid="SimpleQueryFields"/>标签

         refid="设置的片段名"

七、遍历<foreach>

       利用mapper层传来的一个数组,遍历数组内的id来删除多个数据库记录。

public interface OmsCartMapper { 
    int deleteCartsByIds(Long[] ids);
}
<delete id="deleteCartsByIds">
        delete
        from mall_oms.oms_cart
        where
        id in
        <foreach collection="array" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
</delete>

        collection="array"                要迭代循环的属性名

        item="id"                              作用为给遍历的对象起个别名,不一定为id

        separator=","                        在in中每个元素都必须用逗号隔开  in(id1id2id3.......

        open="(" close=")"                in 的元素需要括号括起来  inid1,id2,id3.......

 八、动态SQL语句

 <update id="updateOrderById">
        update mall_oms.oms_order
        <set>
            <if test="contactName!=null">
                contact_name=#{contactName}
            </if>
            <if test="state!=null">
                state=#{state}
            </if>
            <if test="tag!=null">
                tag=#{tag}
            </if>
        </set>
</update>

        if标签可以用来对传给执行的SQL做判断是否为空值,而判断是否执行

        <set></set>标签可以不用写逗号

九、Mybatis中 < 小于号不能使用的问题

        在Mybatis中需要做判断时使用比较符会报错因为含有歧义

        需要写成下面这个样子 

 <select id="selectTimes" resultMap="OrderListMapper">
        select oo.id, 
        where user_id = #{userId}
          and oo.gmt_create &gt; #{startTime}
          and oo.gmt_create &lt; #{endTime}
        order by oo.gmt_modified;
 </select>

        在Mybatis中 > 转义为 &gt;    在Mybatis中 < 转义为 &lt;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值