您好,登錄后才能下訂單哦!
這篇文章主要講解了“mybatisplus怎么實現自定義SQL、多表查詢與多表分頁查詢語句”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“mybatisplus怎么實現自定義SQL、多表查詢與多表分頁查詢語句”吧!
在說怎么實現之前我們要先明白一個概念,就是mybatis-plus是在mybatis的基礎上進行增強,并不做改變,所以mybatis的操作在mybatis-plus中也是一樣可以使用的,咱們直接上代碼
在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(); } }
多表查詢和自定義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(); }
分頁查詢的話就需要用到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()); //總記錄數 } }
多表分頁條件查詢只是在多表分頁查詢的基礎上增加了條件篩選而已
其它步驟都和多表分頁查詢一樣,只要在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);
感謝各位的閱讀,以上就是“mybatisplus怎么實現自定義SQL、多表查詢與多表分頁查詢語句”的內容了,經過本文的學習后,相信大家對mybatisplus怎么實現自定義SQL、多表查詢與多表分頁查詢語句這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。