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

Stella981
• 阅读 559

一、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、项目结构图

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

采用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、逆向工程生成的文件

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

这里就不贴代码了。

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&amp;v=4");
        record.setLinkUrl("https://avatars0.githubusercontent.com/u/50793885?s=460&amp;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" ?-->

<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&lt;&gt;(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框架,集成分页助手插件 SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件

点赞
收藏
评论区
推荐文章
菜鸟阿都 菜鸟阿都
2年前
pageHelper一对多分页解决方案
前言   pageHelper是一款优秀的Mybatis分页插件,在项目中可以非常便利的使用,使开发效率得到很大的提升,但不支持一对多结果映射的分页查询,所以在平时的使用时,对于一对多分页会出现分页错误,这篇文章主要对pageHelper分页错误进行重现以及提出解决方案。分析    mybatis进行一对多查询时,映射文件(mapper.xml
推荐学java 推荐学java
2年前
推荐学Java——第一个MyBatis程序
什么是MyBatis一款优秀的持久层框架。MyBatis使用XML将SQL与程序解耦,便于维护。MyBatis学习成本低,执行高效,底层是对JDBC的封装和扩展。MyBtis官网:https://mybatis.org/mybatis3/zh/index.htmlgithub地址:https://github.com/mybatis/m
Stella981 Stella981
2年前
Spring Boot(六)集成 MyBatis 操作 MySQL 8
一、简介1.1MyBatis介绍MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。1.2MyBatis发展史MyBatis原本是apache的一个开源项目iBatis,2010年这个项目由apache
Easter79 Easter79
2年前
Spring学习笔记(六):MyBatis集成
1概述MyBaits是一个著名的持久层框架,本文首先介绍了MyBatis的简单使用,接着与Spring进行整合,最后简单地使用了Generator去自动生成代码。2MyBatis简介MyBatis本来是Apache的一个开源项目——iBatis,2010年由Apaceh
Wesley13 Wesley13
2年前
Java框架之Mybatis(二)
本文主要介绍Mybatis(一)之后剩下的内容:1mybatis中log4j的配置2dao层的开发(使用mapper代理的方式)3mybatis的配置详解4输入输出映射对应的类型(parameterType和resultType)5mybatis动态sql6mybatis中的一级缓存7mybat
Stella981 Stella981
2年前
Mybatis映射器源码解析
Mybatis映射器映射器是MyBatis最强大的⼯具,也是我们使用MyBatis时⽤得最多的工具,因此熟练掌握它⼗分必要。MyBatis是针对映射器构造的SQL构建的轻量级框架,并且通过配置生成对应的JavaBean返回给调用者,⽽这些配置主要便是映射器,在MyBatis中你可以根据情况定义动态SQL来满足不同场景的需要,它比其他框架
Easter79 Easter79
2年前
SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件
一、Mybatis框架1、mybatis简介MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解来配置和映射原生类型、接口和Java的POJO
Stella981 Stella981
2年前
SpringBoot学习之路:05.Spring Boot集成pagehelper分页插件
      前面说了SpringBoot集成持久层框架Mybatis的过程,和使用mybatis进行对数据库进行CRUD的操作,然而当对多数据进行查询时就需要进行分页了,分页技术分为客户端分页和服务器端分页(数据库分页),客户端分页是前端的数据插件对返回的数据集进行分页(bootstruptable、quitable等),客户端分页会对数据库和客
Easter79 Easter79
2年前
SpringBoot学习之路:05.Spring Boot集成pagehelper分页插件
      前面说了SpringBoot集成持久层框架Mybatis的过程,和使用mybatis进行对数据库进行CRUD的操作,然而当对多数据进行查询时就需要进行分页了,分页技术分为客户端分页和服务器端分页(数据库分页),客户端分页是前端的数据插件对返回的数据集进行分页(bootstruptable、quitable等),客户端分页会对数据库和客
金旋 金旋
2个月前
基于SpringBoot+Mybatis从头搭建通用管理系统(后端篇)
//下仔のke:https://yeziit.cn/14917/MyBatis是一款优秀的持久层框架,它支持自定义SQL、存储过程以及高级映射。MyBatis通过简单的XML或注解来配置和映射原始类型、接口和JavaPOJO(PlainOldJavaObj