首頁 > 軟體

mybatis-plus實現自定義SQL、多表查詢與多表分頁查詢語句範例

2022-09-18 22:00:30

前言

本文介紹了在mybatis-plus中如何實現:自定義SQL語句,多表查詢語句,多表分頁查詢語句

在說怎麼實現之前我們要先明白一個概念,就是mybatis-plus是在mybatis的基礎上進行增強,並不做改變,所以mybatis的操作在mybatis-plus中也是一樣可以使用的,咱們直接上程式碼

1、自定義SQL

在mapper中自定義一個方法即可

@Repository
public interface EduTeacherMapper extends BaseMapper<EduTeacher> {
    @Select("select * from edu_teacher")
    EduTeacherMapper selectTeacherList();
}

然後要呼叫該方法的話,通過注入mapper,然後通過mapper呼叫方法即可

@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private EduTeacherMapper teacherMapper;

    public EduTeacher selectTheacher(){
        return teacherMapper.selectTheacher();
    }
}

2、多表查詢

多表查詢和自定義SQL是一樣寫的,唯一區別就是需要建立一個 vo類來接收資料即可。

@Repository
public interface EduTeacherMapper extends BaseMapper<EduTeacher> {
   @Select("SELECT * FROM edu_teacher t1 INNER JOIN edu_course t2 ON t1.id = t2.teacher_id")
   EduTeacherVo selectTheacher();
}

3、多表分頁查詢

分頁查詢的話就需要用到Page了

1、需要先建立一個設定類,然後在設定類中引入分頁外掛(固定程式碼)

@Configuration                  
public class MpConfig {
    /**
     * 分頁外掛
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

2、mapper程式碼如下:

public interface EduTeacherMapper extends BaseMapper<EduTeacher> {
    @Select("select * from edu_teacher")
    IPage<EduTeacher> selectTheacher(Page<EduTeacher> page);
}

3、serviceImpl實現類程式碼如下:

@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private EduTeacherMapper teacherMapper;

    public void selectTheacher(){
        //1代表當前頁數,10代表每頁資料的數量
        Page<EduTeacher> page = new Page<>(1,10);
        IPage<EduTeacher> eduTeacherIPage = teacherMapper.selectTheacher(page);

        System.out.println(eduTeacherIPage.getCurrent());  //當前頁
        System.out.println(eduTeacherIPage.getPages());    //每頁資料 是一個List集合
        System.out.println(eduTeacherIPage.getSize());     //每頁顯示記錄數
        System.out.println(eduTeacherIPage.getTotal());    //總記錄數
    }
}

4、多表分頁條件查詢

多表分頁條件查詢只是在多表分頁查詢的基礎上增加了條件篩選而已

其它步驟都和多表分頁查詢一樣,只要在mapper中寫sql的時候多加條件引數就可以了。注意:如果使用了動態SQL,那麼該sql語句一定要使用script標籤包裹

@Select("<script>
  SELECT t1.id,t1.qiye_id,t1.member_tag,t1.name,t1.logo,t1.images,t1.video,t1.contacts_mobile,t1.contacts_name,t1.address,t1.sort,t1.sort,t1.click_num,t1.is_put,t1.company_type,t1.is_attestation,t1.is_recommend,t1.introduce,t1.create_time,t2.laoa_product_type_id AS productId,t3.laoa_style_id AS styleId FROM laoa_ad_logistics t1 LEFT JOIN laoa_product_middle t2 ON t1.qiye_id = t2.company_key LEFT JOIN laoa_style_middle t3 ON t1.qiye_id = t3.company_key
  <where>
    t1.company_type = 1
    <if test="adLogisticsVo.isOk == true">
      AND t1.is_put = 1
    </if>
    <if test="adLogisticsVo.name != null">
      AND t1.name LIKE CONCAT('%',#{adLogisticsVo.name},'%')
    </if>
    <if test="adLogisticsVo.styleId != null">
      AND t3.laoa_style_id = #{adLogisticsVo.styleId}
    </if>
    <if test="adLogisticsVo.productId != null">
      AND t2.laoa_product_type_id = #{adLogisticsVo.productId}
    </if>
  </where>
  ORDER BY t1.sort
</script>")
IPage<AdLogisticsVo> selectAdLogisticsList(@Param("page") IPage<AdLogisticsVo> page, @Param("adLogisticsVo") AdLogisticsVo adLogisticsVo);

總結

到此這篇關於mybatis-plus實現自定義SQL、多表查詢與多表分頁查詢語句的文章就介紹到這了,更多相關mybatis-plus自定義SQL 多表查詢內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com