一、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.xmltype-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 {@Resourceprivate ImgInfoMapper imgInfoMapper ;@Overridepublic int insert(ImgInfo record) {return imgInfoMapper.insert(record);}@Overridepublic List<ImgInfo> selectByExample(ImgInfoExample example) {return imgInfoMapper.selectByExample(example);}@Overridepublic int updateByPrimaryKeySelective(ImgInfo record) {return imgInfoMapper.updateByPrimaryKeySelective(record);}@Overridepublic int deleteByPrimaryKey(Integer imgId) {return imgInfoMapper.deleteByPrimaryKey(imgId);}
}

7、控制层测试类

@RestController
public class ImgInfoController {@Resourceprivate 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 configurationPUBLIC "-//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
码云地址:知了一笑
https://gitee.com/cicadasmile/spring-boot-base


SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件相关推荐

  1. SpringBoot2.0基础案例分类总结,后续更新计划说明

    一.基础案例 1.基础案例概览 历时一个半月,SpringBoot2.0基础案例的文章基本更新完毕了,基础案例包含了SpringBoot的基础教程,高级应用,日志配置,数据库使用,事务管理等.关于Sp ...

  2. SpringBoot2.0 基础案例(12):基于转账案例,演示事务管理操作

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.事务管理简介 1.事务基本概念 一组业务操作ABCD,要么全部 ...

  3. SpringBoot2.0 基础案例(09):集成JPA持久层框架,简化数据库操作

    一.JAP框架简介 JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化规范.主要是为了简化持久层开发以及整合ORM技术,结束H ...

  4. SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上 ...

  5. SpringBoot2.0 基础案例(07):集成Druid连接池,配置监控界面

    一.Druid连接池 1.druid简介 Druid连接池是阿里巴巴开源的数据库连接池项目.Druid连接池为监控而生,内置强大的监控功能,监控特性不影响性能.功能强大,能防SQL注入,内置Login ...

  6. SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口

    一.SpringBoot 框架的特点 SpringBoot2.0 特点 1)SpringBoot继承了Spring优秀的基因,上手难度小 2)简化配置,提供各种默认配置来简化项目配置 3)内嵌式容器简 ...

  7. SpringBoot2.0 基础案例(16):配置Actuator组件,实现系统监控

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Actuator简介 1.监控组件作用 在生产环境中,需要实时 ...

  8. SpringBoot2.0 基础案例(15):配置MongoDB数据库,实现增删改查逻辑

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.NoSQL简介 1.NoSQL 概念 NoSQL( Not O ...

  9. SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Cache缓存简介 从Spring3开始定义Cache和Cac ...

最新文章

  1. AngularJs $resource 高大上的数据交互
  2. 剑指Offer Ⅱ 003.二进制加法(力扣剑指Offer专项突击版——整数_3)
  3. mysql可以运行在不同sql mode模式下面,sql mode模式定义了mysql应该支持的sql语法,数据校验等...
  4. 信息学奥赛C++语言: 趣味整数1(自守数)
  5. 谷歌浏览器安装过程 0911
  6. 包含目录、库目录、附加包含目录、附加库目录、附加依赖项如何使用? 及静态库,动态库的创建与调用和vs里引用的使用
  7. 【我的物联网成长记3】如何开发物联网应用?
  8. iPhone 12系列电池容量曝光:不仅没增加竟还在缩水
  9. 安卓手机用久了会卡顿,那么到底应该删掉手机里的哪些东西?
  10. 18. 二叉树的镜像
  11. Jmeter初探之录制
  12. win7系统两台电脑之间利用Socket实现文件传输---C++实现
  13. Get value from agent failed:cannot connect to[[192.168.1.113];10050]:[111]Co
  14. 基于RT1052的FlexSPI读写FLASH(W25Q256JV)
  15. 改善客户服务体验的 5 种方法
  16. 忍者必须死3突然服务器维修,《忍者必须死3》3月12日2:00停服维护公告
  17. FinalShell Mac OS版安装
  18. Ubuntu22.04安装riscv64-toolchain和QEMU
  19. python BFS最短路径问题
  20. 【PG】PG基础操作

热门文章

  1. 计组之存储系统:5、cache(cache功能、cache工作原理、cache性能分析)
  2. zookeeper 和 kafka 集群搭建
  3. thrift客户端调用不支持多线程,非线程安全
  4. 使用和了解Valgrind核心:高级主题
  5. event-config.h指明所在系统的环境
  6. HDU1026 Ignatius and the Princess I(深度优先搜索)
  7. Python gevent高并发(限制最大并发数、协程池)
  8. 学习Python第二天
  9. sqlserver数据库原理
  10. (转) POJO和javabean的异同