Optional与Mybatis能否一起
1、mybatis的@Param()参数传递的问题,与JDK1.8的Optional的返回值问题。使用Optional与spring-data-jpa和mybatis有啥区别?
使用spring-data-jpa可以使用Optional<实体>,这样的返回不会出错。
但是Mybatis不行,返回值不能是 Optional<实体>,这样子会报错,字段也传入不到mapperxml中。
2、mybatis的传递参数为集合(set,list)的问题。
问题原因:


出错原因:collection写set,list或者collection,所以出错。
3、mybatis使用自定义的分页插件,查询记录条数,查询显示内容
public class PageResult<T> {
private int currentPage;
private int pageSize;
private int totalPages;
private long totalCount;
private List<T> content;
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
int page= (int) (this.totalCount/pageSize);
if(this.totalCount%pageSize>0){
page++;
}
this.totalPages = page;
}
public long getTotalCount() {
return totalCount;
}
public void setTotalCount(long totalCount) {
this.totalCount = totalCount;
}
public List<T> getLContent() {
return content;
}
public void setContent(List<T> content) {
this.content = content;
}
页面传递的参数:
currentPage 当前页,pageSize每页的数据条数
pageResult.setCurrentPage(currentPage);
pageResult.setPageSize(pageSize);
计算有多少条记录count:select count(*) from 表
pageResult.setTotalCount(count);pageResult.setTotalPages(pageResult.getTotalPages()); 查询页面内容: select * from 表 where 条件
order by 字段 什么顺序(desc还是asc)limit #{currentPage},#{pageSize}
pageResult.setContent(页面内容)
返回pageResult即可。
Optional与Mybatis能否一起的更多相关文章
- Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件
前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...
- 我最喜欢的Mybatis 3.5新特性!超实用!
Mybatis 3.5 发布有段时间了,终于支持了 Optional ,这么实用的特性,竟然还没人安利……于是本文出现了. 新特性比较简单,但非常实用,因为能大量简化恶心的判空代码. WARNING ...
- Mapper XML Files详解
扫扫关注"茶爸爸"微信公众号 坚持最初的执着,从不曾有半点懈怠,为优秀而努力,为证明自己而活. Mapper XML Files The true power of MyBatis ...
- springboot+springcloud集成jar
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- java:redis(java代码操作redis,实体类mapper生成器(generator))
1.redis_demo Maven ItemMapper.xml: <?xml version="1.0" encoding="UTF-8" ?> ...
- Springboot整合MybatisPlus(超详细)完整教程~
新建springboot项目 开发工具:idea2019.2,maven3 pom.xml <dependency> <groupId>org.springframework. ...
- JavaWeb-JDBC-Mybatis-Junit-Maven-Lombok
Java与数据库 初识JDBC JDBC是什么? JDBC英文名为:Java Data Base Connectivity(Java数据库连接),官方解释它是Java编程语言和广泛的数据库之间独立于数 ...
- IDEA 中生成 MyBatis 逆向工程实践
IDEA 逆向 MyBatis 工程时,不像支持 Hibernate 那样有自带插件,需要集成第三方的 MyBatis Generator. MyBatis Generator的详细介绍 http:/ ...
- 从头开始搭建一个mybatis+postgresql平台
最近有个项目的数据库使用postgresql,使用原生态的mybatis操作数据,原生态的没什么不好,只不过国内有个tk.mybatis的工具帮助我们做了很多实用的事情,大多数情况下我们需要 ...
随机推荐
- CF1016 D. Vasya And The Matrix
传送门 [http://codeforces.com/group/1EzrFFyOc0/contest/1016/problem/D] 题意 已知矩阵n行m列,以及每一行,每一列所有元素的异或,用 a ...
- Scrum Meeting NO.6
Scrum Meeting No.6 1.会议内容 今晚是提交编译测试程序的截至日期,大家果断都在忙着写编译,所以今天的进度不大. 2.任务清单 徐越 序号 近期的任务 进行中 已完成 1 代码重构: ...
- Failed while installing JAX-RS (REST Web Services) 1.1. org.osgi.service.prefs.BackingStoreException: Resource '/.settings' does not exist.
https://issues.jboss.org/browse/JBIDE-10039?_sscc=t https://stackoverflow.com/questions/16836832/fai ...
- Bootstrap Validator使用特性,动态(Dynamic)添加的input的验证问题
http://1000hz.github.io/bootstrap-validator/#validator-usage Validated fields By default, the valida ...
- js排序方法
function swap(ary, x, y) { if (x === y) return let temp = ary[x] ary[x] = ary[y] ary[y] = temp } //生 ...
- Linux shell 菜鸟学习笔记....
20171123 Linux shell 基础学习笔记1. shell 的开始 一般是 #!/bin/bash 通过 #! 来唯一指定使用的shell路径 其他的 # 都表示注释.2. shell 的 ...
- 好文章系列C/C++——图说C++对象模型:对象内存布局详解
注:收藏好文章,得出自己的笔记,以查漏补缺! ------>原文链接:http://blog.jobbole.com/101583/ 前言 本文可加深对C++对象的内存布局.虚表指针.虚 ...
- 非关心数据库无法进行连表查询 所以我们需要在进行一对多查询时候 无法满足 因此需要在"1"的一方添加"多"的一方的的id 以便用于进行连表查询 ; 核心思想通过id进行维护与建文件
非关心数据库无法进行连表查询 所以我们需要在进行一对多查询时候 无法满足 因此需要在"1"的一方添加"多"的一方的的id 以便用于进行连表查询 ; 核心思想通 ...
- Hard Rock
Ilya is a frontman of the most famous rock band on Earth. Band decided to make the most awesome musi ...
- CUBA如何新增ServiceBean
简单的方法 在页面MIDDLEWARE模块,可以直接新建.编辑.删除 复杂的方法 在代码中手动实现,则需要1.添加Serviceweb-spring.xml中,添加 <entry key=&qu ...