java的注解方式和xml方式这两种方式对数据库进行操作详解

首先需要引入mybatisplus包

<dependency>
	<groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.1.1</version>
</dependency>

第一种注解方式:参数是通过#{}来接收的

public interface ApArticleMapper extends BaseMapper<ApArticle> {

    @Select("SELECT apa.* from ap_article apa LEFT JOIN ap_article_config aac on apa.id = aac.article_id where " +
            "aac.is_delete=0 and aac.is_down=0 and apa.channel_id=2 and publish_time>#{startTime}")
    List<ApArticle> selectRedisVoList(LocalDateTime startTime);
}

调用:

apArticleMapper.selectRedisVoList(startTime);

第二种xml方式:

public interface ApArticleMapper extends BaseMapper<ApArticle> {
    List<ApArticle> pageByOrder(@Param(value="start") Long start, @Param(value="size")Long size, @Param(value="channelId")Integer channelId);
}

xml文件应配置在和mapper平级的resources目录下或者修改yml中mybatis-plus的mapper映射路径

yml中修改如下(mybatis-plus和spring平级):
mybatis-plus:
  mapper-locations: classpath*:mapper/*.xml



xml文件中的内容:注意namespace要指定到需要配置的mapper上
<mapper namespace="com.jjw.article.mapper.ApArticleMapper">

    <select id="pageByOrder" resultType="com.jjw.article.pojo.ApArticle" parameterType="map">
        SELECT apa.* FROM ap_article apa LEFT JOIN ap_article_config aac on apa.id = aac.article_id
        <where>
            <if test="channelId != null and channelId != 0">
                and apa.channel_id=#{channelId}
            </if>
            and aac.is_delete=0
            and aac.is_down=0
        </where>
        order by publish_time DESC
        limit #{start},#{size}
    </select>
</mapper>

结构如下:
在这里插入图片描述

调用方式:

apArticleMapper.pageByOrder(start,size,body.getChannelId());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值