SpringBoot2中如何整合Mybatis框架
发表于:2025-11-16 作者:千家信息网编辑
千家信息网最后更新 2025年11月16日,本篇内容主要讲解"SpringBoot2中如何整合Mybatis框架",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"SpringBoot2中如何整合Myba
千家信息网最后更新 2025年11月16日SpringBoot2中如何整合Mybatis框架
本篇内容主要讲解"SpringBoot2中如何整合Mybatis框架",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"SpringBoot2中如何整合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,接近JDBC5)简单的持久化框架,框架不臃肿简单易学
3、适用场景
MyBatis专注于SQL本身,是一个足够灵活的DAO层解决方案。
对性能的要求很高,或者需求变化较多的项目,MyBatis将是不错的选择。
二、与SpringBoot2整合
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);// 组合查询ListselectByExample(ImgInfoExample example);// 修改int updateByPrimaryKeySelective(ImgInfo record);// 删除int deleteByPrimaryKey(Integer imgId);
6、编写接口实现
@Servicepublic 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、控制层测试类
@RestControllerpublic 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/inserthttp://localhost:8010/selectByExamplehttp://localhost:8010/updateByPrimaryKeySelectivehttp://localhost:8010/deleteByPrimaryKey
三、集成分页插件
1、mybatis配置文件
2、分页实现代码
@Overridepublic PageInfoqueryPage(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
到此,相信大家对"SpringBoot2中如何整合Mybatis框架"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
框架
文件
查询
配置
整合
代码
接口
测试
内容
对象
条件
核心
结果
项目
学习
控制
组合
不错
更深
臃肿
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发行业免增值税的文件
新开云服务器
国外服务器网站
徐州通用软件开发资费
聊城搜房网络技术有限公司
防止服务器被攻击
西安游戏人网络技术公司
软件开发外包科技公司
ofo软件开发项目
每年国家网络安全周在几月份举行
智慧生活服务器
服务器对外开放下载
数据库是由一行行的记录组成的吗
nginx服务器更换ssl证书
独特互联网科技公司名称
阿里云服务器安全软件
联想服务器单盘启动
打开网络安全手抄报还不用
访问服务器上的虚拟机
芒果tv 网络安全主管
还原数据库2008r2
数据库设置数据表显示的字体大小
计算机网络技术中级视频
南邮网络安全研究生毕业薪资
全球服务器运维大会
上位机要学数据库吗
百度公司网络安全工资
网络安全法在线考试答案
数据库应用时的问题
北京oa软件开发诚信企业