淺談mybatisPlus的Ipage分頁和map參數(shù)的問題
mybatisPlus的Ipage分頁和map參數(shù)
前提:先有一個map類型的參數(shù)
Map params= new HashMap(); params.put("name","張三"); params.put("age","23");
第一種情況
List<Map<String,Object>> selectList(@Param("params") HashMap params);
這種也是我們最常用的一種
不管參數(shù)是什么類型都可以省略,但是要寫上返回值類型(根據(jù)自己的情況)
<select id="selectList" resultType="java.util.HashMap">
取值時只需要在xml文件內(nèi)用map中的鍵值來取數(shù)據(jù)
<if test="name != '' and name != null"> AND name=#{name} </if> <if test="age != '' and age != null"> AND age=#{age} </if>
第二種情況
dao層聲明參數(shù)和返回值類型
IPage selectAll(IPage page,@Param("params") HashMap params); <select id="selectAll" resultType="java.util.HashMap">
一般我們在xxx.xml里面取map的值都是直接通過#{鍵}來取的
但是當傳入的參數(shù)不止一個時,取map里面的值就需要用參數(shù)去取
<if test="params.name != '' and params.name != null"> AND name=#{params.name} </if> <if test="params.age != '' and params.age != null"> AND age=#{params.age} </if>
這個params就是dao層傳入的map類型的參數(shù)
直接通過鍵來取值無法取到值
mybatisPlus IPage分頁常見問題(坑)
觀前提示:
本文所使用的IDEA版本為ultimate 2019.1,JDK版本為1.8.0_141。
1.TooManyResultsException
最近在使用Mybatis-plus的IPage插件分頁時,出現(xiàn)了以下的莫名其妙的錯誤
Resolved [org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 6]
然后檢查我寫的Controller、Service、Mapper、Mapper.xml,結果還是一無所獲,以下是我的Mapper和Mapper.xml(大致內(nèi)容一致)
Mapper
public interface ExampleMapper { IPage<EntityDto> selectEntityAndPage(@Param("param") Entity param, Page<Entity> page); }
Mapper.xml的select部分
<select id="selectEntityAndPage" parameterType="cn.com.example.enetity.Entity " resultType="cn.com.example.dto.EntityDto"> select id, name from table </select>
百度了一下才發(fā)現(xiàn)了這個深坑
mybatis-plus 中page參數(shù)不在第一個位置,返回的結果集接收對象不被認為是一個集合,而放在第一位就沒有問題。
所以我改寫了Mapper參數(shù)的順序
IPage<EntityDto> selectEntityAndPage(Page<Entity> page, @Param("param") Entity param);
問題解決。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot返回前端Long類型字段丟失精度問題及解決方案
Java服務端返回Long整型數(shù)據(jù)給前端,JS會自動轉換為Number類型,本文主要介紹了SpringBoot返回前端Long類型字段丟失精度問題及解決方案,感興趣的可以了解一下2024-03-03Java使用jxl包寫Excel文件適合列寬實現(xiàn)
用jxl.jar包,讀寫過Excel文件。也沒有注意最適合列寬的問題,但是jxl.jar沒有提供最適合列寬的功能,上次用到寫了一下,可以基本實現(xiàn)最適合列寬。2013-11-11mybatis實現(xiàn)遍歷Map的key和value
這篇文章主要介紹了mybatis實現(xiàn)遍歷Map的key和value方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例
這篇文章主要介紹了java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09