SpringBoot整合Mybatis

2023-05-16

1.在pom.xml中添加依赖,有两种方式

  1. 第一种:直接在pom.xml中添加mybatis-spring-boot-starter依赖
   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--    springboot整合mybatis    -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- 集成druid连接池       -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
  1. 第二种:在pom.xml中添加物理分页插件pagehelper-spring-boot-starter依赖,该jar包底层依赖mybatis-spring-boot-starter,同时可以非常方便的实现分页功能。
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--    mybatis分页插件    -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.10</version>
        </dependency>
        <!--    集成druid连接池       -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!--    mysql数据库驱动       -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
        <!-- mybatis生成器-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

2.在application.properties中配置数据库链接信息与mapper-locations

spring.datasource.url=jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root

mybatis.mapper-locations=classpath:mapper/*.xml,classpath*:com/**/mapper/*.xml
#第一种:配置log-impl以便打印sql语句
#mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#第二种:设置mapper包的日志等级为debug以便打印sql语句
logging.level.com.mybatis.gathermybatis.mbg.mapper=DEBUG

mybatis两种打印sql语句配置的区别:

第一种:不显示类信息,显示结果集

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4df43477] was not registered for synchronization because synchronization is not active
Cache Hit Ratio [SQL_CACHE]: 0.0
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@244a932d] will not be managed by Spring
==>  Preparing: SELECT count(0) FROM pms_brand 
==> Parameters: 
<==    Columns: count(0)
<==        Row: 11
<==      Total: 1
==>  Preparing: select id, name, first_letter, sort, factory_status, show_status, product_count, product_comment_count, logo, big_pic from pms_brand LIMIT ? 
==> Parameters: 6(Integer)
<==    Columns: id, name, first_letter, sort, factory_status, show_status, product_count, product_comment_count, logo, big_pic
<==        Row: 1, 万和, W, 0, 1, 1, 100, 100, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg(5).jpg, 
<==        Row: 2, 三星, S, 100, 1, 1, 100, 100, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg (1).jpg, null
<==        Row: 3, 华为, H, 100, 1, 1, 100, 100, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/17f2dd9756d9d333bee8e60ce8c03e4c_222_222.jpg, null
<==        Row: 4, 格力, G, 30, 1, 1, 100, 100, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/dc794e7e74121272bbe3ce9bc41ec8c3_222_222.jpg, null
<==        Row: 5, 方太, F, 20, 1, 1, 100, 100, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg (4).jpg, null
<==        Row: 6, 小米, M, 500, 1, 1, 100, 100, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png, http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180518/5afd7778Nf7800b75.jpg
<==      Total: 6
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4df43477]

第二种:显示类信息,不显示结果集

2019-12-16 18:57:35.384 DEBUG 15276 --- [nio-8080-exec-1] c.m.g.m.m.P.selectByExample_COUNT        : ==>  Preparing: SELECT count(0) FROM pms_brand 
2019-12-16 18:57:35.398 DEBUG 15276 --- [nio-8080-exec-1] c.m.g.m.m.P.selectByExample_COUNT        : ==> Parameters: 
2019-12-16 18:57:35.407 DEBUG 15276 --- [nio-8080-exec-1] c.m.g.m.m.P.selectByExample_COUNT        : <==      Total: 1
2019-12-16 18:57:35.409 DEBUG 15276 --- [nio-8080-exec-1] c.m.g.m.m.P.selectByExample              : ==>  Preparing: select id, name, first_letter, sort, factory_status, show_status, product_count, product_comment_count, logo, big_pic from pms_brand LIMIT ? 
2019-12-16 18:57:35.410 DEBUG 15276 --- [nio-8080-exec-1] c.m.g.m.m.P.selectByExample              : ==> Parameters: 6(Integer)
2019-12-16 18:57:35.413 DEBUG 15276 --- [nio-8080-exec-1] c.m.g.m.m.P.selectByExample              : <==      Total: 6

3.在启动类或者自定义mybatis配置类(@Configuration注解)上添加mapper接口扫描@MapperScan("mapper包路径")

注意:@MapperScan()为mybaitis的。

@MapperScan()   import org.mybatis.spring.annotation.MapperScan;
@Configuration  import org.springframework.context.annotation.Configuration;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringBoot整合Mybatis 的相关文章

随机推荐

  • 我在华为度过的 “两辈子”(学习那些在大厂表现优秀的人)

    七 是一个很神奇的数字 生物学中认为组成身体的细胞 xff0c 七年会彻底更新一遍 xff1b 李笑来说 xff0c 七年就是一辈子 xff0c 每一辈子至少要习得一个重要的技能 xff0c 进而获得不可逆的重生 xff1b 白居易写道 x
  • 【Effective Java】大厂实战之考虑以静态工厂方法代替构造方法

    图片 Item1 考虑以静态工厂方法代替构造方法 Item1 考虑以静态工厂方法代替构造方法 缺点1 只提供静态工厂方法的话 xff0c 该类就无法被继承 xff08 子类化 xff09 缺点2 API的查找比较麻烦 常见静态工厂方法的命名
  • 一文搞懂leveldb写操作

    前言 leveldb一直也它优秀的写性能而文明 xff0c 本篇文章我们就来分析下leveldb的写流程 来搞懂为什么它的写性能如此优秀 整理流程 leveldb一次写入分为两部分 xff1a 第一步先将写操作写入日志 第二步将写操作应用到
  • 来聊聊对象文件网关和分布式文件存储的区别

    前言 大家都知道 xff0c 存储系统一般分为块存储 对象存储和文件存储三种 其中文件存储的使用最广泛 xff0c 个人电脑 NAS 大到传统的HPC 大数据平台等等 这些都是以使用文件接口为主 最近几年 xff0c 由于成本低 存储空间大
  • Ubuntu中文件属性以及所属用户问题

    1 xff0c 查看文件属性 xff0c 命令如下 xff1a 查看当前目录下某个文件属性 ls l lt file name gt 查看当前目录下文件属性 ls l 查看所有当前文件下属性 ls al 在开始的 rwxr xr x 为该文
  • Android8.0 屏幕旋转180度

    一般手机只能旋转3个方向 xff0c 这里将介绍如何让手机可以旋转180度 xff0c 也就是上下颠倒 1 静态方法 frameworks base core res res values config xml config allowAl
  • 深度理解go中的Map

    这里写自定义目录标题 前言map的内存模型增量扩容查找过程分析插入过程分析 前言 本篇将从底层讲解map的赋值 删除 查询 扩容的具体执行过程 结合源码 xff0c 让你彻底明白map的原理 map的内存模型 在源码中 xff0c 表示ma
  • 在职场我们该具备哪些能力

    专才or 通才 不知道大家有没有这样的感觉 xff0c 现在的工作专业化程度越来越高 xff0c 细分粒度也越来越小 IT领域分到你是计算里面的数据库或者了流式计算引擎 xff0c 或者是协议存储还是KV存储引擎 专业化的程度带来了一个好处
  • 【zabbix Java开发教程】docker部署zabbix及api获取实战教程

    文章目录 Docker安装移除旧版本的docker环境安装必要系统工具添加源信息更新yum缓存安装Docker ce 启动Docker测试运行hello world 修改源 Docker部署zabbix创建zabbix的MySQL运行zab
  • go版本分布式锁redsync使用教程

    redsync使用教程 前言redsync结构Pool结构Mutex结构acquire加锁操作release解锁操作 redsync包的使用 前言 在编程语言中锁可以理解为一个变量 xff0c 该变量在同一时刻只能有一个线程拥有 xff0c
  • Fragment实例精讲——底部导航栏的实现(3)

    转自 xff1a http www runoob com w3cnote android tutorial fragment demo3 html 本节引言 前面我们已经跟大家讲解了实现底部导航栏的两种方案 xff0c 但是这两种方案只适合
  • Android NavigationBar 背景设置成完全透明不起作用

    lt item name 61 34 android navigationBarColor 34 gt 64 android color transparent lt item gt 使用 android navigationBarColo
  • 计算机视觉CV中RANSAC算法的学习笔记~

    1 致谢 感谢网友叶晚zd的博客 xff0c 原文链接如下 xff1a https blog csdn net u013925378 article details 82907502 2 RANSAC算法介绍 随机抽样一致算法 xff08
  • 【Java】JsonArray用法

    1 解析字符串为JsonArray JSONArray jsonArray 61 JSONArray parseArray str 2 存值 jsonArray add object 3 取值 for int i 61 0 i lt jso
  • 怎么用JMeter写性能测试脚本

    JMeter 可能是应用最广泛的性能测试工具 怎么用 JMeter 编写性能测试脚本 xff1f 一 脚本制作原则 我们一般写代码 xff0c 都会有代码规范 xff0c 比如写java有java规范 xff0c 写python有pytho
  • Android MVP Contract分析

    相关文章 xff1a Android MVP Contract 谷歌官方MVP Contract分析 View 只处理UI及页面效果的细节 xff0c 向Presenter暴露更新UI的方法 xff1b 并且持有Presenter的引用 x
  • 《Flask Web开发实战》第一章初识Flask——李辉,读书笔记

    目录 第一章 初识Flask开发环境的搭建使用Pipenv管理Python包 hello Flask启动开发服务器修改默认的重载器修改配置参数实现类似flask run的命令的方式 结尾 第一章 初识Flask 本章主要介绍了一些关于Fla
  • 基于cat12和SPM12进行VBM&SBM数据分析笔记1——数据预处理

    前言 今年是小编步入研究生生活的第一年 xff0c 研究方向待定 xff0c 但主要以磁共振成像为主 xff0c 以后会不断地总结这方面的知识 xff0c 涉及MRI xff0c 数据分析基础方法理论 xff0c 软件操作教程 xff0c
  • SpringBoot打war包运行在独立Tomcat的方式

    第一种方式 xff1a 手动配置 1 修改pom xml文件 1 添加 lt packaging gt war lt packaging gt span class token operator lt span span class tok
  • SpringBoot整合Mybatis

    1 在pom xml中添加依赖 xff0c 有两种方式 第一种 xff1a 直接在pom xml中添加mybatis spring boot starter依赖 span class token generics function span