iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >SpringBoot2中怎么整合Mybatis框架
  • 318
分享到

SpringBoot2中怎么整合Mybatis框架

2023-06-02 12:06:10 318人浏览 安东尼
摘要

SpringBoot2中怎么整合mybatis框架,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、Mybatis框架1、mybatis简介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、项目结构图

SpringBoot2中怎么整合Mybatis框架

采用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、逆向工程生成的文件

SpringBoot2中怎么整合Mybatis框架

这里就不贴代码了。

5、编写基础测试接口

// 增加int insert(ImgInfo record);// 组合查询List<ImgInfo> selectByExample(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<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、控制层测试类

@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<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/inserthttp://localhost:8010/selectByExamplehttp://localhost:8010/updateByPrimaryKeySelectivehttp://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、分页实现代码

@Overridepublic 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

看完上述内容,你们掌握SpringBoot2中怎么整合Mybatis框架的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网精选频道,感谢各位的阅读!

--结束END--

本文标题: SpringBoot2中怎么整合Mybatis框架

本文链接: https://www.lsjlt.com/news/230118.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringBoot2中怎么整合Mybatis框架
    SpringBoot2中怎么整合Mybatis框架,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、Mybatis框架1、mybatis简介MyBatis 是一款优秀的持久层框...
    99+
    2023-06-02
  • SpringBoot2中怎么整合ElasticJob框架
    这篇文章将为大家详细讲解有关SpringBoot2中怎么整合ElasticJob框架,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。一、ElasticJob简介1、定时任务在前面的文章中,说过...
    99+
    2023-06-02
  • Spring+SpringMVC+MyBatis+Maven框架整合
    本文记录了Spring+SpringMVC+MyBatis+Maven框架整合的记录,主要记录以下几点 一、Maven需要引入的jar包 二、Spring与SpringMVC的配置分离&...
    99+
    2024-04-02
  • 若依框架整合mybatis-plus
    在使用若依(RuoYi-vue)时,发现若依用的是mybatis而不是mybatis-plus,所以为了保留原有代码生成器生成的方法,外加入mybaits-plus,故有了下面的内容: ruoyi-admin的prom.xml中添加myba...
    99+
    2023-08-17
    mybatis java mysql
  • SSM框架整合(Spring+SpringMVC+MyBatis)
    【SSM的系统架构】【整合概述】  MyBatis和Spring整合,通过Spring管理mapper接口。  使用mapper的扫描器自动扫描mapper接口在Spring中进行注册。  通过Spring管理Service接口。  使用配...
    99+
    2023-06-03
  • springboot框架中如何整合mybatis框架思路详解
    目录springboot框架中如何整合mybatis框架 一、在pom.xml 文件引入对应依赖二、写配置springboot框架中如何整合mybatis框架 思路: 1....
    99+
    2022-12-20
    springboot整合mybatis框架 springboot整合mybatis
  • SpringBoot2中怎么整合ElasticSearch框架实现高性能搜索引擎
    SpringBoot2中怎么整合ElasticSearch框架实现高性能搜索引擎,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。1、核心依赖<dependency>...
    99+
    2023-06-02
  • Spring+SpringMVC+MyBatis整合实战(SSM框架)
    目录SpringMVCSpringMyBatis项目结构maven配置文件pom.xmlwebapp配置文件web.xmlspring配置文件applicationContext.x...
    99+
    2024-04-02
  • SpringBoot框架整合Mybatis简单攻略
    目录步骤 1 添加mybatis-starter依赖步骤 2 如何配置mybatis到SpringBoot项目步骤 3 测试查询步骤 4 mybatis注解方式步骤 5 用注解方式做...
    99+
    2024-04-02
  • MyBatis-Plus框架整合详细方法
    目录MyBatis-Plus其特性有:引入依赖yml配置主启动类加上包扫码自动生产代码接口util扩展MyBatis-Plus MyBatis-Plus(简称 MP)是一个 MyBa...
    99+
    2024-04-02
  • SpringBoot2中怎么整合Kafka组件
    SpringBoot2中怎么整合Kafka组件,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、搭建Kafka环境1、下载解压-- 下载wget http:...
    99+
    2023-06-02
  • SpringBoot怎么整合JPA框架
    这篇文章主要介绍了SpringBoot怎么整合JPA框架的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot怎么整合JPA框架文章都会有所收获,下面我们一起来看看吧。一. Spring Boot数...
    99+
    2023-07-04
  • SpringBoot2 整合SpringSecurity框架是怎么实现用户权限安全管理
    这篇文章给大家介绍SpringBoot2 整合SpringSecurity框架是怎么实现用户权限安全管理,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。一、Security简介1、基础概念Spring Security是...
    99+
    2023-06-05
  • SpringBoot2中怎么整合ClickHouse数据库
    这期内容当中小编将会给大家带来有关SpringBoot2中怎么整合ClickHouse数据库,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。一、ClickHouse简介1、基础简介Yandex开源的数据分析...
    99+
    2023-06-02
  • SpringBoot2如何整合Shiro框架实现用户权限管理
    小编给大家分享一下SpringBoot2如何整合Shiro框架实现用户权限管理,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一、Shiro简介1、基础概念Apac...
    99+
    2023-06-02
  • SpringBoot2如何整合JWT框架解决Token跨域验证问题
    小编给大家分享一下SpringBoot2如何整合JWT框架解决Token跨域验证问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一、传统Session认证1、认...
    99+
    2023-06-02
  • 干货必看|Spring Boot整合MyBatis框架详解
    在开发中,我们通常会对数据库的数据进行操作,Sprirng Boot对关系型数据库和非关系型数据库的访问操作都提供了非常好的整合支持。所以今天壹哥就给大家讲解一下,如何在SpringBoot环境中整合Mybatis框架,请大家认真看哦。 一...
    99+
    2023-10-25
    mybatis 数据库 java
  • 怎么在springboot中整合jquery和bootstrap框架
    这篇文章主要介绍了怎么在springboot中整合jquery和bootstrap框架的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么在springboot中整合jquery和bootstrap框架文章都会有...
    99+
    2023-06-17
  • SSM框架整合之Spring+SpringMVC+MyBatis实践步骤
    目录1、基本概念1.1、Spring1.2、SpringMVC1.3、MyBatis2、开发环境搭建以及创建Maven Web项目3、SSM整合3.1、Maven引入需要的JAR包3...
    99+
    2024-04-02
  • 如何在SpringBoot2中整合Filter
    今天小编给大家分享一下如何在SpringBoot2中整合Filter的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。首先定义一...
    99+
    2023-07-06
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作