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

一、Mybatis框架

1、mybatis简介

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

2、mybatis特点

11)sql语句与代码分离,存放于xml配置文件中,方便管理 22)用逻辑标签控制动态SQL的拼接,灵活方便 33)查询的结果集与java对象自动映射 44)编写原生态SQL,接近JDBC 55)简单的持久化框架,框架不臃肿简单易学

3、适用场景

MyBatis专注于SQL本身,是一个足够灵活的DAO层解决方案。 对性能的要求很高,或者需求变化较多的项目,MyBatis将是不错的选择。

二、与SpringBoot2.0整合

1、项目结构图

采用druid连接池,该连接池。

2、核心依赖

1<!-- mybatis依赖 --> 2<dependency> 3 <groupid>org.mybatis.spring.boot</groupid> 4 <artifactid>mybatis-spring-boot-starter</artifactid> 5 <version>1.3.2</version> 6</dependency> 7<!-- mybatis的分页插件 --> 8<dependency> 9 <groupid>com.github.pagehelper</groupid> 10 <artifactid>pagehelper</artifactid> 11 <version>4.1.6</version> 12</dependency>

3、核心配置

1mybatis: 2 # mybatis配置文件所在路径 3 config-location: classpath:mybatis.cfg.xml 4 type-aliases-package: com.boot.mybatis.entity 5 # mapper映射文件 6 mapper-locations: classpath:mapper/*.xml

4、逆向工程生成的文件

这里就不贴代码了。

5、编写基础测试接口

1// 增加 2int insert(ImgInfo record); 3// 组合查询 4List<imginfo> selectByExample(ImgInfoExample example); 5// 修改 6int updateByPrimaryKeySelective(ImgInfo record); 7// 删除 8int deleteByPrimaryKey(Integer imgId);

6、编写接口实现

1@Service 2public class ImgInfoServiceImpl implements ImgInfoService { 3 @Resource 4 private ImgInfoMapper imgInfoMapper ; 5 @Override 6 public int insert(ImgInfo record) { 7 return imgInfoMapper.insert(record); 8 } 9 @Override 10 public List<imginfo> selectByExample(ImgInfoExample example) { 11 return imgInfoMapper.selectByExample(example); 12 } 13 @Override 14 public int updateByPrimaryKeySelective(ImgInfo record) { 15 return imgInfoMapper.updateByPrimaryKeySelective(record); 16 } 17 @Override 18 public int deleteByPrimaryKey(Integer imgId) { 19 return imgInfoMapper.deleteByPrimaryKey(imgId); 20 } 21}

7、控制层测试类

1@RestController 2public class ImgInfoController { 3 @Resource 4 private ImgInfoService imgInfoService ; 5 // 增加 6 @RequestMapping("/insert") 7 public int insert(){ 8 ImgInfo record = new ImgInfo() ; 9 record.setUploadUserId("A123"); 10 record.setImgTitle("博文图片"); 11 record.setSystemType(1) ; 12 record.setImgType(2); 13 record.setImgUrl("https://avatars0.githubusercontent.com/u/50793885?s=460&amp;v=4"); 14 record.setLinkUrl("https://avatars0.githubusercontent.com/u/50793885?s=460&amp;v=4"); 15 record.setShowState(1); 16 record.setCreateDate(new Date()); 17 record.setUpdateDate(record.getCreateDate()); 18 record.setRemark("知了"); 19 record.setbEnable("1"); 20 return imgInfoService.insert(record) ; 21 } 22 // 组合查询 23 @RequestMapping("/selectByExample") 24 public List<imginfo> selectByExample(){ 25 ImgInfoExample example = new ImgInfoExample() ; 26 example.createCriteria().andRemarkEqualTo("知了") ; 27 return imgInfoService.selectByExample(example); 28 } 29 // 修改 30 @RequestMapping("/updateByPrimaryKeySelective") 31 public int updateByPrimaryKeySelective(){ 32 ImgInfo record = new ImgInfo() ; 33 record.setImgId(11); 34 record.setRemark("知了一笑"); 35 return imgInfoService.updateByPrimaryKeySelective(record); 36 } 37 // 删除 38 @RequestMapping("/deleteByPrimaryKey") 39 public int deleteByPrimaryKey() { 40 Integer imgId = 11 ; 41 return imgInfoService.deleteByPrimaryKey(imgId); 42 } 43}

8、测试顺序

1http://localhost:8010/insert 2http://localhost:8010/selectByExample 3http://localhost:8010/updateByPrimaryKeySelective 4http://localhost:8010/deleteByPrimaryKey

三、集成分页插件

1、mybatis配置文件

1<!--?xml version="1.0" encoding="UTF-8" ?--> 2 3<configuration> 4 <plugins> 5 <!--mybatis分页插件--> 6 <plugin interceptor="com.github.pagehelper.PageHelper"> 7 <property name="dialect" value="mysql" /> 8 </plugin> 9 </plugins> 10</configuration>

2、分页实现代码

1@Override 2public PageInfo<imginfo> queryPage(int page,int pageSize) { 3 PageHelper.startPage(page,pageSize) ; 4 ImgInfoExample example = new ImgInfoExample() ; 5 // 查询条件 6 example.createCriteria().andBEnableEqualTo("1").andShowStateEqualTo(1); 7 // 排序条件 8 example.setOrderByClause("create_date DESC,img_id ASC"); 9 List<imginfo> imgInfoList = imgInfoMapper.selectByExample(example) ; 10 PageInfo<imginfo> pageInfo = new PageInfo&lt;&gt;(imgInfoList) ; 11 return pageInfo ; 12}

3、测试接口

http://localhost:8010/queryPage

四、源代码地址

1GitHub地址:知了一笑 2https://github.com/cicadasmile/spring-boot-base 3码云地址:知了一笑 4https://gitee.com/cicadasmile/spring-boot-base

</imginfo></imginfo></imginfo></imginfo></imginfo></imginfo>

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

推荐学Java——第一个MyBatis程序

什么是MyBatis一款优秀的持久层框架。MyBatis使用XML将SQL与程序解耦,便于维护。MyBatis学习成本低,执行高效,底层是对JDBC的封装和扩展。MyBtis官网:https://mybatis.org/mybatis3/zh/index.htmlgithub地址:https://github.com/mybatis/m

Spring Boot(六)集成 MyBatis 操作 MySQL 8

一、简介1.1MyBatis介绍MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。1.2MyBatis发展史MyBatis原本是apache的一个开源项目iBatis,2010年这个项目由apache

Spring学习笔记(六):MyBatis集成

1概述MyBaits是一个著名的持久层框架,本文首先介绍了MyBatis的简单使用,接着与Spring进行整合,最后简单地使用了Generator去自动生成代码。2MyBatis简介MyBatis本来是Apache的一个开源项目——iBatis,2010年由Apaceh

Java框架之Mybatis(二)

本文主要介绍Mybatis(一)之后剩下的内容:1mybatis中log4j的配置2dao层的开发(使用mapper代理的方式)3mybatis的配置详解4输入输出映射对应的类型(parameterType和resultType)5mybatis动态sql6mybatis中的一级缓存7mybat

Mybatis映射器源码解析

Mybatis映射器映射器是MyBatis最强大的⼯具,也是我们使用MyBatis时⽤得最多的工具,因此熟练掌握它⼗分必要。MyBatis是针对映射器构造的SQL构建的轻量级框架,并且通过配置生成对应的JavaBean返回给调用者,⽽这些配置主要便是映射器,在MyBatis中你可以根据情况定义动态SQL来满足不同场景的需要,它比其他框架

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