IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> Mybatis使用 -> 正文阅读

[大数据]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值。

二、删除(delete)

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

三、修改(update)?

  <update id="updateQuantityById">
        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">

?五、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="list"?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"/>
</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"? ? ? ? ? ? ? ? collection里存放的是遍历对象的类型 数组:arry, 集合:list

????????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;

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-11-05 00:34:29  更:2022-11-05 00:38:10 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年3日历 -2025/3/4 7:04:26-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码