本文源码: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框架,集成分页助手插件的更多相关文章

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

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

  2. 九:SpringBoot-整合Mybatis框架,集成分页助手插件

    九:SpringBoot-整合Mybatis框架,集成分页助手插件 1.Mybatis框架 1.1 mybatis特点 1.2 适用场景 2.SpringBoot整合MyBatis 2.1 核心依赖 ...

  3. (六)SpringBoot2.0基础篇- Redis整合(JedisCluster集群连接)

    一.环境 Redis:4.0.9 SpringBoot:2.0.1 Redis安装:Linux(Redhat)安装Redis 二.SpringBoot整合Redis 1.项目基本搭建: 我们基于(五) ...

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

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

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

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

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

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

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

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

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

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

  9. SpringBoot2.0 基础案例(11):配置AOP切面编程,解决日志记录业务

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.AOP切面编程 1.什么是AOP编程 在软件业,AOP为Asp ...

随机推荐

  1. Ubuntu apt-get update 失败【转】

    本文转载自:http://www.jianshu.com/p/0de2b5717ce8 1 $ sudo apt-get update 报了一堆错误: Err http://cn.archive.ub ...

  2. 【css学习整理】css基础(样式,语法,选择器)

    CSS是什么? cascading 层叠样式表 sheet 样式文件 style 外观个性化 CSS语法? 声明方法: 选择器(属性: 值; 属性: 值) 选择器: 通过名称制定对哪些标签进行样式设置 ...

  3. 分享知识-快乐自己:Oracle 创建序列 及 使用序列

    1.创建序列语法: create sequence 序列名 [可选参数] 序列名常定义为‘seq_XXX’的形式,创建序列不能使用replace 可选参数说明: increment by: 序列每次增 ...

  4. 第三届蓝桥杯预赛c++b组

    1.微生物增值 假设有两种微生物 X 和 Y     X出生后每隔3分钟分裂一次(数目加倍),Y出生后每隔2分钟分裂一次(数目加倍).     一个新出生的X,半分钟之后吃掉1个Y,并且,从此开始,每 ...

  5. PS 色调— —颜色梯度

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...

  6. javaScript的类型转换

    1.javaScript会自动跟据期望将值进行转换,比如 2.下面表列出了一些javaScript的自动转换,其中粗体字表示了出乎意料的转换情况 3.显示的类型转换 尽管类型可以自动进行一些转换,但是 ...

  7. java面试题06

    题目: 数据库 1. 表名:g_cardapply 字段(字段名/类型/长度): g_applyno varchar 8://申请单号(关键字) g_applydate bigint 8://申请日期 ...

  8. 【Python】String 字符串

    1. split() split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 split()方法语法:str.split(str="" ...

  9. bzoj 1510 [POI2006]Kra-The Disks——思路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1510 #include<iostream> #include<cstdio ...

  10. syslog-ng 配置(tcp协议)

    一.概况 两台服务器,都安装syslog-ng,一台服务端,一台客户端: server:192.168.209.19 client:192.168.209.18 二.安装 采用yum安装,执行: yu ...