SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件
一、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 码云地址:知了一笑 https://gitee.com/cicadasmile

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
js变量提升总结
JavaScript 中,函数及变量的声明都将被提升到函数的最顶部。JavaScript 中,变量可以在使用后声明,也就是变量可以先使用再声明。 例1 var name = 'World!'; (function () { if (typeof name === 'undefined') { var name = 'Jack'; console.log('Goodbye ' + name); } else { console.log('Hello ' + name); } })(); 在自运行函数内,存在var name = 'jack',所以name会提升到当前作用域最前边,所以 name为undefined。当程序运行到变量的时候,会先在当前作用域内查找该变量,如果找不到,则会向父级作用域查找,如果还是找不到,就会报错 例2 var x = 1; // Initialize x console.log(x + " " + y); // '1 undefined' var y = 2; 相当于: var x = 1; // Initialize x var y; // Declare...
- 下一篇
在macOS上安装配置golang开发环境
本文主要介绍如何在macOS上安装配置golang开发环境。 一、安装 Homebrew 打开终端,输入以下命令安装 Homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 二、安装配置 golang 1. 通过brew方式安装golang 在终端中输入以下命令安装golang brew install go 或者 brew install golang 2. 通过下载安装包,安装golang 到官方网站 Downloads 下载golang软件安装包。 或者直接点击下面的链接,下载安装。 go1.12.9.darwin-amd64.pkg (121MB) 3. 配置goproxy代理 由于众所周知的原因,下载golang相关模块非常困难。 这里介绍一种相对靠谱的解决方案:go mod + goproxy 通过编辑器打开 .bashrc 或者 .zshrc,将以下配置贴到文件最后,并保存。 # Enable the go mod...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Windows10,CentOS7,CentOS8安装Nodejs环境
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- 2048小游戏-低调大师作品
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- 设置Eclipse缩进为4个空格,增强代码规范