APP下载

SpringBoot2.0 基础案例(10):整合Mybatis框架 整合分页助手外挂

消息来源:baojiabao.com 作者: 发布时间:2026-05-26

报价宝综合消息SpringBoot2.0 基础案例(10):整合Mybatis框架 整合分页助手外挂

本文源代码

GitHub地址:知了一笑

https://github.com/cicadasmile/spring-boot-base

一、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、核心依赖

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

com.github.pagehelper

pagehelper

4.1.6

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 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 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 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配置档案

br> PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

2、分页实现程式码

@Override

public PageInfo 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 imgInfoList = imgInfoMapper.selectByExample(example) ;

PageInfo 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

2019-12-08 12:53:00

相关文章