SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件
本文源码:GitHub·点这里 || GitEE·点这里
一、Mybatis框架
1、mybatis简介
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
2、mybatis特点
1)sql语句与代码分离,存放于xml配置文件中,方便管理
2)用逻辑标签控制动态SQL的拼接,灵活方便
3)查询的结果集与java对象自动映射
4)编写原生态SQL,接近JDBC
5)简单的持久化框架,框架不臃肿简单易学
3、适用场景
MyBatis专注于SQL本身,是一个足够灵活的DAO层解决方案。
对性能的要求很高,或者需求变化较多的项目,MyBatis将是不错的选择。
二、与SpringBoot2.0整合
1、项目结构图

采用druid连接池,该连接池。
2、核心依赖
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mybatis的分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
3、核心配置
mybatis:
# mybatis配置文件所在路径
config-location: classpath:mybatis.cfg.xml
type-aliases-package: com.boot.mybatis.entity
# mapper映射文件
mapper-locations: classpath:mapper/*.xml
4、逆向工程生成的文件

这里就不贴代码了。
5、编写基础测试接口
// 增加
int insert(ImgInfo record);
// 组合查询
List<ImgInfo> selectByExample(ImgInfoExample example);
// 修改
int updateByPrimaryKeySelective(ImgInfo record);
// 删除
int deleteByPrimaryKey(Integer imgId);
6、编写接口实现
@Service
public class ImgInfoServiceImpl implements ImgInfoService {
@Resource
private ImgInfoMapper imgInfoMapper ;
@Override
public int insert(ImgInfo record) {
return imgInfoMapper.insert(record);
}
@Override
public List<ImgInfo> selectByExample(ImgInfoExample example) {
return imgInfoMapper.selectByExample(example);
}
@Override
public int updateByPrimaryKeySelective(ImgInfo record) {
return imgInfoMapper.updateByPrimaryKeySelective(record);
}
@Override
public int deleteByPrimaryKey(Integer imgId) {
return imgInfoMapper.deleteByPrimaryKey(imgId);
}
}
7、控制层测试类
@RestController
public class ImgInfoController {
@Resource
private ImgInfoService imgInfoService ;
// 增加
@RequestMapping("/insert")
public int insert(){
ImgInfo record = new ImgInfo() ;
record.setUploadUserId("A123");
record.setImgTitle("博文图片");
record.setSystemType(1) ;
record.setImgType(2);
record.setImgUrl("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4");
record.setLinkUrl("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4");
record.setShowState(1);
record.setCreateDate(new Date());
record.setUpdateDate(record.getCreateDate());
record.setRemark("知了");
record.setbEnable("1");
return imgInfoService.insert(record) ;
}
// 组合查询
@RequestMapping("/selectByExample")
public List<ImgInfo> selectByExample(){
ImgInfoExample example = new ImgInfoExample() ;
example.createCriteria().andRemarkEqualTo("知了") ;
return imgInfoService.selectByExample(example);
}
// 修改
@RequestMapping("/updateByPrimaryKeySelective")
public int updateByPrimaryKeySelective(){
ImgInfo record = new ImgInfo() ;
record.setImgId(11);
record.setRemark("知了一笑");
return imgInfoService.updateByPrimaryKeySelective(record);
}
// 删除
@RequestMapping("/deleteByPrimaryKey")
public int deleteByPrimaryKey() {
Integer imgId = 11 ;
return imgInfoService.deleteByPrimaryKey(imgId);
}
}
8、测试顺序
http://localhost:8010/insert
http://localhost:8010/selectByExample
http://localhost:8010/updateByPrimaryKeySelective
http://localhost:8010/deleteByPrimaryKey
三、集成分页插件
1、mybatis配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<!--mybatis分页插件-->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
2、分页实现代码
@Override
public PageInfo<ImgInfo> queryPage(int page,int pageSize) {
PageHelper.startPage(page,pageSize) ;
ImgInfoExample example = new ImgInfoExample() ;
// 查询条件
example.createCriteria().andBEnableEqualTo("1").andShowStateEqualTo(1);
// 排序条件
example.setOrderByClause("create_date DESC,img_id ASC");
List<ImgInfo> imgInfoList = imgInfoMapper.selectByExample(example) ;
PageInfo<ImgInfo> pageInfo = new PageInfo<>(imgInfoList) ;
return pageInfo ;
}
3、测试接口
http://localhost:8010/queryPage
四、源代码地址
GitHub·地址
https://github.com/cicadasmile/spring-boot-base
GitEE·地址
https://gitee.com/cicadasmile/spring-boot-base

SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件的更多相关文章
- SpringBoot2.0 基础案例(12):基于转账案例,演示事务管理操作
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.事务管理简介 1.事务基本概念 一组业务操作ABCD,要么全部 ...
- 九:SpringBoot-整合Mybatis框架,集成分页助手插件
九:SpringBoot-整合Mybatis框架,集成分页助手插件 1.Mybatis框架 1.1 mybatis特点 1.2 适用场景 2.SpringBoot整合MyBatis 2.1 核心依赖 ...
- (六)SpringBoot2.0基础篇- Redis整合(JedisCluster集群连接)
一.环境 Redis:4.0.9 SpringBoot:2.0.1 Redis安装:Linux(Redhat)安装Redis 二.SpringBoot整合Redis 1.项目基本搭建: 我们基于(五) ...
- SpringBoot2.0 基础案例(09):集成JPA持久层框架,简化数据库操作
一.JAP框架简介 JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化规范.主要是为了简化持久层开发以及整合ORM技术,结束H ...
- SpringBoot2.0 基础案例(07):集成Druid连接池,配置监控界面
一.Druid连接池 1.druid简介 Druid连接池是阿里巴巴开源的数据库连接池项目.Druid连接池为监控而生,内置强大的监控功能,监控特性不影响性能.功能强大,能防SQL注入,内置Login ...
- SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上 ...
- SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口
一.SpringBoot 框架的特点 1.SpringBoot2.0 特点 1)SpringBoot继承了Spring优秀的基因,上手难度小 2)简化配置,提供各种默认配置来简化项目配置 3)内嵌式容 ...
- SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Cache缓存简介 从Spring3开始定义Cache和Cac ...
- SpringBoot2.0 基础案例(11):配置AOP切面编程,解决日志记录业务
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.AOP切面编程 1.什么是AOP编程 在软件业,AOP为Asp ...
随机推荐
- JAVA各版本更新特性1-8
JAVA各版本更新特性1-8 原文地址 Java Versions, Features and History This article gives you a highlight of import ...
- poj The Settlers of Catan( 求图中的最长路 小数据量 暴力dfs搜索(递归回溯))
The Settlers of Catan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1123 Accepted: ...
- VLAN(虚拟局域网)划分
VLAN根据不同的需求,可以有多种划分方式: 一:静态划分 基于端口 按VLAN交换机上的物理端口和内部的PVC(永久虚电路)端口来划分 静态划分安全.可靠,易于配置与维护 二 ...
- 创建一个HTML5与JQuery结合的幻灯片
1. [代码][JavaScript]代码 $(window).load(function(){ //我们监听了 window.load 事件,因此我们确定幻灯片上的所有图片都能够正确进行加载. ...
- Android程序-计算器
基于Android 2.3.3做的一个练手计算器. 可解析带括号的四则运算. 解析算术表达式的时候,准备调用Webkit通过Js来解析的. 但是2.3.3存在Bug,Js调用Java会导致程序崩溃, ...
- 【HDU2050】折线分割平面
Position Solution 2×n^2-n+1 证明见分割问题 Code // This file is made by YJinpeng,created by XuYike's black ...
- C#多线程学习 之 线程池[ThreadPool]
在多线程的程序中,经常会出现两种情况: 一种情况: 应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然后才能给予响应 这一般使用ThreadPo ...
- 监测GPU使用情况命令
每2秒监测一次:watch -n 2 nvidia-smi
- 51nod 1218 最长递增子序列 V2——LIS+思路(套路)
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1218 自己怎么连这种 喜闻乐见的大水题 都做不出来了…… 好像见过 ...
- 如何为现有控件的DependencyProperty添加Value Changed事件?
主要是利用DependencyPropertyDescriptor 的AddValueChanged 方法, 比如下面的例子为DataGridColumn的VisibilityPr ...