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语句。

  • 相关阅读:
    [转]VC++ ^和gcnew
    OPPM 一页纸项目管理 OnePage Project Management
    [转]基础总结篇之五:BroadcastReceiver应用详解 .
    [转]深入浅出Java设计模式之备忘录模式
    [转]面向对象的5条基本设计原则
    [转]UED大全
    [转]VC++动态链接库(DLL)编程深入浅出(zz)
    只有壮年时的不遗余力 才能支撑一生的坎坷与幸福
    [书目20121024]当责 AccountaBility
    node.js入门
  • 原文地址:https://www.cnblogs.com/yangyxd/p/13572061.html
Copyright © 2011-2022 走看看