MyBatis+MyBatisPlus中遇到的一些坑及解决
MyBatis+MyBatisPlus中遇到的一些坑
MyBatis是很常用的持久层框架,MyBatisPlus是一个 MyBatis 的增强工具.在实际工作中这两者就像是咖啡伴侣一样如影随形.
但是总会遇到这样或那样的问题,可能是一个失误,也可能是踩了个坑
坑一:MyBatisPlus分页不生效
自己没开启分页插件,是谁更坑呢?
@Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
坑二:一对多关联查询查询总条数错误
这是个真坑,好多人踩过.之所以会错误,是因为MyBatisPlus的分页是在SQL语句最后添加limit实现的,这就导致一对多关联查询出来的多条数据被记入了总条数参加了分页.
想要解决,简单粗暴的就是把关联查询分开,先查"一"的相关信息,然后遍历再查"多"的相关信息.
作为一个认(闲)真(的)负(蛋)责(疼)的程序猿,肯定得用一些看上去高(没)大(卵)上(用)的方式.
实体 @Data @ApiModel(value="产品对象") public class EcProduct{ @TableId(value = "id", type = IdType.AUTO) private Integer id; @ApiModelProperty(value = "产品名称") private String name; @ApiModelProperty(value = "添加时间") private Date createDate; @ApiModelProperty(value = "操作人ID") private Integer optUserId; //一对一 private EcInsuranceCompany insuranceCompany; //一对多 private List<EcProductDuty> dutys; } @Data @ApiModel(value="公司对象") public class EcInsuranceCompany{ @TableId(value = "id", type = IdType.AUTO) private Integer id; @ApiModelProperty(value = "公司名称") private String name; } @Data @ApiModel(value="信息对象") 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代码生成器 出错
1、使用myabtis-plus代码自动生成器,启动时出现下面这个错误
在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、代码生成器启动以后,发现已经自动创建了一些包和代码
打开entiry包下的实体类User,发现显示包不存在
在pom.xml配置文件中添加下面依赖
<!--配置ApiModel在实体类中不生效--> <dependency> <groupId>com.spring4all</groupId> <artifactId>spring-boot-starter-swagger</artifactId> <version>1.5.1.RELEASE</version> </dependency>
至此,使用MyBatis-plus代码生成器 遇到的错误全部总结完毕。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用详解
这篇文章主要介绍了MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2022-03-03Java 使用 HttpClient 发送 GET请求和 POST请求
本文主要介绍了Java 使用 HttpClient 发送 GET请求和 POST请求,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-08-08java.net.UnknownHostException异常的一般原因及解决步骤
关于java.net.UnknownHostException大家也许都比较熟悉,这篇文章主要给大家介绍了关于java.net.UnknownHostException异常的一般原因及解决步骤,文中通过代码介绍的非常详细,需要的朋友可以参考下2024-02-02Springcloud Config支持本地配置文件的方法示例
这篇文章主要介绍了Springcloud Config支持本地配置文件的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-02-02启动 Eclipse 弹出 Failed to load the JNI shared library jvm.dll
这篇文章主要介绍了有时候,新电脑上回碰到打开Eclipse时,弹出提示“Failed to load the JNI shared library jvm.dll”错误,这里给大家分享解决方案2016-08-08SpringCloud Alibaba使用Seata处理分布式事务的技巧
在传统的单体项目中,我们使用@Transactional注解就能实现基本的ACID事务了,随着微服务架构的引入,需要对数据库进行分库分表,每个服务拥有自己的数据库,这样传统的事务就不起作用了,那么我们如何保证多个服务中数据的一致性呢?跟随小编一起通过本文了解下吧2021-06-06
最新评论