MyBatis+MyBatisPlus中遇到的一些坑及解決
MyBatis+MyBatisPlus中遇到的一些坑
MyBatis是很常用的持久層框架,MyBatisPlus是一個(gè) MyBatis 的增強(qiáng)工具.在實(shí)際工作中這兩者就像是咖啡伴侶一樣如影隨形.
但是總會(huì)遇到這樣或那樣的問題,可能是一個(gè)失誤,也可能是踩了個(gè)坑
坑一:MyBatisPlus分頁不生效
自己沒開啟分頁插件,是誰更坑呢?
?@Configuration
?public class WebMvcConfig extends WebMvcConfigurationSupport {
? ?@Bean
? ? public PaginationInterceptor paginationInterceptor() {
? ? ? ? return new PaginationInterceptor();
? ? }
?}坑二:一對(duì)多關(guān)聯(lián)查詢查詢總條數(shù)錯(cuò)誤
這是個(gè)真坑,好多人踩過.之所以會(huì)錯(cuò)誤,是因?yàn)镸yBatisPlus的分頁是在SQL語句最后添加limit實(shí)現(xiàn)的,這就導(dǎo)致一對(duì)多關(guān)聯(lián)查詢出來的多條數(shù)據(jù)被記入了總條數(shù)參加了分頁.
想要解決,簡單粗暴的就是把關(guān)聯(lián)查詢分開,先查"一"的相關(guān)信息,然后遍歷再查"多"的相關(guān)信息.
作為一個(gè)認(rèn)(閑)真(的)負(fù)(蛋)責(zé)(疼)的程序猿,肯定得用一些看上去高(沒)大(卵)上(用)的方式.
實(shí)體
@Data
@ApiModel(value="產(chǎn)品對(duì)象")
public class EcProduct{
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "產(chǎn)品名稱")
private String name;
@ApiModelProperty(value = "添加時(shí)間")
private Date createDate;
@ApiModelProperty(value = "操作人ID")
private Integer optUserId;
//一對(duì)一
private EcInsuranceCompany insuranceCompany;
//一對(duì)多
private List<EcProductDuty> dutys;
}
@Data
@ApiModel(value="公司對(duì)象")
public class EcInsuranceCompany{
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "公司名稱")
private String name;
}
@Data
@ApiModel(value="信息對(duì)象")
public class EcProductDuty {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String detail;
}
mapper.xml
<resultMap id="productPlanRespone" type="XXX.EcProduct">
<id property="id" column="id"/>
<result property="name" column="name"/>
.............
<association property="insuranceCompany" javaType="XXX.EcInsuranceCompany">
<id property="id" column="iid"/>
............
</association>
<collection property="dutys" column="id" select="XXXX.getProductDutyByPlanId">
</collection>
</resultMap>
<select id="XXX" resultMap="productPlanRespone">
</select>
mybatisplus遇到的問題
使用MyBatis-plus代碼生成器 出錯(cuò)
1、使用myabtis-plus代碼自動(dòng)生成器,啟動(dòng)時(shí)出現(xiàn)下面這個(gè)錯(cuò)誤
在pom.xml中添加下面依賴
? <dependency> ? ? ? ? ? ? <groupId>org.apache.velocity</groupId> ? ? ? ? ? ? <artifactId>velocity-engine-core</artifactId> ? ? ? ? ? ? <version>2.2</version> ? ? ? ? </dependency> <dependency> ? ? ? ? ? ? <groupId>com.baomidou</groupId> ? ? ? ? ? ? <artifactId>mybatis-plus-generator</artifactId> ? ? ? ? ? ? <version>3.4.1</version> ? ? ? ? </dependency>
2、代碼生成器啟動(dòng)以后,發(fā)現(xiàn)已經(jīng)自動(dòng)創(chuàng)建了一些包和代碼
打開entiry包下的實(shí)體類User,發(fā)現(xiàn)顯示包不存在
在pom.xml配置文件中添加下面依賴
? ?<!--配置ApiModel在實(shí)體類中不生效--> ? ? <dependency> ? ? ? ? <groupId>com.spring4all</groupId> ? ? ? ? <artifactId>spring-boot-starter-swagger</artifactId> ? ? ? ? <version>1.5.1.RELEASE</version> ? ? </dependency>
至此,使用MyBatis-plus代碼生成器 遇到的錯(cuò)誤全部總結(jié)完畢。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MybatisPlus3.5.5與pagehelper?starter2.1.0沖突的問題解決
在使用MybatisPlus?3.5.5與PageHelper?Starter?2.1.0時(shí),由于引用了不同版本的jsqlparser庫(4.6與4.7),會(huì)導(dǎo)致運(yùn)行時(shí)錯(cuò)誤,解決方案涉及確認(rèn)依賴版本,本文就來介紹一下,感興趣的同學(xué)可以下載學(xué)習(xí)2024-10-10
java傳入時(shí)間戳返回LocalDateTime的實(shí)現(xiàn)方法
這篇文章主要介紹了java傳入時(shí)間戳返回LocalDateTime的實(shí)現(xiàn)方法,在Java中將時(shí)間戳轉(zhuǎn)換為LocalDateTime時(shí)需要注意時(shí)區(qū)問題,因?yàn)長ocalDateTime不包含時(shí)區(qū)信息,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11
Java的List集合框架之LinkedList詳細(xì)解析
這篇文章主要介紹了Java的List集合框架之LinkedList詳細(xì)解析,LinkedList底層是內(nèi)部Node類的存儲(chǔ),prev、next、item值,同時(shí)最外層還有first、last節(jié)點(diǎn),需要的朋友可以參考下2023-11-11
在Docker中部署Spring Boot項(xiàng)目過程詳解
這篇文章主要介紹了在Docker中部署Spring Boot項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
springboot+camunda實(shí)現(xiàn)工作流的流程分析
Camunda是基于Java語言,支持BPMN標(biāo)準(zhǔn)的工作流和流程自動(dòng)化框架,并且還支持CMMN規(guī)范,DMN規(guī)范,本文給大家介紹springboot+camunda實(shí)現(xiàn)工作流的流程分析,感興趣的朋友一起看看吧2021-12-12

