zoukankan      html  css  js  c++  java
  • [Java] 使用@SelectProvider注解实现多表关联查询(全注解,不使用不配置xml)

    使用mybatisplus可以很容易实现不写SQL语句的CURD。也可以通过配置XML或注解实现多表关联查询,但这就得写相应的SQL语句了。我更喜欢全程注解的方式,必竟写着java忽然又跑去配置xml有点跳跃,既使是有相应的插件方便跳转,但还是不如写在一个文件中直接,后期也方便检视代码。

    这里主要是使用 @SelectProvider 实现我们想要的功能。

    @SelectProvider是声明在Mapper中方法上的。

    参数 type 用来指定SQL生成器类, method 用来指定生成SQL对应的函数名称。

    示例代码:

    import xxx.entity.AccountEntity
    import org.apache.ibatis.annotations.Mapper
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import org.apache.ibatis.annotations.Param import org.apache.ibatis.annotations.SelectProvider import org.springframework.stereotype.Component @Component @Mapper interface AccountMapper : BaseMapper<AccountEntity> { /** * 获取账号列表, 注意,这里我们用的是 ArrayList,不能用 List, 因为是个虚类无法实例化 */ @SelectProvider(type = AccountSQLProvider::class, method = "getAccountList") fun <E : ArrayList<AccountEntity>> getAccountList(@Param("id") idArray: String): E /** * sql拼接处理 */ class AccountSQLProvider { fun getAccountList(id: String):String { return """ SELECT a.user_id, a.shop_id, b.name FROM account_user a LEFT Join shop b ON a.shop_id_id = b.id WHERE a.user_id in $id """.trimIndent() } } }

    Mapper建议用Kotlin来写,可以更方便的写sql语句。

  • 相关阅读:
    有趣的项目链接分享
    Nano Pi安装Opencv-python
    Opencv
    安装库的步骤
    EC20 AT指令
    最详细树莓派/NanoPi网线和电脑直接连接方法
    在Altium Designer中如何将一组器件合并为一个?
    Altium Designer如何快速查找PCB板上元件
    pip升级之后,不能使用
    利用opencv-python(cv2)查看设备连接摄像头的数量
  • 原文地址:https://www.cnblogs.com/yangyxd/p/13572061.html
Copyright © 2011-2022 走看看