分类 数据库 下的文章

mybatis系列教程:单表数据操作

以文章列表为例。

文章表结构设计

id :编号
article_title :标题
article_content :内容
article_type :文章类型
................:略了几个字段
代码

1:Controller

1.1 查询 List<DrArticles> list = drArticlesService.selectDrArticlesList(drArticles);

2:Bean

@Data
public class DrArticles extends BaseEntity
{
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String articleTitle;
    private String articleType;
    private String articleContent;
    private String createNickname;
}

3:Service

4:Mapper

5:mybatis xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.system.mapper.DrArticlesMapper">
    
    <resultMap type="DrArticles" id="DrArticlesResult">
        <result property="id"    column="id"    />
        <result property="articleTitle"    column="article_title"    />
        <result property="articleType"    column="article_type"    />
        <result property="articleContent"    column="article_content"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="createNickname"    column="create_nickname"    />
    </resultMap>

    <sql id="selectDrArticlesVo">
        select id, article_title, article_type, article_content, create_by, create_time, update_by, update_time, remark, create_nickname from dr_articles
    </sql>

    <select id="selectDrArticlesList" parameterType="DrArticles" resultMap="DrArticlesResult">
        <include refid="selectDrArticlesVo"/>
        <where>  
            <if test="articleTitle != null  and articleTitle != ''"> and article_title = #{articleTitle}</if>
            <if test="articleType != null  and articleType != ''"> and article_type = #{articleType}</if>
            <if test="articleContent != null  and articleContent != ''"> and article_content = #{articleContent}</if>
            <if test="createNickname != null  and createNickname != ''"> and create_nickname like concat('%', #{createNickname}, '%')</if>
        </where>
    </select>
    <select id="selectDrArticlesById" parameterType="Integer" resultMap="DrArticlesResult">
        <include refid="selectDrArticlesVo"/>
        where id = #{id}
    </select>  
    <insert id="insertDrArticles" parameterType="DrArticles" useGeneratedKeys="true" keyProperty="id">
        insert into dr_articles
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="articleTitle != null">article_title,</if>
            <if test="articleType != null">article_type,</if>
            <if test="articleContent != null">article_content,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="createNickname != null">create_nickname,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="articleTitle != null">#{articleTitle},</if>
            <if test="articleType != null">#{articleType},</if>
            <if test="articleContent != null">#{articleContent},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createNickname != null">#{createNickname},</if>
         </trim>
    </insert>
    <update id="updateDrArticles" parameterType="DrArticles">
        update dr_articles
        <trim prefix="SET" suffixOverrides=",">
            <if test="articleTitle != null">article_title = #{articleTitle},</if>
            <if test="articleType != null">article_type = #{articleType},</if>
            <if test="articleContent != null">article_content = #{articleContent},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createNickname != null">create_nickname = #{createNickname},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDrArticlesById" parameterType="Integer">
        delete from dr_articles where id = #{id}
    </delete>

    <delete id="deleteDrArticlesByIds" parameterType="String">
        delete from dr_articles where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>

技术依赖:如无特殊说明;所有配置和技术选型均采用Ruoyi-vue快速开发框架

    SpringBoot
    Mybatis
    Mysql 
    vue-element ui

说明:一个订单中有多种商品;所以有一个订单表有一个订单详情表

网图说明问题:

20210820131452137.png

表结构设计:
订单表:order

     order_id 订单表ID
     

订单详情表:order_items

    id :详情表ID
    order_id:订单表ID
    goods_id:商品ID

增加订单:
Controller层代码:

mybatis xml的写法

collection主要是应对表关系是一对多的情况
property:属性
javaType:

<resultMap>
     <result property="orderId"    column="order_id"    />
     <result property="goodsAmount"    column="goods_amount"    />
     <collection property="orderItems" column="dept_id" javaType="java.util.List" resultMap="orderItemsResult" />
</resultMap>

<resultMap id="YshopOrderItemsResult" type="orderItems">
    <id  property="id"  column="id" />
    <result property="goodsId" column="goods_id"/>
    <result property="price" column="price"/>
</resultMap>

也可以这样写

 <resultMap type="YshopGoodsType" id="YshopGoodsTypeResult">
    <result property="id"    column="id"    />
    <result property="name"    column="name"    />
    <result property="params"    column="params"    />
    <collection property="students" ofType="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <result property="tid" column="tid"/>
    </collection>
    <collection property=""></collection>
</resultMap>

SQL语句

select * from order left join order_items where order.id=order_items.order_id;

方式二:mybatis 一对多查询方式二

相关文章:

    《事务的处理》
     https://blog.csdn.net/weixin_46645338/article/details/123987406
     https://blog.csdn.net/weixin_45877245/article/details/119821218

[1]:

大家都知道,计算机的瓶颈之一就是IO,为了解决内存与磁盘速度不匹配的问题,产生了缓存,将一些热点数据放在内存中,随用随取,降低连接到数据库的请求链接,避免数据库挂掉。需要注意的是,无论是击穿还是后面谈到的穿透与雪崩,都是在高并发前提下,比如当缓存中某一个热点key失效。

redis缓存击穿

是指某一个非常热点的key (即在客户端搜索的比较多的关键字)突然失效了,这时从客户端发送的大量的请求在redis里找不到这个key,就会去数据里找,最终导致数据库压力过大崩掉

select product_id,survey_date+interval '1 DAYS'

 select * from (

    select *,

    case when last_week_avg_price=0 then 0 else (this_week_avg_price-last_week_avg_price)/last_week_avg_price*100 end  ringvalue,
    (select sum(cast(this_day_trading as double)) from dr_information_items  where good_id=v.good_id and date_week=to_char(to_date(#{surveyDateWeek},'yyyy-MM-dd')+interval '-7 DAYS', 'yyyy-mm-dd') group by good_id,date_week) lastWeekTrading
    from
    (

    SELECT
    min(dr.id) id,
    items.cateid AS class_code,
    min(items.good_name) AS class_name,
    min(items.good_id) AS good_id,
    min(items.product) AS product,
    sum(items.this_day_count_price) AS this_week_price,
    sum(cast(items.this_day_trading as double)) AS this_day_trading,
    case when sum(cast(items.this_day_trading as double))=0 then avg(items.last_week_avg_price)  else  sum(items.this_day_count_price)/sum(cast(items.this_day_trading as double)) end this_week_avg_price ,
    avg(items.last_week_avg_price) AS last_week_avg_price,
    min(items.unit) AS unit,
    max(u.user_name) shop_number
    FROM dr_information_items items
    left JOIN dr_information dr ON dr.id = items.infomationid
    left JOIN sys_user u on LTrim(RTrim(items.create_by))=cast(LTrim(RTrim(u.user_name)) as text)
    WHERE  items.date_week IS NOT NULL AND dr.status =#{status}
    <if test="marketId != null  and marketId != ''">
        and dr.market_id=#{marketId}
    </if>
    <if test="surveyDateWeek != null">
        and to_date(items.date_week,'YYYY-MM-DD')=#{surveyDateWeek}
    </if>
    GROUP BY items.date_week, items.cateid
    ) v
    ) t where 1=1
    <if test="classCode != null">
        and LEFT(class_code,4)=#{classCode}
    </if>
    <if test="ringvalue != null">
        and ringvalue <![CDATA[ >= ]]> #{ringvalue}
    </if>

    UNION

    select  id,class_id class_code,name class_name,'' good_id,'' product,null this_week_price,null this_day_trading,null this_week_avg_price,null last_week_avg_price,'' unit,'' shop_number,null ringvalue,null lastWeekTrading

    from dr_lybase_class where  LEFT(class_id,4)=#{classCode}  and LENGTH(class_id)=8 and  LEFT(class_id,8)  in (select LEFT(cateid,8) from   dr_information_items
     where  to_date(date_week,'YYYY-MM-DD')=#{surveyDateWeek})

    order by class_code