• Mybatis使用


    一、新增(insert)

    1. <insert id="saveCart" useGeneratedKeys="true" keyProperty="id">
    2. insert into mall_oms.oms_cart(
    3. user_id,
    4. sku_id,
    5. title,
    6. main_picture,
    7. price,
    8. quantity)
    9. values (#{userId},
    10. #{skuId},
    11. #{title},
    12. #{mainPicture},
    13. #{price},
    14. #{quantity})
    15. insert>

            id="mapper层对应的名字"

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

            1.1多条记录插入

       Mapper映射

    void saveProjectCoord(List ProjecList);

     SQL语句 

    1. <insert id="saveProjectCoord">
    2. INSERT INTO map_coord(
    3. longitude,
    4. latitude,
    5. project_id
    6. ) VALUES
    7. <foreach collection="ProjecList" separator="," item="list">
    8. (#{list.longitude},#{list.latitude},#{list.projectId})
    9. </foreach>
    10. </insert>

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

    二、删除(delete)

    1. <delete id="removeAllCarts">
    2. delete
    3. from mall_oms.oms_cart
    4. where user_id = #{userId}
    5. delete>

    三、修改(update) 

    1. <update id="updateQuantityById" parameterType="xxxxxx">
    2. update mall_oms.oms_cart
    3. set quantity=#{quantity}
    4. where id = #{id}
    5. update>

    四、查询(select)

    1. <select id="selectExistsCart" resultMap="BaseResultMap">
    2. select
    3. <include refid="SimpleQueryFields"/>
    4. from
    5. mall_oms.oms_cart
    6. where user_id=#{userId}
    7. and sku_id=#{skuId}
    8. select>

     reultMap可以替换为resultType

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

            模糊查询 

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

     五、reultMap设置Mybatis的关系映射

    1. <resultMap id="OrderListMapper" type="cn.tedu.mall.pojo.order.vo.OrderListVO">
    2. <id column="id" property="id"/>
    3. <result column="sn" property="sn"/>
    4. <result column="user_id" property="userId"/>
    5. <result column="contact_name" property="contactName"/>
    6. <collection property="orderItems"
    7. ofType="cn.tedu.mall.pojo.order.vo.OrderItemListVO">
    8. <id column="ooi_id" property="id"/>
    9. <result column="order_id" property="orderId"/>
    10. <result column="sku_id" property="skuId"/>
    11. <result column="title" property="title"/>
    12. collection>
    13. resultMap>

    public class OrderListVO implements Serializable {

    private List<OrderItemListVO> orderItems;

    }

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

    六、设置SQL语句片段

    1. <sql id="SimpleQueryFields">
    2. <if test="true">
    3. id,
    4. user_id,
    5. sku_id,
    6. title,
    7. main_picture,
    8. price,
    9. quantity
    10. if>
    11. sql>

            使用时用  SimpleQueryFields"/>标签

             refid="设置的片段名"

    七、遍历

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

    1. public interface OmsCartMapper {
    2. int deleteCartsByIds(Long[] ids);
    3. }
    1. <delete id="deleteCartsByIds">
    2. delete
    3. from mall_oms.oms_cart
    4. where
    5. id in
    6. <foreach collection="array" item="id" separator="," open="(" close=")">
    7. #{id}
    8. foreach>
    9. delete>

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

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

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

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

     八、动态SQL语句

    1. <update id="updateOrderById">
    2. update mall_oms.oms_order
    3. <set>
    4. <if test="contactName!=null">
    5. contact_name=#{contactName}
    6. if>
    7. <if test="state!=null">
    8. state=#{state}
    9. if>
    10. <if test="tag!=null">
    11. tag=#{tag}
    12. if>
    13. set>
    14. update>

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

            标签可以不用写逗号

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

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

            需要写成下面这个样子 

    1. <select id="selectTimes" resultMap="OrderListMapper">
    2. select oo.id,
    3. where user_id = #{userId}
    4. and oo.gmt_create > #{startTime}
    5. and oo.gmt_create < #{endTime}
    6. order by oo.gmt_modified;
    7. select>

            在Mybatis中 > 转义为 >    在Mybatis中 < 转义为 <

  • 相关阅读:
    将对象从3D坐标系转换为2D显示空间(立方体)
    【Unity的 Built-in 渲染管线下实现好用的GUI模糊效果_Blur_案例分享(内附源码)】
    wpf触发与模板的使用示例:批量生产工具
    怎样将mysql中的数据导入redis
    C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.5 数组和指针的其他区别
    安卓开发--如何将onPostExecute方法结果返回给调用方?
    Python爬虫实战案例——第五例
    猿创征文|【Python数据科学快速入门系列 | 05】常用科学计算函数
    物联网网关硬件和云端分别实现了哪些功能?-天拓四方
    优秀商业程序员的基础
  • 原文地址:https://blog.csdn.net/weixin_51722520/article/details/127676324