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

  

一、Mybatis框架

  

1, mybatis简介

  

MyBatis是一款优秀的持久层框架,它支持定制化SQL,存储过程以及高级映射.MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集.MyBatis可以使用简单的XML或注解来配置和映射原生类型,接口和Java的POJO (Plain Old Java object,普通老式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框架,集成分页助手插件“> <br/>采用德鲁伊连接池,该连接池。</p>
  <h3> 2,核心依赖</h3>
  <pre> <代码> & lt; !——mybatis依赖——比;
  & lt; dependency>
  & lt; groupId> org.mybatis.spring.boot</groupId>
  & lt; artifactId> mybatis-spring-boot-starter</artifactId>
  & lt; version> 1.3.2</version>
  & lt;/dependency>
  & lt; !——mybatis的分页插件——比;
  & lt; dependency>
  & lt; groupId> com.github.pagehelper</groupId>
  & lt; artifactId> pagehelper</artifactId>
  & lt; version> 4.1.6</version>
  & lt;/dependency> </代码> </pre>
  <h3> 3,核心配置</h3>
  <pre> <代码> mybatis:
  # mybatis配置文件所在路径
  config-location:类路径:mybatis.cfg.xml
  type-aliases-package: com.boot.mybatis.entity
  # mapper映射文件
  mapper-locations:类路径:mapper/* . xml </代码> </pre>
  <h3> 4,逆向工程生成的文件</h3>
  <p> <img src=//增加   int插入(ImgInfo记录);//组合查询   ListselectByExample (ImgInfoExample例子);//修改   int updateByPrimaryKeySelective (ImgInfo记录);//删除   int deleteByPrimaryKey(整数imgId);   

6,编写接口实现

  
 <=坝镅詊ava代码类> @ service
  公共类ImgInfoServiceImpl实现ImgInfoService {
  @
  私人ImgInfoMapper ImgInfoMapper;
  @Override
  公共int插入(ImgInfo记录){
  返回imgInfoMapper.insert(记录);
  }
  @Override
  公共ListselectByExample (ImgInfoExample例子){
  返回imgInfoMapper.selectByExample(例子);
  }
  @Override
  公共int updateByPrimaryKeySelective (ImgInfo记录){
  返回imgInfoMapper.updateByPrimaryKeySelective(记录);
  }
  @Override
  公共int deleteByPrimaryKey(整数imgId) {
  返回imgInfoMapper.deleteByPrimaryKey (imgId);
  }
  } 
  

7、控制层测试类

  
 <=坝镅詊ava代码类> @RestController
  公开课ImgInfoController {
  @
  私人ImgInfoService ImgInfoService;//增加
  @RequestMapping("/插入”)
  公共int插入(){
  ImgInfo记录=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);
  记录。setCreateDate(新日期());
  record.setUpdateDate (record.getCreateDate ());
  record.setRemark(“知了”);
  record.setbEnable (" 1 ");
  返回imgInfoService.insert(记录);
  }//组合查询
  @RequestMapping ("/selectByExample”)
  公共ListselectByExample () {
  ImgInfoExample示例=new ImgInfoExample ();
  example.createCriteria () .andRemarkEqualTo(“知了”);
  返回imgInfoService.selectByExample(例子);
  }//修改
  @RequestMapping ("/updateByPrimaryKeySelective”)
  公共int updateByPrimaryKeySelective () {
  ImgInfo记录=new ImgInfo ();
  record.setImgId (11);
  record.setRemark(“知了一笑”);
  返回imgInfoService.updateByPrimaryKeySelective(记录);
  }//删除
  @RequestMapping ("/deleteByPrimaryKey”)
  公共int deleteByPrimaryKey () {
  整数imgId=11;
  返回imgInfoService.deleteByPrimaryKey (imgId);
  }
  }

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