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 ...
随机推荐
- Spring事务超时时间可能存在的错误认识
摘自:http://jinnianshilongnian.iteye.com/blog/1986023, 感谢作者. 1.先看代码 1.1.spring-config.xml <bean id= ...
- BZOJ 1492 货币兑换 Cash CDQ分治
这题n2算法就是一个维护上凸包的过程. 也可以用CDQ分治做. 我的CDQ分治做法和网上的不太一样,用左边的点建立一个凸包,右边的点在上面二分. 好处是思路清晰,避免了凸包的插入删除,坏处是多了一个l ...
- 《CSS权威指南(第三版)》---第六章 文本属性
本章主要的内容是: 1.文本缩进: text-indent.行内元素无法缩进,一般用左内边距或外边距来创造这种效果. 2.文本对齐:text-align .只应用于块状元素. 3.行高:一般line- ...
- 在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5 ,PHP5.6)支持。
本人预装了一个wamp的集成环境(Apache 2.4.9 + PHP 5.5.12 + mysql 5.6.17),今天在wamp环境下 添加PHP多版本 (PHP 5.5.30).中间两个过程,1 ...
- POJ题目算法分类总结博客地址
http://blog.csdn.net/sunbaigui/article/details/4421705 又从这个地址找了一些:http://blog.csdn.net/koudaidai/art ...
- jquery 用addClass之后 class有对应的事件,为什么要重新绑定一下事件呢
假设有元素A,B,C,其中A和B都有class属性cls,如果在页面加载完成时,给具有class属性为cls的元素绑定某一事件,例如click,执行事件时调用alert.也就是说,页面加载完成后A和B ...
- db2move 数据导出整理
db2move <database-name> <action> [<option> <value>] 命令解释:1).database-name, ...
- Map功能简化Python并发代码
<转摘>Python 并行任务技巧 支持Map并发的包文件有两个: Multiprocessing,还有少为人知的但却功能强大的子文件 multiprocessing.dummy. Dum ...
- bjwc Day1 暴力大战
今天终于有题了... 题目是COCI2016/2017 Round #4 T1一看就是NP问题,k<=50,开始想暴力,想了个n^4的,大概能过,就没去管它 T2想得太naive,丢了100分给 ...
- BZOJ3991:寻宝游戏 (LCA+dfs序+树链求并+set)
小B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以任意选择一个村庄,瞬间转移到这个村庄,然后可以任意在地图的道路上行走 ...